树模型的可解释性

Python 可解释性 AI

Fouad Trad

Machine Learning Engineer

决策树

  • 树模型的基本单元
  • 可用于回归和分类
  • 以树状结构进行预测
    • 多个决策
    • 每个决策基于一个特征
  • 天生可解释

一个决策树的示意图,展示如何基于多个条件进行预测

Python 可解释性 AI

随机森林

  • 由多棵决策树组成
  • 可用于回归和分类
  • 降低直接可解释性
  • 特征重要性
    • 衡量对预测不确定性的降低
    • 不同于线性模型的系数

一幅随机森林的图示:多棵树接收同一条样本,并聚合预测得到最终结果。

Python 可解释性 AI

录取数据集

GRE 分数 TOEFL 分数 大学评级 SOP LOR CGPA 录取
337 118 4 4.5 4.5 9.65 1
324 107 4 4 4.5 8.87 1
316 104 3 3 3.5 8.00 1
322 110 3 3.5 2.5 8.67 1
314 103 2 2 3 8.21 0
X_train = data.drop(['Accept'], axis=1)
y_train = data['Accept']
Python 可解释性 AI

模型训练

from sklearn.tree import DecisionTreeClassifier

tree_model = DecisionTreeClassifier() tree_model.fit(X_train, y_train)
print(tree_model.feature_importances_)
[0.17936982 0.08878744 0.04388924 
 0.0532897  0.07130751 0.56335628]
from sklearn.ensemble import RandomForestClassifier

forest_model = RandomForestClassifier() forest_model.fit(X_train, y_train)
print(forest_model.feature_importances_)
[0.25347149 0.17518662 0.06551317 
 0.06758647 0.07866478 0.35957747]
Python 可解释性 AI

特征重要性

import matplotlib.pyplot as plt

plt.barh(X_train.columns, 
         tree_model.feature_importances_)
plt.title('Feature Importance - Decision Tree')
plt.show()

一张水平条形图,显示 CGPA 和 GRE 分数是最重要的特征。

import matplotlib.pyplot as plt

plt.barh(X_train.columns, 
         forest_model.feature_importances_)
plt.title('Feature Importance - Random Forest')
plt.show()

一张水平条形图,显示 CGPA 和 GRE 分数是最重要的特征。

Python 可解释性 AI

开始练习!

Python 可解释性 AI

Preparing Video For Download...