Home > Technology peripherals > AI > body text

This article will take you to understand SHAP: model explanation for machine learning

WBOY
Release: 2024-06-01 10:58:13
Original
642 people have browsed it

In the fields of machine learning and data science, the interpretability of models has always been a focus of researchers and practitioners. With the widespread application of complex models such as deep learning and ensemble methods, understanding the model's decision-making process has become particularly important. Explainable AI | XAI helps build trust and confidence in machine learning models by increasing model transparency. Improving model transparency can be achieved through methods such as the widespread use of multiple complex models, as well as the decision-making processes used to explain the models. These methods include feature importance analysis, model prediction interval estimation, local interpretability algorithms, etc. Feature importance analysis can explain the model's decision-making process by evaluating the degree of influence of the model on the input features. Model prediction interval estimates can provide deterministic information about model predictions. Local interpretability algorithms can help

XAI is a set of tools and frameworks for understanding and explaining how machine learning models make decisions. Among them, the SHAP (SHapley Additive explanations) library in Python is a very useful tool. The SHAP library quantifies the contribution of features to individual predictions and overall predictions, and provides beautiful and easy-to-use visualizations.

Next, we will outline the basics of the SHAP library to understand predictions for regression and classification models built in Scikit-learn.

This article will take you to understand SHAP: model explanation for machine learning

SHAP and SHAP values

