2017-05-02 18 views
0

私は9入力が が から例を使用される機能のために隔離森を実装しようとしていますhttp://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-pyValueError:モデルのフィーチャの数が入力と一致する必要があります。モデルn_feature>入力nfeature

私の電車とテストセットは、したがって、私は同じ形状サイズ

のXtrianとXTESTを作成しています9つの機能を備えています
X.shape 
(100, 9) 
>> X_train.shape 
(200, 9) 

マイコード:

print(__doc__) 

import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.ensemble import IsolationForest 

rng = np.random.RandomState(42) 

# Generate train data 
X = 0.3 * rng.randn(100, 9) 
X_train = np.r_[X + 2, X - 2] 
# Generate some regular novel observations 
X = 0.3 * rng.randn(20, 9) 
X_test = np.r_[X + 2, X - 2] 
# Generate some abnormal novel observations 
X_outliers = rng.uniform(low=-4, high=4, size=(20, 9)) 

# fit the model 
clf = IsolationForest(max_samples=100, random_state=rng) 
clf.fit(X_train) 
y_pred_train = clf.predict(X_train) 
y_pred_test = clf.predict(X_test) 
y_pred_outliers = clf.predict(X_outliers) 

# plot the line, the samples, and the nearest vectors to the plane 
xx, yy = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50)) 
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) 
Z = Z.reshape(xx.shape) 

plt.title("IsolationForest") 
plt.contourf(xx, yy, Z, cmap=plt.cm.Blues_r) 

b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white') 
b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='green') 
c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='red') 
plt.axis('tight') 
plt.xlim((-5, 5)) 
plt.ylim((-5, 5)) 
plt.legend([b1, b2, c], 
      ["training observations", 
      "new regular observations", "new abnormal observations"], 
      loc="upper left") 
plt.show() 

しかし、私はエラー

を取得しています
--------------------------------------------------------------------------- 

ValueError: Number of features of the model must match the input. Model n_features is 9 and input n_features is 2 

私の場合は私のエラーが表示さ:モデルn_featuresは9と入力n_featuresである私がここで行方不明です何に2つの

どれ入力です:あなたは9の機能を備えたモデルに適合したにも関わらず、

答えて

1

# plot the line, the samples, and the nearest vectors to the plane 
xx, yy = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50)) 
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) 

np.c_()配列の形状を見にに渡される:あなたはオフ作業している例の場合と同様に、コードのプロット部分はまだ、たった2つの次元を推定されます:

np.c_[xx.ravel(), yy.ravel()].shape 
(2500, 2) 

clfは9-D入力を期待していますが、唯一の2次元配列を提供しているので、あなたがエラーを取得しています。

分類器自体は、問題なくアクセスできます。たとえば、decision_function()メソッドとpredict()メソッドを使用することはできますが、オフにしているコードを使用して9次元すべてをプロットすることはできません.2次元でプロットするように設計されています。 9次元のnp.meshgrid()を実行してもほぼ確実にMemoryErrorが投げられます。詳細はthis discussionを参照してください。

9-D空間をプロットしようとしても、ここではあまり役に立ちません。代わりにROC curvesのようなクラシファイアの強さのより標準的な視覚的表現に焦点を当てることもできます。また、古い形式のconfusion matrixを使用することもできます。

+0

洞察力ありがとう!私はy_pred_test = clf.predict(X_test)のラベル予測結果を期待していた y_pred_outliers = clf.predict(X_outliers)しかし、それは私に何か結果を与えてくれませんでした..そこに何かがないか、混乱を生むことができますか?そこからの行列? –

+0

'y_pred_outliers = clf.predict(X_outliers)'行までコードを実行しようとすると、出力が得られるはずです。 (私は、エラーの原因となった部分を除いてあなたのコードを実行していましたが、その行まで働いていました) 'confusion_matrix()'で 'y_pred_outliers'を' y_pred'として、長さ 'y_pred_outliers '' y_true'で '-1'で構成されています。しかし、この例では、非常に明確な異常/異常値を示すように設計されていることに注意してください。 'X_outliers'を入力として' predict() 'から100%真陽性を得る可能性が高いです。 –

関連する問題