关于人工智能:可解释的AI-XAI如何使用LIME-和-SHAP更好地解释模型的预测

14次阅读

共计 3286 个字符,预计需要花费 9 分钟才能阅读完成。

作为数据科学家或机器学习从业者,将可解释性集成到机器学习模型中能够帮忙决策者和其余利益相关者有更多的可见性并能够让他们了解模型输入决策的解释。

在本文中,我将介绍两个能够帮忙理解模型的决策过程的模型 LIME 和 SHAP。

模型

咱们将应用来自 Kaggle 的糖尿病数据集。次要关注点是可解释性,因而咱们不会花太多工夫尝试领有花哨的模型。

# Load useful libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
`
# Read data set
df = pd.read_csv("./data/diabetes.csv")

# Separate Features and Target Variables
X = df.drop(columns='Outcome')
y = df['Outcome']

# Create Train & Test Data
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.3, 
                                                    stratify =y, 
                                                    random_state = 13)

# Build the model
rf_clf = RandomForestClassifier(max_features=2, n_estimators =100 ,bootstrap = True)

# Make prediction on the testing data
y_pred = rf_clf.predict(X_test)

# Classification Report 
print(classification_report(y_pred, y_test))
rf_clf.fit(X_train, y_train)

SHAP

它是 SHapley Additive exPlanations 的缩写。该办法旨在通过计算每个特色对预测的奉献来解释实例 / 察看的预测。

# Import the SHAP library
import shap

# load JS visualization code to notebook
shap.initjs()

# Create the explainer
explainer = TreeExplainer(rf_clf)

"""
Compute shap_values for all of X_test rather instead of 
a single row, to have more data for plot.
"""
shap_values = explainer.shap_values(X_test)
     
print("Variable Importance Plot - Global Interpretation")
figure = plt.figure()
shap.summary_plot(shap_values, X_test)

SHAP 有许多用于模型解释的可视化图表,但咱们将着重介绍其中的几个。

特色重要性的汇总图

print("Variable Importance Plot - Global Interpretation")
figure = plt.figure()
shap.summary_plot(shap_values, X_test)

咱们能够从下面的图中失去以下的论断:

  1. 它显示了重要特色的列表,从最重要到最不重要(从上到下)。
  2. 所有特色仿佛对诊断为糖尿病(标签 = 1)或未诊断(标签 = 0)的两个类别的奉献均等,因为基本上都占据了矩形的 50%。
  3. 依据该模型,Glucose(葡萄糖)是对预测奉献最大的特色。Age(年龄)是奉献第二大的特色
  4. Pregnancies(怀孕)是预测能力最强的第 5 个特色。

特定分类后果的汇总图

# Summary Plot Deep-Dive on Label 1
shap.summary_plot(shap_values[1], X_test)

对于分类问题,每个标签都有 SHAP 值。在咱们的例子中,咱们应用 1 (True) 的预测显示该类后果的汇总。该图的示意内容如下:

  • 特色的重要性和排序与汇总图一样,排名越上,重要性越高。
  • 图中每个点代表单个数据实例的特征值。
  • 色彩表明该特色是高值(红色)还是低值(蓝色)。
  • X 轴代表对预测输入的正或负奉献

当咱们将这些剖析利用于特色时,咱们失去以下论断:

对于葡萄糖:咱们看到大多数高值(红点)对预测输入有正奉献(在 X 轴上为正)。换句话说,如果单个数据实例的葡萄糖量很高,则其取得 1 后果(被诊断患有糖尿病)的机会会大大增加,而低量(蓝点)会升高(负 X 轴值)被诊断为糖尿病的概率。

对于年龄:对年龄进行雷同的剖析。年龄越高,数据实例(患者)最有可能被诊断出患有糖尿病。

另一方面,模型在波及未成年人时仿佛很凌乱,因为咱们能够在垂直线(X 轴 = 0)的每一侧察看到简直雷同数量的数据点。因为年龄特征对剖析来说仿佛令人困惑,咱们能够应用上面的相干图来取得更细粒度的信息。

相干图(依赖图)

# Dependence Plot on Age feature
shap.dependence_plot('Age', shap_values[1], X_test, interaction_index="Age")

从相干图中咱们能够分明地看到,30 岁以下的患者被诊断为糖尿病的危险较低,而 30 岁以上的患者被诊断为糖尿病的危险较高。

LIME

它是 Local Interpretable Model Agnostic Explanation 的缩写。部分(Local)意味着它能够用于解释机器学习模型的个别预测。

要应用它也十分的简略,只须要 2 个步骤:(1) 导入模块,(2) 应用训练值、特色和指标拟合解释器。

# Import the LimeTabularExplainer module
from lime.lime_tabular import LimeTabularExplainer

# Get the class names
class_names = ['Has diabetes', 'No diabetes']

# Get the feature names
feature_names = list(X_train.columns)

# Fit the Explainer on the training data set using the LimeTabularExplainer 
explainer = LimeTabularExplainer(X_train.values, feature_names = feature_names, 
                                 class_names = class_names, mode = 'classification')

代码中咱们应用 class_names 创立了两个标签,而不是 1 和 0 因为应用名字会更加的直观。

对单例进行解释阐明

这里的解释是针对测试数据中的单个实例进行的

#Perform the explanation on the 8th instance in the test data
explaination = explainer.explain_instance(X_test.iloc[8], rf_clf.predict_proba)

# show the result of the model's explaination
explaination.show_in_notebook(show_table = True, show_all = False)

该模型以 73% 的置信度预测该特定患者患有糖尿病,并解释该预测,因为血糖程度高于 99,血压高于 70。在右侧,咱们能够看到患者特色的值。

总结

本文中接单的介绍了如何应用 SHAP 和 LIME 解释您的机器学习模型。当初,你也能够对构建的模型进行可解释性剖析了,这能够帮忙决策者和其余利益相关者取得更多的可见性并了解导致模型输入的决策的解释。,你能够在上面的资源中找到本文蕴含的两个 python 包,浏览他们的文档能够找到更加高级的应用形式。

作者:Zoumana Keita

正文完
 0