Python 用数字学习
我不明白 我正在尝试使用scikit learn with matplotlib with digits dataset 这是我的密码Python 用数字学习,python,matplotlib,machine-learning,scikit-learn,Python,Matplotlib,Machine Learning,Scikit Learn,我不明白 我正在尝试使用scikit learn with matplotlib with digits dataset 这是我的密码 import matplotlib.pyplot as plt from sklearn import datasets from sklearn import svm from sklearn.model_selection import train_test_split digits = datasets.load_digits() clf = svm.
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
digits = datasets.load_digits()
clf = svm.SVC(gamma=0.001, C=100.)
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=2017)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
print("Prediction: {}".format(pred))
plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
matplotlib绘图图像编号4,当我尝试打印预测时,它每次都会打印此输出
Prediction: [8 1 3 8 5 8 1 4 9 7 5 2 1 4 3 2 4 9 5 4 1 9 2 4 7 8 9 3 1 7 5 7 6 2 0 5 7
1 6 1 9 4 4 5 3 7 3 6 3 3 9 8 5 2 6 1 1 1 4 5 4 2 8 2 7 2 9 7 8 9 1 2 8 0
7 8 9 0 1 5 4 0 0 9 2 6 7 8 6 5 1 3 1 8 7 7 2 2 2 6 7 4 1 7 2 5 8 3 4 2 3
7 6 1 1 0 3 0 2 5 9 3 1 6 9 5 6 2 0 3 2 7 4 6 5 3 9 5 1 5 6 0 1 8 6 5 1 6
2 1 2 5 0 2 3 4 2 4 9 4 4 2 3 9 2 9 8 2 5 9 9 7 3 7 8 1 4 9 2 9 5 1 8 7 4
8 2 7 6 9 8 8 3 7 1 9 1 4 5 7 0 5 9 3 5 0 5 0 5 5 2 1 3 5 3 2 8 4 7 4 7 3
7 2 9 5 6 2 8 0 5 0 2 1 9 2 9 6 1 0 1 7 6 3 1 0 3 2 4 0 6 1 2 1 6 2 8 2 7
1 5 6 6 9 2 1 4 4 8 0 7 6 2 5 0 4 5 5 5 5 7 4 8 1 0 8 4 8 7 2 5 5 7 3 2 4
4 7 8 2 0 7 1 4 0 9 6 1 8 5 5 1 5 6 1 7 1 5 5 8 4 6 6 0 6 5 0 9 8 0 8 0 9
2 0 9 5 7 0 8 1 7 0 6 7 7 0 0 7 7 5 0 3 2 2 8 8 7 7 0]
我试图在matplotlib中打印数字,但它显示了此输出
我希望打印预测:8好的,下面是评论中的人们试图告诉你的:
X_测试
不是单个数据点,而是整个测试集。包括多少个样品
X_test.shape
# (360, 64)
因此,它包括360个样本;因此,您的pred
变量还必须包含所有这些360个样本的预测。事实上:
pred.shape
# (360,)
是否要在X\u测试中检查第一个样本的预测
pred[0]
# 8
这一预测的基本事实是什么
y_test[0]
# 8
看来您对第一个测试样本的预测是正确的。是否要绘制此示例(X_测试[0]
)?您应该首先将其重新整形为(8,8)
,因为列车测试分割加载数字()
plt.imshow(X_test[0].reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
嗯,它看起来像是8(一种).你不明白什么?请解释清楚。pred
数组返回X_test
中存在的所有样本的预测。你不明白为什么你的代码每次都打印相同的预测,是吗?就是这样,每次我尝试打印matplotlib图像中的数字时,它都打印相同的预测,但它会打印这个输出,因为您将X_测试参数传递给predict()。只需稍加更正<代码>序列测试分割
不要展平数字的形状。load\u digits().data
返回的数组已经是的数组(n\u samples,64)