SHAP (Shapley Additive Explanations) is a game theory method for interpreting the output of any machine learning model. It leverages the classic game theory game value and its related extensions to combine optimal credit allocation with local interpretation (see related paper for details and citations: https://github.com/shap/shap#citations). SHAP provides optimal credit allocation and local explanation by calculating the contribution of each feature to the model output. This approach can be applied to various model types, including linear models, tree models, deep learning models, etc. The goal of SHAP is to provide an intuitive and interpretable way to help people understand the decision-making process of the machine learning model and the impact of each feature on the prediction results. By using SHAP values ​​and related extensions, we can obtain a more accurate and comprehensive interpretation of feature importance, and pre-

SHAP+ values ​​for the model can help us quantify the contribution of features to predictions. The closer the SHAP value is to zero, the smaller the contribution of the feature to prediction; the further the SHAP value is from zero, the greater the contribution of the feature to prediction. In addition, the SHAP value can also tell us the contribution of features to prediction. When the SHAP value is close to zero, it means that the feature contributes little to prediction; and when the SHAP value is far from zero,

Install the shap package:

pip install shap-i https://pypi.tuna.tsinghua.edu.cn/simple
Copy after login

us See the example below: How to get the SHAP value of a feature in a regression problem. We'll start by loading the library and sample data, then quickly build a model to predict diabetes progression:

import numpy as npnp.set_printoptions(formatter={'float':lambda x:"{:.4f}".format(x)})import pandas as pdpd.options.display.float_format = "{:.3f}".formatimport seaborn as snsimport matplotlib.pyplot as pltsns.set(style='darkgrid', context='talk', palette='rainbow')from sklearn.datasets import load_diabetesfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import (RandomForestRegressor, RandomForestClassifier)import shapshap.initjs()# Import sample datadiabetes = load_diabetes(as_frame=True)X = diabetes['data'].iloc[:, :4] # Select first 4 columnsy = diabetes['target']# Partition dataX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)print(f"Training features shape: {X_train.shape}")print(f"Training target shape: {y_train.shape}\n")print(f"Test features shape: {X_test.shape}")print(f"Test target shape: {y_test.shape}")display(X_train.head())# Train a simple modelmodel = RandomForestRegressor(random_state=42)model.fit(X_train, y_train)
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

A common GET SHAP The value method is to use the Explainer object. Next create an Explainer object and extract the shap_test value for the test data:

explainer = shap.Explainer(model)shap_test = explainer(X_test)print(f"Shap values length: {len(shap_test)}\n")print(f"Sample shap value:\n{shap_test[0]}")
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

The length of shap_test is 89 because it contains each test Records of instances. From looking at the first test record, we can see that it contains three attributes:

shap_test[0].base_values: The base value of the target

shap_test[0].data: Each Values ​​of each feature

shap_test[0].values: SHAP value of each object

  • Base value: Base value (shap_test.base_values), also called expected value (explainer. expected_value), is the average of the target values ​​in the training data.
print(f"Expected value: {explainer.expected_value[0]:.1f}")print(f"Average target value (training data): {y_train.mean():.1f}")print(f"Base value: {np.unique(shap_test.base_values)[0]:.1f}")
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

  • shap_test.data contains the same value as X_test
(shap_test.data == X_test).describe()
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

  • values: The most important attribute of shap_test is the values ​​attribute because we can access the SHAP values ​​through it. Let's convert the SHAP value to a DataFrame for easier manipulation:
shap_df = pd.DataFrame(shap_test.values,  columns=shap_test.feature_names,  index=X_test.index)shap_df
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

可以看到每条记录中每个特征的 SHAP 值。如果将这些 SHAP 值加到期望值上,就会得到预测值:

This article will take you to understand SHAP: model explanation for machine learning

np.isclose(model.predict(X_test),  explainer.expected_value[0] + shap_df.sum(axis=1))
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

现在我们已经有了 SHAP 值,可以进行自定义可视化,如下图所示,以理解特征的贡献:

columns = shap_df.apply(np.abs).mean()\ .sort_values(ascending=False).indexfig, ax = plt.subplots(1, 2, figsize=(11,4))sns.barplot(data=shap_df[columns].apply(np.abs), orient='h', ax=ax[0])ax[0].set_title("Mean absolute shap value")sns.boxplot(data=shap_df[columns], orient='h', ax=ax[1])ax[1].set_title("Distribution of shap values");plt.show()
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

左侧子图显示了每个特征的平均绝对 SHAP 值,而右侧子图显示了各特征的 SHAP 值分布。从这些图中可以看出,bmi 在所使用的4个特征中贡献最大。

Shap 内置图表

虽然我们可以使用 SHAP 值构建自己的可视化图表,但 shap 包提供了内置的华丽可视化图表。在本节中,我们将熟悉其中几种选择的可视化图表。我们将查看两种主要类型的图表:

  • 全局:可视化特征的整体贡献。这种类型的图表显示了特征在整个数据集上的汇总贡献。
  • 局部:显示特定实例中特征贡献的图表。这有助于我们深入了解单个预测。
  • 条形图/全局:对于之前显示的左侧子图,有一个等效的内置函数,只需几个按键即可调用:
shap.plots.bar(shap_test)
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

这个简单但有用的图表显示了特征贡献的强度。该图基于特征的平均绝对 SHAP 值而生成:shap_df.apply(np.abs).mean()。特征按照从上到下的顺序排列,具有最高平均绝对 SHAP 值的特征显示在顶部。

  • 总结图/全局:另一个有用的图是总结图:
shap.summary_plot(shap_test)
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

以下是解释这张图的指南:

  • 图的横轴显示了特征的 SHAP 值分布。每个点代表数据集中的一个记录。例如,我们可以看到对于 BMI 特征,点的分布相当散乱,几乎没有点位于 0 附近,而对于年龄特征,点更加集中地分布在 0 附近。
  • 点的颜色显示了特征值。这个额外的维度允许我们看到随着特征值的变化,SHAP 值如何变化。换句话说,我们可以看到关系的方向。例如,我们可以看到当 BMI 较高时(由热粉色点表示)SHAP 值倾向于较高,并且当 BMI 较低时(由蓝色点表示)SHAP 值倾向于较低。还有一些紫色点散布在整个光谱中。

  • 热力图/全局:热力图是另一种可视化 SHAP 值的方式。与将 SHAP 值聚合到平均值不同,我们看到以颜色编码的个体值。特征绘制在 y 轴上,记录绘制在 x 轴上:
shap.plots.heatmap(shap_test)
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

这个热力图的顶部还补充了每个记录的预测值(即 f(x))的线图。

  • Force plot/全局:这个交互式图表允许我们通过记录查看 SHAP 值的构成。
shap.initjs()shap.force_plot(explainer.expected_value, shap_test.values, X_test)
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

就像热力图一样,x 轴显示每个记录。正的 SHAP 值显示为红色,负的 SHAP 值显示为蓝色。例如,由于第一个记录的红色贡献比蓝色贡献多,因此该记录的预测值将高于期望值。

交互性允许我们改变两个轴。例如,y 轴显示预测值 f(x),x 轴根据输出(预测)值排序,如上面的快照所示。

  • 条形图/局部:现在我们将看一下用于理解个别案例预测的图表。让我们从一个条形图开始:
shap.plots.bar(shap_test[0])
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

与“ 条形图/全局 ”中完全相同,只是这次我们将数据切片为单个记录。

  1. Force plot/局部:Force plot是单个记录的强制图。
shap.initjs()shap.plots.force(shap_test[0])
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

分类模型的SHAP values/图表

上面示例是回归模型,下面我们以分类模型展示SHAP values及可视化:

import numpy as npnp.set_printoptions(formatter={'float':lambda x:"{:.4f}".format(x)})import pandas as pdpd.options.display.float_format = "{:.3f}".formatimport seaborn as snsimport matplotlib.pyplot as pltsns.set(style='darkgrid', context='talk', palette='rainbow')from sklearn.datasets import load_diabetesfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestClassifierimport shapfrom sklearn.datasets import fetch_openml# 加载 Titanic 数据集titanic = fetch_openml('titanic', version=1, as_frame=True)df = titanic.frame# 选择特征和目标变量features = ['pclass', 'age', 'sibsp', 'parch', 'fare']df = df.dropna(subset=features + ['survived'])# 删除包含缺失值的行X = df[features]y = df['survived']# 分割数据集X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# 训练随机森林分类器model = RandomForestClassifier(n_estimators=100, random_state=42)model.fit(X_train, y_train)
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

和回归模型一样的,shap values 值也是包括base_values 和values 值:

explainer = shap.Explainer(model)shap_test = explainer(X_test)print(f"Length of shap_test: {len(shap_test)}\n")print(f"Sample shap_test:\n{shap_test[0]}")print(f"Expected value: {explainer.expected_value[1]:.2f}")print(f"Average target value (training data): {y_train}")print(f"Base value: {np.unique(shap_test.base_values)[0]:.2f}")shap_df = pd.DataFrame(shap_test.values[:,:,1],  columns=shap_test.feature_names,  index=X_test.index)shap_df
Copy after login

我们仔细检查一下将 shap 值之和添加到预期概率是否会给出预测概率:

np.isclose(model.predict_proba(X_test)[:,1],  explainer.expected_value[1] + shap_df.sum(axis=1))
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

内置图与回归模型是一致的,比如:

shap.plots.bar(shap_test[:,:,1])
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

或者瀑布图如下:

shap.plots.waterfall(shap_test[:,:,1][0])
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

示例

看一个具体的用例。我们将找出模型对幸存者预测最不准确的例子,并尝试理解模型为什么会做出错误的预测:

test = pd.concat([X_test, y_test], axis=1)test['probability'] = model.predict_proba(X_test)[:,1]test['order'] = np.arange(len(test))test.query("survived=='1'").nsmallest(5, 'probability')
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

生存概率为第一个记录的746。让我们看看各个特征是如何对这一预测结果产生贡献的:

ind1 = test.query("survived=='1'")\ .nsmallest(1, 'probability')['order'].values[0]shap.plots.waterfall(shap_test[:,:,1][ind1])
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

主要是客舱等级和年龄拉低了预测值。让我们在训练数据中找到类似的例子:

pd.concat([X_train, y_train], axis=1)[(X_train['pclass']==3) & (X_train['age']==29) & (X_train['fare'].between(7,8))]
Copy after login

This article will take you to understand SHAP: model explanation for machine learning

所有类似的训练实例实际上都没有幸存。现在,这就说得通了!这是一个小的分析示例,展示了 SHAP 如何有助于揭示模型为何会做出错误预测。

在机器学习和数据科学中,模型的可解释性一直备受关注。可解释人工智能(XAI)通过提高模型透明度,增强对模型的信任。SHAP库是一个重要工具,通过量化特征对预测的贡献,提供可视化功能。本文介绍了SHAP库的基础知识,以及如何使用它来理解回归和分类模型的预测。通过具体用例,展示了SHAP如何帮助解释模型错误预测。

The above is the detailed content of This article will take you to understand SHAP: model explanation for machine learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!