樹狀模型的可解釋性

Python 的 Explainable AI

Fouad Trad

Machine Learning Engineer

決策樹

  • 樹狀模型的基本組成
  • 可用於回歸與分類
  • 以類樹結構做預測
    • 多重決策
    • 每個決策依據一個特徵
  • 天生可解釋

決策樹示意圖,顯示如何依多個條件進行預測

Python 的 Explainable AI

隨機森林

  • 由多棵決策樹組成
  • 可用於回歸與分類
  • 增加直接詮釋的難度
  • 特徵重要性
    • 衡量對預測不確定性的降低
    • 不同於線性模型的係數

隨機森林示意:多棵樹接收同一筆資料,彙整各自預測得到最終結果。

Python 的 Explainable AI

入學申請資料集

GRE Score TOEFL Score University Rating SOP LOR CGPA Accept
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 的 Explainable 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 的 Explainable 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 的 Explainable AI

一起來練習吧!

Python 的 Explainable AI

Preparing Video For Download...