나중에 내가 보려고 만든 블로그

[python] feature importances 본문

Python

[python] feature importances

winches 2021. 5. 18. 10:42

파이썬 scikit-learn에서 트리계열(decision tree, random forest 등) 알고리즘에는 feature_importances_ 기능이 있다.

분류에서 어떤 feature를 사용했고, 어느정도 중요도로 판단했는지 알 수 있다.

다만 그 중요도가 모든 알고리즘에서 적용되는건 아니고 적용된 분류방식에서 판단한 정도라는 점이다.

 

바로 출력하면 index순서대로 중요도만 뽑아줘서 어떤 특성의 중요도 인지 알기 어렵다.

반복문을 통해서 특성이름과 중요도가 함께 출력되도록 구현한 코드다.

1
2
3
4
5
6
7
8
list= []
 
for i in range (len(clf.feature_importances_)):
    list.append ([clf.feature_importances_[i], features[i]])
 
print ('feature_importance: ')
for importance_, feature_ in sorted(feature_importance, reverse = True):
    print ('%.4f \t {}'.format(feature_) % importance_)
cs