2017-12-06 4 views
-1

필자는 72 개 기능을 가진 데이터 세트를위한 랜덤 포레스트 모델을 가지고있다 : 목표는 피쳐 importance를 찾아서 기능 선택을 위해 사용하는 것이다.랜덤 포레스트와 파이썬

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

내가 그들의 특징 값과 예측의 목록을 얻을 수 아니에요, 그냥 이 거기에 이름을 얻을 수있는 방법을 각 기능 이름으로 매핑하는 것은 매우 diffficult 72 개 기능의 중요성 번호를 제공 중요성과 함께

0.55656
B 0.4333

등처럼

+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 %가되는 것입니다.

중위 확인하고 가장 중요한 기능을 선택

,

# 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]) 

이 임계 값 설정에 따라 당신의 가장 중요한 기능 이름을 인쇄합니다.

관련 문제