0

How to fix the error in the code, I'm using python 3.7, macOS high sierra installed libraries are: sklearn matplotlib numpy.

code:

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

Error:

  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.
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
  • The code, as posted, appears to have indentation that is messed up. Did you simply copy-paste from elsewhere?... – code_dredd Sep 04 '18 at 22:26
  • I copied it from my idle where it is properly indented, i was asked to give space before code lines while uploading this question here. So i did but while doing so i didn't spaced all lines equally. So it appears not intended properly here but in my IDLE it is properly intended. – Harman Thind Sep 05 '18 at 05:15
  • You should've corrected that. Everyone seems to think your code issues are indentation problems. Adding space before code is simply to make sure it formats properly on the site (4 spaces minimum). – code_dredd Sep 05 '18 at 16:19

1 Answers1

0

Have you indented correctly ?

I have runned your code on my machine (Windows 8.1) with proper indentation and it worked fine.

Indented code:

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

Furthermore, if it doesn't work, then try changing the kernel/interpreter. Try Python 3.6.x kernel/interpreter.

p.s: I have used Python 3.6.0 on Thonny IDE to run test this code and it worked fine on my machine.

Mohammad Zain Abbas
  • 728
  • 1
  • 11
  • 24
  • Yes it is intended correctly. I copied it from my idle where it is properly indented, i was asked to give space before code lines while uploading this question here. So i did but while doing so i didn't spaced all lines equally. So it appears not intended properly here but in my IDLE it is properly intended. How to change kernel/interpreter in python regular IDLE? – Harman Thind Sep 05 '18 at 17:38
  • @HarmanThind Normally, each python version come with it's own IDLE. Please see this post https://stackoverflow.com/a/4776384/6390175 for more details. – Mohammad Zain Abbas Sep 06 '18 at 09:26
  • @HarmanThind I am using Thonny IDE for Python and in it, you can change your interpreter via Tools -> Options -> Interpreter. – Mohammad Zain Abbas Sep 06 '18 at 09:28
  • @HarmanThind I ran above code on Python 3.7.0 to test it. And it worked fine. It's working fine with Python 3.6 as well. – Mohammad Zain Abbas Sep 06 '18 at 09:29
  • @HarmanThind Please check the code now, I have made a minor fix. It should be able to work well now. – Mohammad Zain Abbas Sep 06 '18 at 11:44
  • It worked finally: print('prediction:',clf.predict([digits.data[-2]])) i can't believe the missing brackets were causing error – Harman Thind Sep 06 '18 at 14:36
  • Thank you so much for the help – Harman Thind Sep 06 '18 at 14:37
  • @HarmanThind Glad I can help :') You may accept the answer by approving it though so others may kind it helpful also. :') – Mohammad Zain Abbas Sep 06 '18 at 14:52