[Day 36] Interpreting ML Model

2022. 3. 25. 20:36AI/Codestates

728x90
반응형

https://www.codestates.com/

 

코딩부트캠프 | 코드스테이츠 - 비전공생도 개발자가 될 수 있습니다

코딩부트캠프를 찾는다면? 개발자로 커리어 전환을 위한 책임있는 코딩 교육 기관! 서비스 기획자, 그로스 마케터, 데이터 사이언티스트 등 다양한 전문 커리어에 도전하세요. 취업 성공의 후기

www.codestates.com


Warm-up

  • PDP ( Partial Dependence Plot )

Note

  • 부분 의존도 그림 ( Partical Dependence Plots, PDP )
import sklearn
import xgboost
import shap
from sklearn.model_selection import train_test_split

shap.initjs();

df, target = shap.datasets.boston()
X_train,X_test,y_train,y_test = train_test_split(df, target, test_size=0.2, random_state=2)

model = xgboost.XGBRegressor().fit(X_train, y_train)

### Draw PDP plots ###
import matplotlib.pyplot as plt

plt.rcParams['figure.dpi'] = 144
from pdpbox.pdp import pdp_isolate, pdp_plot

feature = 'LSTAT'

isolated = pdp_isolate(
    model=model, 
    dataset=X_test, 
    model_features=X_test.columns, 
    feature=feature,
    grid_type='percentile', # default='percentile', or 'equal'
    num_grid_points=10 # default=10
)
pdp_plot(isolated, feature_name=feature)

  • ICE ( Individual Conditional Expectation ) Curves
  • SHAP
explainer = shap.TreeExplainer(model)
row = X_test.iloc[[1]]  
shap_values = explainer.shap_values(row)
### Draw SHAP plots ###
shap.initjs()
shap.force_plot(
    base_value=explainer.expected_value, 
    shap_values=shap_values,
    features=row
)
shap_values = explainer.shap_values(X_train)
shap.summary_plot(shap_values, X_train, plot_type='bar')

 


Review

더보기

오늘은 부분 의존도와 SHAP로 타겟과의 관계를 시각화 해보았다.


참고

728x90
반응형

'AI > Codestates' 카테고리의 다른 글

[Day38 ~ Day43] Section 2 Project  (0) 2022.03.25
[Day 37] Sprint Review  (0) 2022.03.25
[Day 35] Feature Importances  (0) 2022.03.15
[Day 34] Data Wrangling  (0) 2022.03.15
[Day 33] Choose your ML problems  (0) 2022.03.10