模型解释器-LIME

模型解释性--LIME算法

简 介

图片

简单的模型例如线性回归,LR等模型非常易于解释,但在实际应用中的效果却远远低于复杂的梯度提升树模型以及神经网络等模型。现在大部分互联网公司的建模都是基于梯度提升树或者神经网络模型等复杂模型,遗憾的是,这些模型虽然效果好,但是我们却较难对其进行很好地解释,这也是目前一直困扰着大家的一个重要问题,现在大家也越来越加关注模型的解释性。

本文介绍一种解释机器学习模型输出的方法LIME。

LIME

图片

LIME(Local Interpretable Model-agnostic Explanations)支持的模型包括:

  • 结构化模型的解释;

  • 文本分类器的解释;

  • 图像分类器的解释;

LIME被用作解释机器学习模型的解释,通过LIME我们可以知道为什么模型会这样进行预测。

本文我们就重点观测一下LIME是如何对预测结果进行解释的。

代 码

图片

此处我们使用winequality-white数据集,并且将quality<=5设置为0,其它的值转变为1.

# !pip install lime
import pandas as pd
from xgboost import XGBClassifier
import shap
import numpy as np
from sklearn.model_selection import train_test_split
df = pd.read_csv('./data/winequality-white.csv',sep = ';')
df['quality'] = df['quality'].apply(lambda x: 0 if x <= 5 else 1)
df.head()
fixed acidityvolatile aciditycitric acidresidual sugarchloridesfree sulfur dioxidetotal sulfur dioxidedensitypHsulphatesalcoholquality
07.00.270.3620.70.04545.0170.01.00103.000.458.81
16.30.300.341.60.04914.0132.00.99403.300.499.51
28.10.280.406.90.05030.097.00.99513.260.4410.11
37.20.230.328.50.05847.0186.00.99563.190.409.91
47.20.230.328.50.05847.0186.00.99563.190.409.91
# 训练集测试集分割
X = df.drop('quality', axis=1)
y = df['quality'] 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
# 模型训练
model = XGBClassifier(n_estimators = 100, random_state=42)
model.fit(X_train, y_train)
score = model.score(X_test, y_test)
score
The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. 

0.832653061224489

对单个样本进行预测解释

下面的图中表明了单个样本的预测值中各个特征的贡献。

import lime
from lime import lime_tabular

explainer = lime_tabular.LimeTabularExplainer(
    training_data=np.array(X_train),
    feature_names=X_train.columns,
    class_names=['bad', 'good'],
    mode='classification'
)
  • 模型有84%的置信度是坏的wine,而其中alcohol,total sulfur dioxide是最重要的。

图片

exp = explainer.explain_instance(data_row=X_test.iloc[1], predict_fn=model.predict_proba)
exp.show_in_notebook(show_table=True)
  • 模型有59%的置信度是坏的wine,而其中alcohol,chlorides, density, citric acid是最重要的预测参考因素。

图片

exp = explainer.explain_instance(data_row=X_test.iloc[3], predict_fn=model.predict_proba)
exp.show_in_notebook(show_table=True)

适用问题

图片

LIME可以认为是SHARP的升级版,它通过预测结果解释机器学习模型很简单。它为我们提供了一个很好的方式来向非技术人员解释地下发生了什么。您不必担心数据可视化,因为LIME库会为您处理数据可视化。

参考文献

图片

  1. https://www.kaggle.com/piyushagni5/white-wine-quality

  2. LIME: How to Interpret Machine Learning Models With Python

  3. https://github.com/marcotcr/lime

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值