【问题标题】:how to fix Python Error, In description is the code and the error如何修复 Python 错误,描述是代码和错误
【发布时间】:2019-02-09 19:13:39
【问题描述】:

如何修复代码中的错误,我用的是python 3.7,macOS high sierra 安装的库是:sklearnmatplotlibnumpy。

代码:

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
import numpy


digits=datasets.load_digits()
'''
print(digits.data)
print(digits.target)
print(digits.images[0])
 '''

clf=svm.SVC(gamma=0.001, C=1.0)

print(len(digits.data))

x,y = digits.data[:-1],digits.target[:-1]
clf.fit(x,y)

print('prediction:',clf.predict(digits.data[-1]))
plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, 
interpolation="nearest")
plt.show()

错误:

  Traceback (most recent call last):
   File "/Users/harmanthind/Documents/Python/scikit learn 
  liberary/pehla.py", line 21, in <module>
  print('prediction:',clf.predict(digits.data[-1]))

  File"/Library/Frameworks/ 
   Python.framework/Versions/3.7/lib/python3.7/site- 
    packages/sklearn/svm/base.py", line 548, in predict
  y = super(BaseSVC, self).predict(X)
    File 
 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
  packages/sklearn/svm/base.py", line 308, in predict
   X = self._validate_for_predict(X)
   File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
packages/sklearn/svm/base.py", line 439, in _validate_for_predict
 X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C")
 File 
 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
 packages/sklearn/utils/validation.py", line 441, in check_array
 "if it contains a single sample.".format(array))
 ValueError: Expected 2D array, got 1D array instead:
 array=[ 0.  0. 10. 14.  8.  1.  0.  0.  0.  2. 16. 14.  6.  1.  0.  0.  
 0.  0.
  15. 15.  8. 15.  0.  0.  0.  0.  5. 16. 16. 10.  0.  0.  0.  0. 12. 
  15.
  15. 12.  0.  0.  0.  4. 16.  6.  4. 16.  6.  0.  0.  8. 16. 10.  8. 
  16.
  8.  0.  0.  1.  8. 12. 14. 12.  1.  0.].
  Reshape your data either using array.reshape(-1, 1) if your data has 
   a single feature or array.reshape(1, -1) if it contains a single 
   sample.

【问题讨论】:

  • 发布的代码似乎有混乱的缩进。您只是从其他地方复制粘贴吗?...
  • 我从空闲位置复制了它正确缩进的位置,在此处上传此问题时,我被要求在代码行之前留出空间。所以我做到了,但在这样做的同时,我并没有将所有行平均间隔开。所以它在这里看起来不是正确的,但在我的 IDLE 中它是正确的。
  • 你应该已经纠正了。每个人似乎都认为您的代码问题是缩进问题。在代码前添加空格只是为了确保它在网站上的格式正确(最少 4 个空格)。

标签: python arrays python-3.x scikit-learn libsvm


【解决方案1】:

您缩进是否正确?

我已经在我的机器 (Windows 8.1) 上运行了您的代码,并使用了适当的缩进,它运行良好很好。

缩进代码:

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
import numpy


digits=datasets.load_digits()
'''
 print(digits.data)
 print(digits.target)
 print(digits.images[0])
 '''

clf=svm.SVC(gamma=0.001, C=1.0)

print(len(digits.data))

x,y = digits.data[:-1],digits.target[:-1]
clf.fit(x,y)

print('prediction:',clf.predict([digits.data[-1]]))
plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, interpolation="nearest")
plt.show()

此外,如果它不起作用,请尝试更改内核/解释器。试试 Python 3.6.x 内核/解释器。

p.s:我在 Thonny IDE 上使用 Python 3.6.0 运行测试此代码,它在我的机器上运行良好。

【讨论】:

  • 是的,它的目的是正确的。我从空闲的地方复制了它正确缩进的地方,在此处上传此问题时,我被要求在代码行之前留出空间。所以我做到了,但在这样做的同时,我并没有将所有行平均间隔开。所以它在这里似乎不是正确的,但在我的 IDLE 中它是正确的。如何在 python 常规 IDLE 中更改内核/解释器?
  • @HarmanThind 通常,每个 python 版本都有自己的 IDLE。请参阅此帖子stackoverflow.com/a/4776384/6390175 了解更多详情。
  • @HarmanThind 我正在使用 Thonny IDE for Python,在其中,您可以通过工具 -> 选项 -> 解释器更改解释器。
  • @HarmanThind 我在 Python 3.7.0 上运行了上面的代码来测试它。它工作得很好。它也适用于 Python 3.6。
  • @HarmanThind 请现在检查代码,我做了一个小修复。现在应该可以正常使用了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多