python机器学习Github已达8.9Kstars模型解释器LIME

作者:Python学习与数据挖掘 时间:2022-06-21 13:38:28 

简单的模型例如线性回归,LR等模型非常易于解释,但在实际应用中的效果却远远低于复杂的梯度提升树模型以及神经网络等模型。

现在大部分互联网公司的建模都是基于梯度提升树或者神经网络模型等复杂模型,遗憾的是,这些模型虽然效果好,但是我们却较难对其进行很好地解释,这也是目前一直困扰着大家的一个重要问题,现在大家也越来越加关注模型的解释性。

本文介绍一种解释机器学习模型输出的方 * IME。它可以认为是SHARP的升级版,Github链接:https://github.com/marcotcr/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()

python机器学习Github已达8.9Kstars模型解释器LIME


# 训练集测试集分割
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,totals ulfur dioxide是最重要的。

python机器学习Github已达8.9Kstars模型解释器LIME


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'
)

模型有59%的置信度是坏的wine,而其中alcohol,chlorides, density, citric acid是最重要的预测参考因素。

python机器学习Github已达8.9Kstars模型解释器LIME


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

适用问题

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

参考链接

https://www.kaggle.com/piyushagni5/white-wine-quality
LIME: How to Interpret Machine Learning Models With Python
https://github.com/marcotcr/lime
https://mp.weixin.qq.com/s/47omhEeHqJdQTtciLIN2Hw

来源:https://blog.csdn.net/weixin_38037405/article/details/118313872

标签:python,LIME,机器学习,模型解释器
0
投稿

猜你喜欢

  • Tensorflow 多线程设置方式

    2021-09-29 21:53:50
  • Python 进程操作之进程间通过队列共享数据,队列Queue简单示例

    2023-06-01 12:17:58
  • 无图片CSS圆角的五个实例

    2008-08-02 12:18:00
  • CSS实现HTML元素透明的那些事

    2010-02-01 12:34:00
  • phpstudy apache开启ssi使用详解

    2023-05-25 08:04:44
  • Python深入06——python的内存管理详解

    2021-07-29 05:18:26
  • 使用Python遍历文件夹实现查找指定文件夹

    2021-01-19 09:23:06
  • 如何让WML页面自己更新?

    2008-05-21 13:35:00
  • 各种页面定时跳转(倒计时跳转)代码总结

    2023-09-05 00:12:01
  • 带你深入了解MySQL语句优化的基本原则

    2008-11-27 17:00:00
  • 简单代码实现可输入的下拉框功能(select)

    2008-10-20 19:52:00
  • 基于Python2、Python3中reload()的不同用法介绍

    2023-10-01 17:59:15
  • Bresenham图形算法JavaScript版本

    2010-01-25 12:09:00
  • python实现FTP文件传输的方法(服务器端和客户端)

    2021-03-29 23:36:08
  • 解决pycharm启动后总是不停的updating indices...indexing的问题

    2023-03-05 07:35:38
  • php 静态页面中显示动态内容

    2023-11-18 22:09:22
  • 解析smarty模板中类似for的功能实现

    2023-11-15 12:53:40
  • Python求解平方根的方法

    2023-02-13 13:25:47
  • Django1.11自带分页器paginator的使用方法

    2021-07-04 12:44:23
  • 详谈python3 numpy-loadtxt的编码问题

    2021-08-28 06:42:09
  • asp之家 网络编程 m.aspxhome.com