2017-05-29 22 views
0

Isolation Forest sklearn implementationを使用して357個の機能を持つデータセットを訓練しようとしています。 max features変数が1.0(デフォルト値)に設定されていると、私はうまくトレーニングして結果を得ることができます。sklearnを使用したIsolation Forestアルゴリズムでmax featuresパラメータを設定する際にエラーが発生しました

は、しかし、最大の特徴ときは2に設定され、それが次のエラーを与える:

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

enter image description here

機能のカウントが1(int型)とない1.0であるとき、それはまた同じエラーになります(浮く)。

私が理解したことは、フィーチャ数が2(int)の場合、各ツリーを作成する際に2つの機能を考慮する必要があるということでした。これは間違っていますか? max featuresパラメータを変更するにはどうすればよいですか?それは述べドキュメントで

from sklearn.ensemble.iforest import IsolationForest 

def isolation_forest_imp(dataset): 

    estimators = 10 
    samples = 100 
    features = 2 
    contamination = 0.1 
    bootstrap = False 
    random_state = None 
    verbosity = 0 

    estimator = IsolationForest(n_estimators=estimators, max_samples=samples, contamination=contamination, 
            max_features=features, 
            bootstrap=boostrap, random_state=random_state, verbose=verbosity) 

    model = estimator.fit(dataset) 
+1

scikitバージョン0.18以下の問題があります。 [問題はこちら](https://github.com/scikit-learn/scikit-learn/issues/5732)を参照してください。あなたのscikit-versionを0.20 –

+0

に更新してくださいThanks @ VivekKumar、それは問題であるようです。 – Fleur

答えて

0

::次のように

コードがある

max_features : int or float, optional (default=1.0) 
    The number of features to draw from X to train each base estimator. 

     - If int, then draw `max_features` features. 
     - If float, then draw `max_features * X.shape[1]` features. 

だから、2は二つの特徴を取る意味すべきであると1.0は機能のすべてを取る意味する必要があり、0.5テイク私が理解しているところから半分のように。

私はIsolationForestのフィット感で表情を取って、以来これは、バグの可能性を考える:

# Isolation Forest inherits from BaseBagging 
# and when _fit is called, BaseBagging takes care of the features correctly 
super(IsolationForest, self)._fit(X, y, max_samples, 
              max_depth=max_depth, 
              sample_weight=sample_weight) 
# however, when after _fit the decision_function is called using X - the whole sample - not taking into account the max_features 
self.threshold_ = -sp.stats.scoreatpercentile(
      -self.decision_function(X), 100. * (1. - self.contamination)) 

その後:

# when the decision function _validate_X_predict is called, with X unmodified, 
    # it calls the base estimator's (dt) _validate_X_predict with the whole X 
    X = self.estimators_[0]._validate_X_predict(X, check_input=True) 

    ... 

    # from tree.py: 
    def _validate_X_predict(self, X, check_input): 
     """Validate X whenever one tries to predict, apply, predict_proba""" 
     if self.tree_ is None: 
      raise NotFittedError("Estimator not fitted, " 
           "call `fit` before exploiting the model.") 

     if check_input: 
      X = check_array(X, dtype=DTYPE, accept_sparse="csr") 
      if issparse(X) and (X.indices.dtype != np.intc or 
           X.indptr.dtype != np.intc): 
       raise ValueError("No support for np.int64 index based " 
           "sparse matrices") 
     # so, this check fails because X is the original X, not with the max_features applied 
     n_features = X.shape[1] 
     if self.n_features_ != n_features: 
      raise ValueError("Number of features of the model must " 
          "match the input. Model n_features is %s and " 
          "input n_features is %s " 
          % (self.n_features_, n_features)) 

     return X 

だから、私はあなたがこれを処理する方法にはわかりません。たぶん、あなたが必要とする2つの機能につながるパーセンテージを理解することができます。たとえそれが期待どおりに機能するかわからないとしても。

注:私は編集 scikit-学ぶv.0.18に

を使用しています:@Vivekクマーが、これは0.20に問題とアップグレードは、トリックを行う必要があるコメントとして。

関連する問題