【问题标题】:Sklearn digits datasetSklearn 数字数据集
【发布时间】:2017-03-07 13:27:02
【问题描述】:
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn import svm

digits = datasets.load_digits()

print(digits.data)

classifier = svm.SVC(gamma=0.4, C=100)
x, y = digits.data[:-1], digits.target[:-1]

x = x.reshape(1,-1)
y = y.reshape(-1,1)
print((x))

classifier.fit(x, y)
###
print('Prediction:', classifier.predict(digits.data[-3]))
###
plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()

我也重塑了 x 和 y。我仍然收到一条错误消息:

发现样本数量不一致的输入变量:[1, 1796]

Y 有 1796 个元素的一维数组,而 x 有很多。它如何为 x 显示 1?

【问题讨论】:

    标签: python python-3.x scikit-learn sklearn-pandas


    【解决方案1】:

    实际上放弃我下面的建议:

    This link describes the general dataset API。属性data 是每个图像的二维数组,已经展平:

    import sklearn.datasets
    digits = sklearn.datasets.load_digits()
    digits.data.shape
    #: (1797, 64)
    

    这就是您需要提供的全部内容,无需重新塑造。同样,data 属性是每个标签的一维数组:

    digits.data.shape
    #: (1797,)
    

    无需重塑。只需拆分为训练和测试并运行即可。


    尝试打印x.shapey.shape。我觉得你会找到类似的东西:(1, 1796, ...)(1796, ...) 分别。在 scikit 中为分类器调用 fit 时,它需要两个形状相同的迭代。

    线索,为什么在重塑不同的方式时会出现争论:

    x = x.reshape(1, -1)
    y = y.reshape(-1, 1)
    

    不妨试试:

    x = x.reshape(-1, 1)
    

    与您的问题完全无关,但您预测 digits.data[-3] 时训练集中唯一遗漏的元素是 digits.data[-1]。不确定这是不是故意的。

    无论如何,最好使用 scikit 指标包检查您的分类器以获得更多结果。 This page has an example of using it over the digits dataset.

    【讨论】:

    • 它给出一个错误:Found input variables with inconsistent numbers of samples: [114944, 1796]
    • @lithum 像我建议的那样打印x.shapey.shape 的结果是什么?
    • @linthum 实际上做了一些改变。我们都错了。
    • 我重新塑造了它,因为它给出了警告 .19 版本的 sklearn。 DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)
    【解决方案2】:

    重塑会将您的 8x8 矩阵转换为一维向量,该向量可用作特征。您需要重塑整个 X 向量,而不仅仅是那些训练数据,因为您将用于预测的向量需要具有相同的格式。

    下面的代码展示了如何:

    import matplotlib.pyplot as plt
    
    from sklearn import datasets
    from sklearn import svm
    
    digits = datasets.load_digits()
    
    
    classifier = svm.SVC(gamma=0.4, C=100)
    x, y = digits.images, digits.target
    
    #only reshape X since its a 8x8 matrix and needs to be flattened
    n_samples = len(digits.images)
    x = x.reshape((n_samples, -1))
    print("before reshape:" + str(digits.images[0]))
    print("After reshape" + str(x[0]))
    
    
    classifier.fit(x[:-2], y[:-2])
    ###
    print('Prediction:', classifier.predict(x[-2]))
    ###
    plt.imshow(digits.images[-2], cmap=plt.cm.gray_r, interpolation='nearest')
    plt.show()
    
    ###
    print('Prediction:', classifier.predict(x[-1]))
    ###
    plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, interpolation='nearest')
    plt.show()
    

    它会输出:

    before reshape:[[  0.   0.   5.  13.   9.   1.   0.   0.]
     [  0.   0.  13.  15.  10.  15.   5.   0.]
     [  0.   3.  15.   2.   0.  11.   8.   0.]
     [  0.   4.  12.   0.   0.   8.   8.   0.]
     [  0.   5.   8.   0.   0.   9.   8.   0.]
     [  0.   4.  11.   0.   1.  12.   7.   0.]
     [  0.   2.  14.   5.  10.  12.   0.   0.]
     [  0.   0.   6.  13.  10.   0.   0.   0.]]
    After reshape[  0.   0.   5.  13.   9.   1.   0.   0.   0.   0.  13.  15.  10.  15.   5.
       0.   0.   3.  15.   2.   0.  11.   8.   0.   0.   4.  12.   0.   0.   8.
       8.   0.   0.   5.   8.   0.   0.   9.   8.   0.   0.   4.  11.   0.   1.
      12.   7.   0.   0.   2.  14.   5.  10.  12.   0.   0.   0.   0.   6.  13.
      10.   0.   0.   0.]
    

    最后 2 张未用于训练的图像的正确预测 - 但是您可以决定在测试集和训练集之间进行更大的分割。

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 2023-04-04
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 2020-03-12
      • 2017-05-18
      • 2016-06-25
      • 2018-09-11
      相关资源
      最近更新 更多