import matplotlib.pyplot as pltimport matplotlib as cmapfrom sklearn import datasetsfrom sklearn import svmdigits = datasets.load_digits()clf = svm.SVC(gamma=0.001, C=100)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.8.3中运行这段代码时,我得到了以下错误信息:
Traceback (most recent call last): File "C:\Users\Aben\Downloads\pythonstuff\machinelearning.py", line 18, in <module> plt.imshow(digits.images[-1], cmap-plt.cm.gray_r, interpolation-"nearest")TypeError: unsupported operand type(s) for -: 'module' and 'LinearSegmentedColormap'
有谁知道这是为什么吗?我顺便提一下,我正在使用这个教程
回答:
不知为何,您在向imshow()
传递参数时使用了-
而不是=
。请按以下方式修复:
plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, interpolation="nearest")