데이터 분석과 머신러닝 모델의 성능을 높이기 위해서는 가장 중요한 특징(Feature)을 선별하는 과정이 필수적입니다. 이번 스터디에서는 Permutation Importance, Random Forest, 그리고 Confusion Matrix를 활용하여 데이터에서 중요한 Feature를 찾는 방법을 학습합니다.
Permutation Importance는 특정 Feature를 무작위로 섞어서 모델의 성능 변화가 얼마나 일어나는지를 측정하는 기법입니다.
이 방법은 모델의 Feature 중요도를 해석하는 데 유용하며, 특히 Random Forest, Gradient Boosting과 같은 트리 기반 모델과 함께 사용됩니다.
다음은 sklearn.inspection.permutation_importance()를 사용하여 Feature 중요도를 측정하는 코드입니다.
python
복사편집
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.inspection import permutation_importance
# 샘플 데이터 생성
np.random.seed(0)
X = pd.DataFrame(np.random.randn(100, 5), columns=['X1', 'X2', 'X3', 'X4', 'X5'])
y = np.random.choice([0, 1], size=100) # 이진 분류 문제
# 데이터 분할
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Random Forest 모델 학습
model = RandomForestClassifier(n_estimators=100, random_state=0)
model.fit(X_train, y_train)
# Permutation Importance 계산
perm_importance = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=0)
# 결과 출력
feature_importance = pd.DataFrame({'Feature': X.columns, 'Importance': perm_importance.importances_mean})
feature_importance.sort_values(by='Importance', ascending=False)
Random Forest는 여러 개의 결정 트리(Decision Tree)를 조합하여 예측하는 앙상블 학습 기법입니다.
이 방법을 사용하면 모델이 어떤 Feature를 많이 사용했는지를 분석하여 Feature Selection에 활용할 수 있습니다.
python
복사편집
# Random Forest를 이용한 Feature Importance 측정
rf_importance = pd.DataFrame({'Feature': X.columns, 'Importance': model.feature_importances_})
rf_importance.sort_values(by='Importance', ascending=False)
Confusion Matrix는 분류 모델의 성능을 평가할 때 사용하는 지표로, 예측값과 실제값의 비교 결과를 표로 정리한 것입니다.
| True Class | Positive (실제) | Negative (실제) |
|---|---|---|
| Predicted Positive (예측) | TP (True Positive) | FP (False Positive) |
| Predicted Negative (예측) | FN (False Negative) | TN (True Negative) |
Confusion Matrix를 활용하면 Precision, Recall, F1-score 등의 성능 평가 지표를 계산할 수 있습니다.
python
복사편집
from sklearn.metrics import confusion_matrix, classification_report
# 예측값 생성
y_pred = model.predict(X_test)
# Confusion Matrix 출력
conf_matrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", conf_matrix)
# Classification Report 출력
print("\nClassification Report:\n", classification_report(y_test, y_pred))
| 기법 | 목적 | 결과 해석 |
|---|---|---|
| Permutation Importance | Feature 중요도 평가 | Feature를 섞어서 모델 성능이 얼마나 변하는지 측정 |
| Random Forest Feature Importance | 모델 기반 Feature 선택 | 모델이 중요하게 여기는 Feature 분석 |
| Confusion Matrix | 분류 모델 성능 평가 | Precision, Recall, F1-score 분석 |
👉 Permutation Importance와 Random Forest를 활용하여 데이터에서 중요한 Feature를 선별하고, Confusion Matrix로 모델 성능을 분석하는 것이 중요합니다.