Method Feature(s) Sample(s) Result Value/Feature
Permutation Importance 1 all validation samples Single Scale
Partial Dependence Plots 1~2 all validation samples Vector(reasults vs feature)
SHAP Values N individual sample 每个feature对当前结果的贡献(相对于baseline)
Advanced Uses of SHAP Values- Summary Plots N all 绘制每个feature在每个样本预测结果中的贡献(相对于baseline)
Advanced Uses of SHAP Values- SHAP Dependence Contribution Plots 2 all 绘制2个feature在所有样本也测结果中的贡献(相对于baseline)

参考: https://www.kaggle.com/learn/machine-learning-explainability

这个课程将讲解如何从复杂的机器学习模型中解释这些发现。

  1. 模型认为数据中的哪些特征是最重要的?
  2. 对于来自模型的任何单个预测,数据中的每个特性如何影响该特定预测
  3. 每个特性如何影响模型的整体预测(当考虑大量可能的预测时,它的典型影响是什么?)
这些发现有许多用途,包括
  1. 调试,理解模型所发现的模式将帮助您识别那些与您对真实世界的认识不一致的地方
  2. 为特征工程提供信息
  3. 指导未来的数据收集
  4. 为人的决策提供信息
  5. 建立信任,提高产品在用户中的接受度。

Permutation Importance置换重要性

统计每个feature的重要程度,训具体步骤如下:

  1. 正常训练完模型。
  2. 对原始validation数据,依次shuffle每个feature的原始数据。
  3. 根据得到的模型参数,对shuffle后的数据进行预测,计算性能(准确度)下降程度。
  4. 对每个feature重复2-3,最后得出每个feature的重要程度(shuffle它后性能下降程度)

用eli5库实现的置换重要性计算

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier data = pd.read_csv('../input/fifa-2018-match-statistics/FIFA 2018 Statistics.csv')
y = (data['Man of the Match'] == "Yes") # Convert from string "Yes"/"No" to binary
feature_names = [i for i in data.columns if data[i].dtype in [np.int64]]
X = data[feature_names]
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
my_model = RandomForestClassifier(random_state=0).fit(train_X, train_y)

import eli5
from eli5.sklearn import PermutationImportance
perm = PermutationImportance(my_model, random_state=1).fit(val_X, val_y)
eli5.show_weights(perm, feature_names = val_X.columns.tolist())

官方例程输出如下,其中排在前面的是更重要的feature,排在后面的是不那么重要的feature,最后偶然出现负数,也是正常现象。

毕竟是shuffle feature data,对一些不太重要的feature,偶尔出现shuffle后比shuffle前更准确也时有发生。

Partial Dependence Plots

用于统计feature(s)如何影响predictions,用pdpbox库

单个feature的影响

# Build Random Forest model
rf_model = RandomForestClassifier(random_state=0).fit(train_X, train_y) pdp_dist = pdp.pdp_isolate(model=rf_model, dataset=val_X, model_features=feature_names, feature=feature_to_plot) pdp.pdp_plot(pdp_dist, feature_to_plot)
plt.show()

两个features的组合影响

# Similar to previous PDP plot except we use pdp_interact instead of pdp_isolate and pdp_interact_plot instead of pdp_isolate_plot
features_to_plot = ['Goal Scored', 'Distance Covered (Kms)']
inter1 = pdp.pdp_interact(model=tree_model, dataset=val_X, model_features=feature_names, features=features_to_plot) pdp.pdp_interact_plot(pdp_interact_out=inter1, feature_names=features_to_plot, plot_type='contour')
plt.show()

SHAP Values, SHapley Additive exPlanations

对于特定sample的预测,解释每个feature在其中的影响,正负都有。

可用于:

  1. 一个模型说银行不应该借钱给别人,法律要求银行解释每笔贷款被拒的原因。
  2. 医疗服务提供者想要确定,是什么因素导致了每个病人患某些疾病的风险,这样他们就可以通过有针对性的健康干预,直接解决这些风险因素

使用shap库,代码片段如下,其中KernelExplainer 结果和TreeExplainer不完全一样,但是比较接近,结果中表达的意思相同。

import shap  # package used to calculate Shap values

# Create object that can calculate shap values
explainer = shap.TreeExplainer(my_model) # Calculate Shap values
shap_values = explainer.shap_values(data_for_prediction) shap.initjs()
shap.force_plot(explainer.expected_value[1], shap_values[1], data_for_prediction) # use Kernel SHAP to explain test set predictions
k_explainer = shap.KernelExplainer(my_model.predict_proba, train_X)
k_shap_values = k_explainer.shap_values(data_for_prediction)
shap.force_plot(k_explainer.expected_value[1], k_shap_values[1], data_for_prediction)

运行结果的图表类似如下图形,

  • 其中左边(红色)代表当前样本相对于baseline增加的预测值
  • 右边(蓝色)代表当前样本相对于baseline减少的预测值
  • 左边(红色) - 右边(蓝色) => output_value - base_value

Advanced Uses of SHAP Values

Summary Plots

import shap  # package used to calculate Shap values

# Create object that can calculate shap values
explainer = shap.TreeExplainer(my_model) # calculate shap values. This is what we will plot.
# Calculate shap_values for all of val_X rather than a single row, to have more data for plot.
shap_values = explainer.shap_values(val_X) # Make plot. Index of [1] is explained in text below.
shap.summary_plot(shap_values[1], val_X)

