【问题标题】:How do I solve this Error with Array Dimensionality如何使用数组维度解决此错误
【发布时间】:2022-11-10 23:36:53
【问题描述】:

我尝试按照有关机器学习算法的教程进行操作,但我不断收到此错误。它一直给我这个错误,我尝试了各种调试方式,但仍然收到相同的错误消息。我该怎么办?

堆栈溢出不希望我发布所有代码,所以我试图缩短错误消息

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

numberImages = datasets.load_digits()

supportVectorClassifier = svm.SVC(gamma=0.0001, C=100)

x = numberImages.data[:-5]
y = numberImages.target[:-5]
supportVectorClassifier.fit(x,y)

predictedImage = numberImages.data[-4]

print ("Assume the image is: ", supportVectorClassifier.predict(predictedImage))

plt.imshow(numberImages.images[-4],cmap = plt.cm.gray_r, interpolation="nearest")
plt.show()

Traceback (most recent call last):
  File "c:\Users\...\Documents\Algorithim\App.py", line 15, in <module>
    print ("Assume the image is: ", supportVectorClassifier.predict(predictedImage))
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\svm\_base.py", line 810, in predict
    y = super().predict(X)
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\svm\_base.py", line 433, in predict
    X = self._validate_for_predict(X)
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\svm\_base.py", line 611, in _validate_for_predict
    X = self._validate_data(
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\base.py", line 577, in _validate_data
    X = check_array(X, input_name="X", **check_params)
py", line 879, in check_array
    raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=[ 0.  0.  6. 16. 13. 11.  1.  0.  0.  0. 16. 15. 12. 16.  1.  0.  0.  3.
 16.  7.  0. 13.  6.  0.  0.  4. 16.  0.  0. 10.  8.  0.  0.  8. 16.  0.
  0. 14.  6.  0.  0.  5. 16.  7.  9. 16.  5.  0.  0.  1. 15. 16. 16. 16.
  1.  0.  0.  0.  6. 16. 14.  6.  0.  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.
PS C:\Users\...> & C:/Users/.../AppData/Local/Programs/Python/Python310/python.exe c:/Users/.../Documents/Algorithim/App.py"C:\Users\RESIDENT_ALIEN3\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\svm\_base.py", line 810, in predict
    y = super().predict(X)
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\svm\_base.py", line 433, in predict
    X = self._validate_for_predict(X)
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\svm\_base.py", line 611, in _validate_for_predict
    X = self._validate_data(
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\base.py", line 577, in _validate_data
    X = check_array(X, input_name="X", **check_params)
  File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\validation.py", line 879, in check_array
    raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=[ 0.  0.  6. 16. 13. 11.  1.  0.  0.  0. 16. 15. 12. 16.  1.  0.  0.  3.
 16.  7.  0. 13.  6.  0.  0.  4. 16.  0.  0. 10.  8.  0.  0.  8. 16.  0.
  0. 14.  6.  0.  0.  5. 16.  7.  9. 16.  5.  0.  0.  1. 15. 16. 16. 16.
  1.  0.  0.  0.  6. 16. 14.  6.  0.  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

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    如果您查看您的错误,它表明该函数需要一个 2D 数组,但您只传递了一个 1D 数组。这是因为predictedImage = numberImages.data[-4] 返回一个一维数组。

    如果你再看看你的堆栈跟踪,你可以看到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。由于在您的情况下您只有一个样本,因此您需要使用reshape(1, -1) 重塑predictedImage。这给你

    import matplotlib.pyplot as plt
    from sklearn import datasets
    from sklearn import svm
    
    numberImages = datasets.load_digits()
    
    supportVectorClassifier = svm.SVC(gamma=0.0001, C=100)
    
    x = numberImages.data[:-5]
    y = numberImages.target[:-5]
    supportVectorClassifier.fit(x, y)
    
    predictedImage = numberImages.data[-4]
    
    predictedImage = predictedImage.reshape(1, -1)
    
    print("Assume the image is: ", supportVectorClassifier.predict(predictedImage))
    
    plt.imshow(numberImages.images[-4], cmap=plt.cm.gray_r, interpolation="nearest")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2021-12-11
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2012-02-14
      相关资源
      最近更新 更多