SHAP カーネル解釈器

Pythonで学ぶExplainable AI

Fouad Trad

Machine Learning Engineer

SHAP カーネル解釈器

 

  • 任意のモデルで SHAP 値を算出

    • k 近傍法
    • ニューラルネット
    • 木ベースモデル
  • タイプ別解釈器より遅い

SHAP 解釈器は、どのモデルにも使える汎用解釈器と、特定のモデル型に最適化されたタイプ別解釈器に分かれることを示す図。

Pythonで学ぶExplainable AI

心疾患

age sex chest_pain_type blood_pressure ecg_results thalassemia target
52 1 0 125 1 3 0
53 1 0 140 0 3 0
70 1 0 145 1 3 0
61 1 0 148 1 3 0
62 0 0 138 1 2 0

 

mlp_clf: 心疾患リスクを予測する多層パーセプトロン

Pythonで学ぶExplainable AI

保険料

age gender bmi children smoker charges
19 0 27.900 0 1 16884.92
18 1 33.770 1 0 1725.55
28 1 33.000 3 0 4449.46
33 1 22.705 0 0 21984.47
32 1 28.880 0 0 3866.85

 

mlp_reg: 保険料を予測する多層パーセプトロン

Pythonで学ぶExplainable AI

カーネル解釈器の作成

MLPRegressor
import shap


explainer = shap.KernelExplainer( # モデルの予測関数, # データセットの代表要約 )
MLPClassifier
import shap


explainer = shap.KernelExplainer( # モデルの予測関数, # データセットの代表要約 )
Pythonで学ぶExplainable AI

カーネル解釈器の作成

MLPRegressor
import shap

explainer = shap.KernelExplainer(
  mlp_reg.predict, 
  # データセットの代表要約
)


MLPClassifier
import shap

explainer = shap.KernelExplainer(
  mlp_clf.predict_proba, 
  # データセットの代表要約
)


Pythonで学ぶExplainable AI

カーネル解釈器の作成

MLPRegressor
import shap

explainer = shap.KernelExplainer(
  mlp_reg.predict, 
  shap.kmeans(X, 10)
)


shap_values_reg = explainer.shap_values(X)
MLPClassifier
import shap

explainer = shap.KernelExplainer(
  mlp_clf.predict_proba, 
  shap.kmeans(X, 10)
)


shap_values_cls = explainer.shap_values(X)
Pythonで学ぶExplainable AI

特徴量重要度

MLPRegressor
mean_reg = np.abs(shap_values_reg).mean(axis=0)

plt.bar(X.columns, mean_reg)

回帰タスクの特徴量重要度の棒グラフ。喫煙と年齢が保険料の予測に最も影響することを示す。

MLPClassifier
mean_cls = np.abs(shap_values_cls[:,:,1]).mean(axis=0)

plt.bar(X.columns, mean_cls)

分類タスクの特徴量重要度の棒グラフ。胸痛タイプとサラセミアが予測に最も影響することを示す。

Pythonで学ぶExplainable AI

モデル固有手法との比較

線形回帰
plt.bar(X.columns, np.abs(lin_reg.coef_))

線形回帰モデルでの特徴量重要度の棒グラフ。喫煙と年齢が保険料の予測に最も影響することを示す。

ロジスティック回帰
plt.bar(X.columns, np.abs(log_reg.coef_[0]))

分類タスクでの特徴量重要度の棒グラフ。胸痛タイプが予測に最も影響することを示す。

Pythonで学ぶExplainable AI

Ayo berlatih!

Pythonで学ぶExplainable AI

Preparing Video For Download...