结果如下图所示:

  • 每个点代表一个sample
  • 垂直方向是特征
  • 水平方向是该特征对应的SHAP Value
  • 颜色代表该特征的数值大小

SHAP Dependence Contribution Plots

import shap  # package used to calculate Shap values

# Create object that can calculate shap values
explainer = shap.TreeExplainer(my_model) # calculate shap values. This is what we will plot.
shap_values = explainer.shap_values(X) # make plot.
shap.dependence_plot('Ball Possession %', shap_values[1], X, interaction_index="Goal Scored")

运行结果图简介:

  • 横坐标表示Ball Possession %特征的值
  • 纵坐标表示SHAP Value值
  • 颜色(如右边注释)表示Goal Scored特征的值

学习小记: Kaggle Learn - Machine Learning Explainability的更多相关文章

  1. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  2. ML Lecture 0-2: Why we need to learn machine learning?

    在Github上也po了这个系列学习笔记(MachineLearningCourseNote),觉得写的不错的小伙伴欢迎来给项目点个赞哦~~ ML Lecture 0-2: Why we need t ...

  3. kaggle _Titanic: Machine Learning from Disaster

    A Data Science Framework: To Achieve 99% Accuracy https://www.kaggle.com/ldfreeman3/a-data-science-f ...

  4. Kaggle:Titanic: Machine Learning from Disaster

    一直想着抓取股票的变化,偶然的机会在看股票数据抓取的博客看到了kaggle,然后看了看里面的题,感觉挺新颖的,就试了试. 题目如图:给了一个train.csv,现在预测test.csv里面的Passa ...

  5. 李宏毅老师机器学习课程笔记_ML Lecture 0-2: Why we need to learn machine learning?

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  6. 机器学习案例学习【每周一例】之 Titanic: Machine Learning from Disaster

     下面一文章就总结几点关键: 1.要学会观察,尤其是输入数据的特征提取时,看各输入数据和输出的关系,用绘图看! 2.训练后,看测试数据和训练数据误差,确定是否过拟合还是欠拟合: 3.欠拟合的话,说明模 ...

  7. How do I learn mathematics for machine learning?

    https://www.quora.com/How-do-I-learn-mathematics-for-machine-learning   How do I learn mathematics f ...

  8. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  9. In machine learning, is more data always better than better algorithms?

    In machine learning, is more data always better than better algorithms? No. There are times when mor ...

随机推荐

  1. 原来ReadWriteLock也能开发高性能缓存,看完我也能和面试官好好聊聊了!

    大家好,我是冰河~~ 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在 ...

  2. Java 给Word添加印章

    一.概述 本文以Java程序代码展示如何给Word文档添加印章,这里添加的印章为.png格式的图片,添加印章即在Word中的指定位置添加印章图片. 基本思路:加载word文档,获取段落,在段落中插入图 ...

  3. 手把手教你玩转HarmonyOS版地图应用开发

    ​一.导读 7月31日,华为HarmonyOS开发者日将在杭州举行.为了方便更多开发者,高德开放平台地图SDK已在业内率先实现鸿蒙化迁移和重构,全面适配HarmonyOS并面向开发者免费发布.开发者可 ...

  4. 一个完整的socket recv()案例,包括解决粘包、服务器主动推数据的问题

    前言: 本文是针对socket长连接(涉及到服务器主动推数据),每个包头的拼接算法和长度都不一样,具体的包头小伙伴们问自己公司的开发吧,本文只是提供思路.再啰嗦一句:recv到的包头中数字进行某种运算 ...

  5. root密码找回

    1,启动系统时,按上下键,选择第一项,按e. 2,编辑kernel中,将rhgb quiet 替换作init=/bin/sh.回车确认修改 3,根据提示按b键继续启动. 4,进入bash窗口并有管理员 ...

  6. 大数据学习(18)—— Flume介绍

    老规矩,学习新东西先上官网瞅瞅Apache Flume Flume是什么 Flume是一个分布式.可靠的大规模高效日志收集.汇聚和传输的这么一个服务.它的架构基于流式数据,配置简单灵活.它具备可调节的 ...

  7. ES6新特征

    1.块级作用域 {   }  就是块级作用域,还包括if.else.for.while...下都属于块级作用域. let 声明的变量不存在变量的提升,不允许let反复声明同一个变量:块级作用域下let ...

  8. 用python将word转pdf、doc转docx等

    word ==> pdf def doc2pdf(file_path): """ word格式转换doc|docx ==> pdf :return: &quo ...

  9. SpringBoot+Maven 多模块项目的构建、运行、打包实战

    前言 最近在做一个很复杂的会员综合线下线上商城大型项目,单模块项目无法满足多人开发和架构,很多模块都是重复的就想到了把模块提出来,做成公共模块,基于maven的多模块项目,也好分工开发,也便于后期微服 ...

  10. 20分钟掌握Android Gradle

    目前国内对Android领域的探索已经越来越深,不少技术领域如插件化.热修复.构建系统等都对Gradle有迫切的需求,不懂Gradle将无法完成上述事情.所以Gradle必须要学习. Gradle 里 ...