시작하기무료로 시작하기

Gradient boosting의 특성 중요도

random forests와 마찬가지로, gradient boosting 모델에서도 특성 중요도를 추출해 어떤 특성이 가장 좋은 예측 변수인지 파악할 수 있어요. 때로는 서로 다른 트리 기반 모델을 시도해 보고, 각 모델의 특성 중요도를 비교하는 것이 좋아요. 이렇게 하면 특정 모델에서만 나타나는 특이성을 평균화하는 데 도움이 될 수 있어요.

특성 중요도는 gradient boosting 모델의 .feature_importances_ 속성에 numpy 배열로 저장돼 있어요. 보기 좋은 플롯을 만들려면 np.argsort()로 특성 중요도의 정렬된 인덱스를 구해야 해요. 우리는 큰 값에서 작은 값 순서로 보고 싶으므로, Python 인덱싱을 사용해 feat_importances[::-1]처럼 정렬된 중요도를 뒤집을 거예요.

이 연습은 강의의 일부입니다

Python으로 배우는 금융 분야 Machine Learning

강의 보기

연습 안내

  • Python 인덱싱을 사용해 sorted_index 변수를 뒤집어 큰 값에서 작은 값 순으로 바꾸세요.
  • feature_names를 numpy 배열로 변환한 뒤 sorted_index로 인덱싱하여, 정렬된 특성 레이블 리스트를 labels로 만드세요.
  • xticks와 sorted_index로 인덱싱한 feature_importances, 그리고 눈금 레이블로 labels를 사용해 막대 그래프를 만드세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Extract feature importances from the fitted gradient boosting model
feature_importances = gbr.feature_importances_

# Get the indices of the largest to smallest feature importances
sorted_index = np.argsort(feature_importances)[::____]
x = range(features.shape[1])

# Create tick labels 
labels = np.array(feature_names)[____]

plt.bar(____, feature_importances[____], tick_label=____)

# Set the tick lables to be the feature names, according to the sorted feature_idx
plt.xticks(rotation=90)
plt.show()
코드 편집 및 실행