2017-12-06 11 views
-1

私は、72の機能を持つデータセットのランダムフォレストモデルを持っています。目的は、機能のインポートを見つけて機能の選択に使用することです。ランダムフォレストとPython

rf = RandomForestRegressor(n_estimators=XXX) 
rf.fit(X, y) 

私は彼らの特徴値と予測変数のリストを取得することはできませんよ、それだけで各機能名にマップすることは非常にdiffficultである、72個の機能の重要度番号を提供は、 は名前を取得する方法はありますそして、一緒に重要

0.4333 0.55656 B

同様

など

+0

この例は、あなたが望むものとまったく同じです:http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html –

答えて

0

feature_importance_メソッドは、ツリーが訓練された機能の順序を保持します。したがって、オリジナルのフィーチャリストとfeature_importance_戻り値の間にzip関数を使用して、各フィーチャの重要度を取得することができます。

1

for feature in zip(feature_labels, rf.feature_importances_): 
    print(feature) 

、次のように、機能の重要性を印刷することができ、あなたの機能がfeature_labelsと呼ばれるリストに割り当てられている

を仮定上のスコアは各変数の重要度スコアです。ここで覚えておくべきことは、すべての重要度スコアが100%になることです。

INORDERを特定し、最も重要な機能を選択する

# Create a selector object that will use the random forest classifier to identify 
# features that have an importance of more than 0.15 
sfm = SelectFromModel(rf, threshold=0.15) 

# Train the selector 
sfm.fit(X_train, y_train) 

'''SelectFromModel(estimator=RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini', 
      max_depth=None, max_features='auto', max_leaf_nodes=None, 
      min_impurity_split=1e-07, min_samples_leaf=1, 
      min_samples_split=2, min_weight_fraction_leaf=0.0, 
      n_estimators=10000, n_jobs=-1, oob_score=False, random_state=0, 
      verbose=0, warm_start=False), 
     prefit=False, threshold=0.15)''' 

# Print the names of the most important features 
for feature_list_index in sfm.get_support(indices=True): 
    print(feature_labels[feature_list_index]) 

これは、しきい値の設定に基づいて、あなたの最も重要な機能の名前を出力します。

関連する問題