ValueError:模型的特征数必须与输入匹配决策树

问题描述

enter image description here

enter image description here

嗨,有什么办法可以修复值错误并绘制正确的图表吗?

这是我的代码

from sklearn import tree
dt_clf = tree.DecisionTreeClassifier()
dt_clf.fit(X_train,y_train)
print(dt_clf.score(X_test,y_test))

X_train.shape

y_train.shape

X_test.shape

y_test.shape


dt_clf.predict(np.array([6,3,5.5,1]).reshape(1,4))

import matplotlib.pyplot as plt
fig,ax = plt.subplots(dpi=500)
tree.plot_tree(dt_clf,fontsize=3) 
plt.show()

解决方法

正如错误所说,您已经创建了一个具有 784 个特征的模型。所以你需要在使用 predict

时传递相同数量的特征
dt_clf.predict(np.array([6,3,5.5,1]).reshape(1,4))

这里的 NumPy 数组应该是 (1,784)

或者你可以调整fit,只使用4个特征,所以X_train将是(60000,4)