Python Sklearn-弃用警告

2024-09-27 07:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和Sklearn的初学者。不知道我是否遗漏了什么。我收到以下警告消息:

DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.

代码如下:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDClassifier
from sklearn.datasets.samples_generator import make_blobs

def plot_sgd_separator():
    # we create 50 separable points
    X, Y = make_blobs(n_samples=50, centers=2,random_state=0, cluster_std=0.60)
    X = np.array(X).reshape((1, -1))


    # fit the model
    clf = SGDClassifier(loss="hinge", alpha=0.01,
                        n_iter=200, fit_intercept=True)
    clf.fit(X, Y)

    # plot the line, the points, and the nearest vectors to the plane
    xx = np.linspace(-1, 5, 10)
    yy = np.linspace(-1, 5, 10)

    X1, X2 = np.meshgrid(xx, yy)
    Z = np.empty(X1.shape)
    for (i, j), val in np.ndenumerate(X1):
        x1 = val
        x2 = X2[i, j]
        p = clf.decision_function([x1, x2])
        Z[i, j] = p[0]
    levels = [-1.0, 0.0, 1.0]
    linestyles = ['dashed', 'solid', 'dashed']
    colors = 'k'

    ax = plt.axes()
    ax.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles)
    ax.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)

    ax.axis('tight')


if __name__ == '__main__':
    plot_sgd_separator()
    plt.show()

再次感谢您的关注。顺便说一下,我使用的是Python3.5.1。


Tags: andtheinimportplotasnpplt
2条回答

我想你的问题已经被回答了,这可能是一个重复的

如果您阅读了警告消息并进行了一些调试,您将意识到出现警告是因为您对模型的输入是一维的。你可以看到这个链接:Sklearn train model with single sample raises a DeprecationWarning来纠正这个问题。

我觉得你的代码还有其他问题。当我运行它时,我看到X和Y中的数据点数量不一样。X有100,Y有50,这是一个更严重的问题,我觉得这需要首先纠正。

相关问题 更多 >

    热门问题