Python sklearn决策树分类器具有多个功能?
我试图用四个特征对训练数据进行预测;我的代码:Python sklearn决策树分类器具有多个功能?,python,scikit-learn,decision-tree,Python,Scikit Learn,Decision Tree,我试图用四个特征对训练数据进行预测;我的代码: from sklearn.cross_validation import train_test_split X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42) # Train clf = DecisionTreeClassifier() clf.
from sklearn.cross_validation import train_test_split
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)
# Train
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
# Plot the decision boundary
plt.subplot(2, 3, pairidx + 1)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.xlabel(iris.feature_names[pair[0]])
plt.ylabel(iris.feature_names[pair[1]])
plt.axis("tight")
# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],
cmap=plt.cm.Paired)
plt.axis("tight")
plt.suptitle("Decision surface of a decision tree using paired features")
plt.legend()
plt.show()
在我的预测行:Z=clf.predict(np.c_xx.ravel(),yy.ravel())
我得到以下错误:
模型的特征数量必须与输入匹配。型号n_特征为4,输入n_特征为2
iris数据是一个150x4的数据集。我如何让这项功能适用于4个功能
- 在培训期间,您提供的功能数量为4
- 但是,当您进行预测时,您提供了一个样本,其中包含两个要预测的特征
- 训练中使用的特征数量在进行预测时,您必须使用相同数量的特征
- 如果您执行以下操作:
打印(np.c_uxx.ravel(),yy.ravel())
如果您的绘图步骤是1,它将为您提供一个形状:(30,2)
- 作为参数提供给predict函数的numpy数组必须具有以下形状:
(x,4)
,其中x可以是任何正整数,但numpy数组中的列数必须为4
为什么要使用ravel()。你在学习网络教程吗?发布链接。什么是plot\u-step
,pairidx
?