Table of Contents
SHAP and SHAP values
Shap 内置图表
分类模型的SHAP values/图表
示例
Home Technology peripherals AI This article will take you to understand SHAP: model explanation for machine learning

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

Jun 01, 2024 am 10:58 AM
AI machine learning xai

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Bytedance Cutting launches SVIP super membership: 499 yuan for continuous annual subscription, providing a variety of AI functions Bytedance Cutting launches SVIP super membership: 499 yuan for continuous annual subscription, providing a variety of AI functions Jun 28, 2024 am 03:51 AM

This site reported on June 27 that Jianying is a video editing software developed by FaceMeng Technology, a subsidiary of ByteDance. It relies on the Douyin platform and basically produces short video content for users of the platform. It is compatible with iOS, Android, and Windows. , MacOS and other operating systems. Jianying officially announced the upgrade of its membership system and launched a new SVIP, which includes a variety of AI black technologies, such as intelligent translation, intelligent highlighting, intelligent packaging, digital human synthesis, etc. In terms of price, the monthly fee for clipping SVIP is 79 yuan, the annual fee is 599 yuan (note on this site: equivalent to 49.9 yuan per month), the continuous monthly subscription is 59 yuan per month, and the continuous annual subscription is 499 yuan per year (equivalent to 41.6 yuan per month) . In addition, the cut official also stated that in order to improve the user experience, those who have subscribed to the original VIP

Context-augmented AI coding assistant using Rag and Sem-Rag Context-augmented AI coding assistant using Rag and Sem-Rag Jun 10, 2024 am 11:08 AM

Improve developer productivity, efficiency, and accuracy by incorporating retrieval-enhanced generation and semantic memory into AI coding assistants. Translated from EnhancingAICodingAssistantswithContextUsingRAGandSEM-RAG, author JanakiramMSV. While basic AI programming assistants are naturally helpful, they often fail to provide the most relevant and correct code suggestions because they rely on a general understanding of the software language and the most common patterns of writing software. The code generated by these coding assistants is suitable for solving the problems they are responsible for solving, but often does not conform to the coding standards, conventions and styles of the individual teams. This often results in suggestions that need to be modified or refined in order for the code to be accepted into the application

Seven Cool GenAI & LLM Technical Interview Questions Seven Cool GenAI & LLM Technical Interview Questions Jun 07, 2024 am 10:06 AM

To learn more about AIGC, please visit: 51CTOAI.x Community https://www.51cto.com/aigc/Translator|Jingyan Reviewer|Chonglou is different from the traditional question bank that can be seen everywhere on the Internet. These questions It requires thinking outside the box. Large Language Models (LLMs) are increasingly important in the fields of data science, generative artificial intelligence (GenAI), and artificial intelligence. These complex algorithms enhance human skills and drive efficiency and innovation in many industries, becoming the key for companies to remain competitive. LLM has a wide range of applications. It can be used in fields such as natural language processing, text generation, speech recognition and recommendation systems. By learning from large amounts of data, LLM is able to generate text

Can fine-tuning really allow LLM to learn new things: introducing new knowledge may make the model produce more hallucinations Can fine-tuning really allow LLM to learn new things: introducing new knowledge may make the model produce more hallucinations Jun 11, 2024 pm 03:57 PM

Large Language Models (LLMs) are trained on huge text databases, where they acquire large amounts of real-world knowledge. This knowledge is embedded into their parameters and can then be used when needed. The knowledge of these models is "reified" at the end of training. At the end of pre-training, the model actually stops learning. Align or fine-tune the model to learn how to leverage this knowledge and respond more naturally to user questions. But sometimes model knowledge is not enough, and although the model can access external content through RAG, it is considered beneficial to adapt the model to new domains through fine-tuning. This fine-tuning is performed using input from human annotators or other LLM creations, where the model encounters additional real-world knowledge and integrates it

To provide a new scientific and complex question answering benchmark and evaluation system for large models, UNSW, Argonne, University of Chicago and other institutions jointly launched the SciQAG framework To provide a new scientific and complex question answering benchmark and evaluation system for large models, UNSW, Argonne, University of Chicago and other institutions jointly launched the SciQAG framework Jul 25, 2024 am 06:42 AM

Editor |ScienceAI Question Answering (QA) data set plays a vital role in promoting natural language processing (NLP) research. High-quality QA data sets can not only be used to fine-tune models, but also effectively evaluate the capabilities of large language models (LLM), especially the ability to understand and reason about scientific knowledge. Although there are currently many scientific QA data sets covering medicine, chemistry, biology and other fields, these data sets still have some shortcomings. First, the data form is relatively simple, most of which are multiple-choice questions. They are easy to evaluate, but limit the model's answer selection range and cannot fully test the model's ability to answer scientific questions. In contrast, open-ended Q&A

SOTA performance, Xiamen multi-modal protein-ligand affinity prediction AI method, combines molecular surface information for the first time SOTA performance, Xiamen multi-modal protein-ligand affinity prediction AI method, combines molecular surface information for the first time Jul 17, 2024 pm 06:37 PM

Editor | KX In the field of drug research and development, accurately and effectively predicting the binding affinity of proteins and ligands is crucial for drug screening and optimization. However, current studies do not take into account the important role of molecular surface information in protein-ligand interactions. Based on this, researchers from Xiamen University proposed a novel multi-modal feature extraction (MFE) framework, which for the first time combines information on protein surface, 3D structure and sequence, and uses a cross-attention mechanism to compare different modalities. feature alignment. Experimental results demonstrate that this method achieves state-of-the-art performance in predicting protein-ligand binding affinities. Furthermore, ablation studies demonstrate the effectiveness and necessity of protein surface information and multimodal feature alignment within this framework. Related research begins with "S

Five schools of machine learning you don't know about Five schools of machine learning you don't know about Jun 05, 2024 pm 08:51 PM

Machine learning is an important branch of artificial intelligence that gives computers the ability to learn from data and improve their capabilities without being explicitly programmed. Machine learning has a wide range of applications in various fields, from image recognition and natural language processing to recommendation systems and fraud detection, and it is changing the way we live. There are many different methods and theories in the field of machine learning, among which the five most influential methods are called the "Five Schools of Machine Learning". The five major schools are the symbolic school, the connectionist school, the evolutionary school, the Bayesian school and the analogy school. 1. Symbolism, also known as symbolism, emphasizes the use of symbols for logical reasoning and expression of knowledge. This school of thought believes that learning is a process of reverse deduction, through existing

A new era of VSCode front-end development: 12 highly recommended AI code assistants A new era of VSCode front-end development: 12 highly recommended AI code assistants Jun 11, 2024 pm 07:47 PM

In the world of front-end development, VSCode has become the tool of choice for countless developers with its powerful functions and rich plug-in ecosystem. In recent years, with the rapid development of artificial intelligence technology, AI code assistants on VSCode have sprung up, greatly improving developers' coding efficiency. AI code assistants on VSCode have sprung up like mushrooms after a rain, greatly improving developers' coding efficiency. It uses artificial intelligence technology to intelligently analyze code and provide precise code completion, automatic error correction, grammar checking and other functions, which greatly reduces developers' errors and tedious manual work during the coding process. Today, I will recommend 12 VSCode front-end development AI code assistants to help you in your programming journey.

See all articles