2017-03-21 13 views
2

私は4つの機能を備えたトレーニングデータの予測を実行しようとしています。私のコード:私の予測ラインで複数の機能を備えたPython sklearnデシジョンツリークラシファイア?

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()])は、私は次のエラーを取得する:

Number of features of the model must match the input. Model n_features is 4 and input n_features is 2

アイリスデータは、150x4のデータセットです。どのようにこれを4つの機能で動作させるのですか?あなたが提供する機能の数を訓練中に

+0

なぜあなたは(ラヴェルを使用していることがあります。引数は形状のものである必要がありますよう、あなたは予測関数に提供で)。ウェブチュートリアルに従っていますか?リンクを投稿してください。 'plot_step'、' pairidx'とは何ですか? –

答えて

0
  • は4
  • あるしかし、あなたは
  • あなたが持って訓練に使用される機能の数を予測する2機能を備えたサンプルを提供している予測しているときあなたのplot_stepは1
  • numpyの配列番目であれば(30、2):あなたは次の手順を実行した場合は、予測
  • を作っている時に機能の同じ番号を使用する:print(np.c_[xx.ravel(), yy.ravel()])それはあなたの形状を与えますxは任意の正の整数を指定でき(x, 4)が、あなたのnumpyの配列内の列の数は必ずしも
関連する問題