728x90
[Feature Importance]
- 머신러닝 모델에서 각 특성(feature)이 예측 결과에 얼마나 영향을 미치는지를 평가하는 지표
- 어떤 특성이 중요한지를 확인할 수 있으며, 모델 해석과 특성 선택에 유용
- 여러 방법으로 Feature Importance를 계산할 수 있음
- 각 각의 방법은 특성 중요도를 계산하기 위해 다른 접근 방식을 사용하며, 실제로는 데이터와 모델에 맞게 적절한 방법을 선택하여 사용하면 됨
[1. MDI (Mean Decrease Impurity)]
- 의사결정나무 기반 모델에서 사용되는 방법
- 특성을 기준으로 분기할 때 각 특성이 얼마나 불순도를 감소시키는지를 측정
- 불순도의 감소가 클수록 해당 특성은 중요한 것으로 간주
from sklearn.ensemble import RandomForestRegressor
# 모델 훈련
model = RandomForestRegressor()
model.fit(X, y)
# MDI 계산
importance = model.feature_importances_
[2. Drop Column Importance]
- 특성을 하나씩 제거한 후 모델의 성능 감소 정도를 측정하는 방법
- 특성을 제거했을 때 성능 감소가 크면 해당 특성은 중요한 것으로 판단
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# 훈련 데이터와 검증 데이터 분할
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
# 모델 훈련
model = RandomForestClassifier()
model.fit(X_train, y_train)
# 원래 성능 측정
original_accuracy = accuracy_score(y_val, model.predict(X_val))
# 특성별로 성능 감소 측정
importance = []
for col in X.columns:
X_drop = X.drop(columns=[col])
X_train_drop, X_val_drop = train_test_split(X_drop, test_size=0.2)
model_drop = RandomForestClassifier()
model_drop.fit(X_train_drop, y_train)
drop_accuracy = accuracy_score(y_val, model_drop.predict(X_val_drop))
importance.append(original_accuracy - drop_accuracy)
[3. Permutation Importance]
- 특성의 순서를 무작위로 섞은 후 성능 변화를 측정하는 방법
- 해당 특성이 모델의 예측에 얼마나 영향을 미치는지를 나타냄
- 높은 성능 변화가 있을수록 해당 특성은 중요한 것으로 판단
import eli5
from eli5.sklearn import PermutationImportance
# 모델 훈련
model = RandomForestClassifier()
model.fit(X, y)
# Permutation Importance 계산
perm = PermutationImportance(model, random_state=42).fit(X, y)
# 결과 출력
eli5.show_weights(perm, feature_names=X.columns.tolist())
728x90
'STUDY > 인공지능' 카테고리의 다른 글
[딥러닝] ResNet이란? 예제 코드 tensorflow, pytorch (0) | 2023.07.17 |
---|---|
[딥러닝] RNN, LSTM 설명 (0) | 2023.07.17 |
[ML 책] 차원 축소, 차원의 저주 (0) | 2023.06.27 |
[ML 책] 특이값 분해 (0) | 2023.06.27 |
[ML 책] 고유값, 고유벡터, 고유값분해 (0) | 2023.06.22 |