SHAP kernel explainer

Explainable AI ด้วย Python

Fouad Trad

Machine Learning Engineer

SHAP kernel explainer

 

  • คำนวณค่า SHAP สำหรับโมเดลทุกประเภท

    • K-nearest neighbors
    • Neural networks
    • โมเดลแบบ tree
  • ช้ากว่า explainer เฉพาะประเภท

ภาพแสดงการแบ่ง SHAP explainer เป็น general explainer ที่ใช้ได้กับทุกโมเดล และ type-specific explainer ที่ปรับให้เหมาะกับโมเดลเฉพาะประเภท

Explainable AI ด้วย Python

โรคหัวใจ

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: multilayer perceptron สำหรับพยากรณ์ความเสี่ยงโรคหัวใจ

Explainable AI ด้วย Python

ค่าเบี้ยประกัน

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: multilayer perceptron สำหรับพยากรณ์ค่าเบี้ยประกัน

Explainable AI ด้วย Python

การสร้าง kernel explainer

MLPRegressor
import shap


explainer = shap.KernelExplainer( # Model's prediction function, # Representative summary of dataset )
MLPClassifier
import shap


explainer = shap.KernelExplainer( # Model's prediction function, # Representative summary of dataset )
Explainable AI ด้วย Python

การสร้าง kernel explainer

MLPRegressor
import shap

explainer = shap.KernelExplainer(
  mlp_reg.predict, 
  # Representative summary of dataset
)


MLPClassifier
import shap

explainer = shap.KernelExplainer(
  mlp_clf.predict_proba, 
  # Representative summary of dataset
)


Explainable AI ด้วย Python

การสร้าง kernel explainer

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)
Explainable AI ด้วย Python

ความสำคัญของ feature

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

plt.bar(X.columns, mean_reg)

ภาพแสดง bar plot ความสำคัญของ feature ในงาน regression โดยพบว่าการสูบบุหรี่และอายุมีอิทธิพลสูงสุดต่อการพยากรณ์ค่าเบี้ยประกัน

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

plt.bar(X.columns, mean_cls)

ภาพแสดง bar plot ความสำคัญของ feature ในงาน classification โดยพบว่าประเภทอาการเจ็บหน้าอกและธาลัสซีเมียมีอิทธิพลสูงสุดต่อการพยากรณ์

Explainable AI ด้วย Python

เปรียบเทียบกับวิธีเฉพาะโมเดล

Linear regression
plt.bar(X.columns, np.abs(lin_reg.coef_))

ภาพแสดง bar plot ความสำคัญของ feature จากโมเดล linear regression โดยพบว่าการสูบบุหรี่และอายุมีอิทธิพลสูงสุดต่อการพยากรณ์ค่าเบี้ยประกัน

Logistic regression
plt.bar(X.columns, np.abs(log_reg.coef_[0]))

ภาพแสดง bar plot ความสำคัญของ feature ในงาน classification โดยพบว่าประเภทอาการเจ็บหน้าอกมีอิทธิพลสูงสุดต่อการพยากรณ์

Explainable AI ด้วย Python

มาฝึกกันเถอะ!

Explainable AI ด้วย Python

Preparing Video For Download...