카테고리 없음

7주차 TIL - 예측모델 성능 평가

게임취업하고싶은 사람 2025. 2. 10. 20:49

# 필요한 함수 다시 정의
def evaluate_model(y_test, y_pred, model_name):
    r2 = r2_score(y_test, y_pred)
    mae = mean_absolute_error(y_test, y_pred)
    rmse = mean_squared_error(y_test, y_pred, squared=False)
    return {'Model': model_name, 'R² Score': r2, 'MAE': mae, 'RMSE': rmse}

# Random Forest 모델 학습 (다시 정의 후 실행)
rf = RandomForestRegressor(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
y_pred_rf = rf.predict(X_test)

# Random Forest 모델 성능 평가
rf_results = evaluate_model(y_test, y_pred_rf, "Random Forest")
rf_results_df = pd.DataFrame([rf_results])

# Random Forest 특성 중요도 분석
feature_importance_rf = pd.DataFrame({
    "Feature": features,
    "Importance": rf.feature_importances_
}).sort_values(by="Importance", ascending=False)

# 특성 중요도 시각화
plt.figure(figsize=(8, 5))
sns.barplot(x="Importance", y="Feature", data=feature_importance_rf, palette="viridis")
plt.title("Random Forest Feature Importance (Target: outtrn)")
plt.xlabel("중요도")
plt.ylabel("변수")
plt.show()

import ace_tools as tools
tools.display_dataframe_to_user(name="Random Forest 모델 성능 평가 결과", dataframe=rf_results_df)