소품집

[python] SHAP (SHapley Additive exPlanations), 설명 가능한 인공지능 본문

AI

[python] SHAP (SHapley Additive exPlanations), 설명 가능한 인공지능

sodayeong 2023. 2. 9. 15:01

1. SHAP Feature Importance

- feature importance plot

- 상위 중요도 기준으로 내림차순 기본 정렬

import shap
shap_values = shap.TreeExplainer(model).shap_values(train_data)
shap.summary_plot(shap_values, train_data, plot_type='bar')

Model - xgboost

SHAP 에는 저장 기능이 없기 때문에 matplotlib로 저장해야함. 

import matplotlib.pyplot as plt
f = plf.figure()

shap.summary_plot(shap_values, X_test)
f.savefig('/경로/파일명.png', bbox_incehs='tight', dpi=600)

 

2. SHAP Summary Plot 

- feature importance + feature effects 

- 각 포인트는 기능 및 인스턴스에 대한 Shapley 값

- y축은 기능, x축은 Shapley 값에 의해 결정됨.

shap.summary_plot(shap_values, train_data)

2-2. SHAP Beeswarm plot

- 모델 출력에 어떤 영향을 미치는지를 보여주는 밀도를 표시해줌.

parameter

- max_display = n

- order = shap_values.abs.max(0) : SHAP 값의 최대 절댓값을 기준으로 정렬

 

변환

- shap_values.abs : 절대값을 plot, red로 고정

shap.plots.beeswarm(shap_values.abs, color='shap_red')

- shap_values.abs.mean(0) : bar plot이 beeswarm plot에 있는 점의 평균 값을 plot.

shap.plots.bar(shap_values.abs.mean(0))

 

3. SHAP Dependence scatter plot

- 각 독립변수가 종속변수에 미치는 영향을 확인하기 위해 scatter plot으로 확인할 수있음. 

# Dependence scatter plot
shap.plots.scatter(shap_values[:'features'], color = shap_values)

 

 

 

 

참조

https://github.com/slundberg/shap

 

GitHub - slundberg/shap: A game theoretic approach to explain the output of any machine learning model.

A game theoretic approach to explain the output of any machine learning model. - GitHub - slundberg/shap: A game theoretic approach to explain the output of any machine learning model.

github.com

https://shap.readthedocs.io/en/latest/example_notebooks/api_examples/plots/beeswarm.html

 

beeswarm plot — SHAP latest documentation

This notebook is designed to demonstrate (and so document) how to use the shap.plots.beeswarm function. It uses an XGBoost model trained on the classic UCI adult income dataset (which is a classification task to predict if people made over \$50k in the 199

shap.readthedocs.io

 

728x90
Comments