機器學習可解釋性實踐

kaggle教程:
https://www.kaggle.com/learn/machine-learning-explainability

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

%matplotlib inline
data = pd.read_csv('./train.csv')
print(data.shape)
data.columns

train.csv可在kaggle模型解釋化教程中下載。首先做分類模型的解釋實踐。

y = data.readmitted

base_features = [c for c in data.columns if c != "readmitted"]

X = data[base_features]


train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
my_model = RandomForestClassifier(n_estimators=30, random_state=1).fit(train_X, train_y)
import eli5
from eli5.sklearn import PermutationImportance

perm = PermutationImportance(my_model, random_state=1).fit(val_X, val_y)
eli5.show_weights(perm, top=100,feature_names = val_X.columns.tolist())

在這裏插入圖片描述

# ??perm
#獲取重要性列表
pd.concat([pd.Series(val_X.columns),pd.Series(perm.feature_importances_)],axis=1).sort_values(by=1,ascending=False)
from matplotlib import pyplot as plt
from pdpbox import pdp, get_dataset, info_plots

feature_name = 'number_inpatient'
# Create the data that we will plot
my_pdp = pdp.pdp_isolate(model=my_model, dataset=val_X, model_features=val_X.columns, feature=feature_name)

# plot it
pdp.pdp_plot(my_pdp, feature_name)
feature_name = 'time_in_hospital'
# Create the data that we will plot
my_pdp = pdp.pdp_isolate(model=my_model, dataset=val_X, model_features=val_X.columns, feature=feature_name)

# plot it
pdp.pdp_plot(my_pdp, feature_name)
features_to_plot = ['number_inpatient', 'time_in_hospital']
inter1  =  pdp.pdp_interact(model=my_model, dataset=val_X, model_features=val_X.columns, features=features_to_plot)

pdp.pdp_interact_plot(pdp_interact_out=inter1, feature_names=features_to_plot, plot_type='contour')
plt.show()
import shap  # package used to calculate Shap values

sample_data_for_prediction = val_X.iloc[0].astype(float)  # to test function

def patient_risk_factors(model, patient_data):
    # Create object that can calculate shap values
    explainer = shap.TreeExplainer(model)
    shap_values = explainer.shap_values(patient_data)
    shap.initjs()
#     explainer.expected_value[1], shap_values[1],取下標1是因爲模型爲分類模型,輸出兩個概率值,對應兩個shap_value值
    return shap.force_plot(explainer.expected_value[1], shap_values[1], patient_data)
explainer = shap.TreeExplainer(my_model)

# calculate shap values. This is what we will plot.
# Calculate shap_values for all of val_X rather than a single row, to have more data for plot.
shap_values = explainer.shap_values(val_X)

# Make plot. Index of [1] is explained in text below.
shap.summary_plot(shap_values[1], val_X)

迴歸模型實踐

import xgboost
import shap
shap.initjs()  # notebook環境下,加載用於可視化的JS代碼
X,y = shap.datasets.boston()
model = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
X.shape
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)  # 傳入特徵矩陣X,計算SHAP值
shap_values.shape
shap_values
# 可視化第一個prediction的解釋   如果不想用JS,傳入matplotlib=True
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])

在這裏插入圖片描述

# 基本值(base_value)是我們傳入數據集上模型預測值的均值,可以通過自己計算來驗證:
y_base = explainer.expected_value
print(y_base)

pred = model.predict(xgboost.DMatrix(X))
print(pred.mean())
#鼠標可以放圖上面顯示具體數值
shap.force_plot(explainer.expected_value, shap_values, X)

在這裏插入圖片描述

# summarize the effects of all the features
shap.summary_plot(shap_values, X)
#Feature Importance
# SHAP提供了另一種計算特徵重要性的思路。
# 取每個特徵的SHAP值的絕對值的平均值作爲該特徵的重要性,得到一個標準的條形圖(multi-class則生成堆疊的條形圖)
shap.summary_plot(shap_values, X, plot_type="bar")

在這裏插入圖片描述

# interaction value是將SHAP值推廣到更高階交互的一種方法。
# 實現了快速、精確的兩兩交互計算,這將爲每個預測返回一個矩陣,其中主要影響在對角線上,交互影響在對角線外。
# 這些數值往往揭示了有趣的隱藏關係(交互作用)
shap_interaction_values = explainer.shap_interaction_values(X)
shap.summary_plot(shap_interaction_values, X)
# dependence_plot
# 爲了理解單個feature如何影響模型的輸出,我們可以將該feature的SHAP值與數據集中所有樣本的feature值進行比較。
# 由於SHAP值表示一個feature對模型輸出中的變動量的貢獻,下面的圖表示隨着特徵RM變化的預測房價(output)的變化。
# 單一RM(特徵)值垂直方向上的色散表示與其他特徵的相互作用,爲了幫助揭示這些交互作用,“dependence_plot函數”
# 自動選擇另一個用於着色的feature。在這個案例中,RAD特徵着色強調了RM(每棟房屋的平均房間數)對RAD值較高地區的房價影響較小。
# create a SHAP dependence plot to show the effect of a single feature across the whole dataset
# interaction_index :“auto”, None, int, or string
shap.dependence_plot(ind="RM", shap_values=shap_values, features=X,interaction_index='RAD')

在這裏插入圖片描述

# 其他類型的explainers
# SHAP庫可用的explainers有:

# deep:用於計算深度學習模型,基於DeepLIFT算法
# gradient:用於深度學習模型,綜合了SHAP、集成梯度、和SmoothGrad等思想,形成單一期望值方程
# kernel:模型無關,適用於任何模型
# linear:適用於特徵獨立不相關的線性模型
# tree:適用於樹模型和基於樹模型的集成算法
# sampling :基於特徵獨立性假設,當你想使用的後臺數據集很大時,kenel的一個很好的替代方案
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章