SHAP Values
SHAP Values는 어떤 prediction에 각 feature들이 얼만큼의 영향을 끼쳤는지를 보여준다.
SHAP의 의미
Shapley additive explanations의 약자.
기본 원리
만약 어떤 모델 에 대해 어떤 입력 에 대해 의 prediction을 구하게 됬다고 하자. 그러면 우리는 기준값(= 의 기댓값) 로 구한 prediction 와 비교해서 얼만큼 차이가 나는지 알 수 있다.
만약 이러한 차이를 만드는데 이 얼마나 영향을 줬는지 알고 싶다면, feature를 제외한 나머지 feature 에 B또는 V의 원소를 채워넣는 각 조합의 경우마다 ( 일때의 prediction) - ( 일때의 prediction)을 구하고 그 결과들의 가중평균을 구하면 된다.
이렇게 구한 값이 입력이 V일때 이 가지는 SHAP 값이다. 그리고 V의 모든 원소의 SHAP 값들을 더하면 가 나오게 된다.
SHAP의 종류
SHAP는 그 구현 알고리즘에 따라 여러 종류가 있다. n개의 input Feature를 가지는 모델에서 SHAP 값을 구하면 한 feature에 대해서는 적어도 2^(n-1)개 모든 feature에 대해서는 중복을 제거하고 의 prediction을 필요로한다. 따라서 input feature의 수가 많은 경우에 대한 다른 알고리즘을 필요로 하게된다. 그 대표적인게 deep learning model에서 사용하는 deep explainer 그리고 모든 model에서 SHAP 근사값을 빠르게 구할 수 있는 kernel explainer이다.
구현
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shap | |
#Tree, Deep or kernel | |
explainer = shap.TreeExplainer(my_model) | |
shap_values = explainer.shap_values(single_record) | |
shap.initjs() | |
shap.force_plot(explainer.expected_value[0], shap_values[0], single_record) |
Summary Plots
전체 records에 대한 SHAP values를 구하고 각 feature의 값에 따른 SHAP값을 도표에 나타내면, Feature importance 와 SHAP values를 같이 나타낼 수 있다. 많은 점들이 양쪽 끝에 몰려있을 수록 feature importance가 크다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shap # package used to calculate Shap values | |
#tree or deep or kernel | |
explainer = shap.TreeExplainer(my_model) | |
shap_values = explainer.shap_values(val_X)#전체 record에 대한 shap 값들이 필요하다. | |
shap.summary_plot(shap_values[1], val_X)#shap_values[1]는 regression에서 possitive. |
댓글
댓글 쓰기