Python 可解释性 AI
Fouad Trad
Machine Learning Engineer



| GRE 分数 | TOEFL 分数 | 大学评级 | SOP | LOR | CGPA | 录取概率 | 是否录取 |
|---|---|---|---|---|---|---|---|
| 337 | 118 | 4 | 4.5 | 4.5 | 9.65 | 0.92 | 1 |
| 324 | 107 | 4 | 4 | 4.5 | 8.87 | 0.76 | 1 |
| 316 | 104 | 3 | 3 | 3.5 | 8 | 0.72 | 1 |
| 322 | 110 | 3 | 3.5 | 2.5 | 8.67 | 0.8 | 1 |
| 314 | 103 | 2 | 2 | 3 | 8.21 | 0.45 | 0 |
rf_reg:预测录取概率rf_class:预测是否录取X:包含特征的数据框import shap
explainer_reg = shap.TreeExplainer(rf_reg)shap_values_reg = explainer_reg.shap_values(X)
explainer_class = shap.TreeExplainer(rf_class)shap_values_class = explainer_class.shap_values(X)
print(shap_values_reg.shape)
(400, 6)

print(shap_values_reg.shape)
(400, 6)

print(shap_values_class.shape)
(400, 6, 2)
选择正类的值
positive_values = shap_values_class[:,:,1]
mean_shap_values_class = np.abs(shap_values_class[:, :, 1]).mean(axis=0) mean_shap_values_reg = np.abs(shap_values_reg).mean(axis=0)plt.bar(X_train.columns, mean_shap_values_class) plt.bar(X_train.columns, mean_shap_values_reg)

Python 可解释性 AI