【问题标题】:Python Sklearn - Deprecation WarningPython Sklearn - 弃用警告
【发布时间】:2016-09-30 04:59:50
【问题描述】:

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

DeprecationWarning:将一维数组作为数据传递在 0.17 中已弃用 并将在 0.19 中引发 ValueError。

代码如下:

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()

再次感谢您的关注。顺便说一句,我使用的是 Python 3.5.1。

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      我觉得您的代码还有其他问题。跑的时候发现X和Y的数据点个数不一样。 X有100,Y有50,这是一个比较严重的问题,我觉得这需要先纠正。

      【讨论】:

        猜你喜欢
        • 2017-09-04
        • 2016-08-21
        • 2017-12-28
        • 2017-08-22
        • 2018-07-15
        • 2021-02-19
        相关资源
        最近更新 更多