首页 > 其他分享 >Machine Learning Explainability

Machine Learning Explainability

时间:2022-12-21 17:00:26浏览次数:58  
标签:plot shap feature pdp Machine values Learning Explainability model

Machine Learning Explainability

Extract human-understandable insights from any model.

Permutation Importance

What features does your model think are important?

核心思想:如果一个 feature 对模型很重要,那么对于原始数据集,打乱这个 feature 的值之后再进行预测,得到的 loss 会很大。对于每一个 feature 都进行这样的处理和计算,之后就能得到影响最大的 feature。

the process is as follows:

  1. Get a trained model.
  2. Shuffle the values in a single column, make predictions using the resulting dataset. Use these predictions and the true target values to calculate how much the loss function suffered from shuffling. That performance deterioration measures the importance of the variable you just shuffled.
  3. Return the data to the original order (undoing the shuffle from step 2). Now repeat step 2 with the next column in the dataset, until you have calculated the importance of each column.
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())

每行中的第一个数字显示随机改组后模型性能下降了多少(在这种情况下,使用“准确性”作为性能指标)。

与数据科学中的大多数事情一样,对列进行改组的确切性能变化存在一些随机性。 我们通过多次洗牌重复该过程来测量排列重要性计算中的随机性。 ± 后面的数字衡量性能从一次改组到下一次改组的变化情况。

偶尔会看到排列重要性的负值。 在这些情况下,对打乱(或嘈杂)数据的预测恰好比真实数据更准确。 当特征无关紧要(重要性应该接近 0)时会发生这种情况,但随机机会导致对混洗数据的预测更加准确。 这在小型数据集(如本例中的数据集)中更为常见,因为运气/机会的空间更大。

出租车示例

大多数地方不会根据乘客人数改变票价。但是在接触一个模型之前,不能假设纽约市是一样的,必须要根据实际情况来。

重点思考从结果如何分析可能性(下面第 2、3 条):

  1. 旅行的纬度距离可能比经度距离大。 如果经度值通常靠得更近,那么将它们改组就没有那么重要了。 2. 城市的不同地区可能有不同的定价规则(例如每英里的价格),并且定价规则因纬度而不是经度而异。 3. 向北<->向南(纬度变化)的道路收费可能高于向东<->向西(经度变化)的道路。 因此,纬度会对预测产生更大的影响,因为它反映了通行费的数量。

思考一下这几个问题:https://www.kaggle.com/code/jinxinc/exercise-permutation-importance/edit

Partial Plots

How does each feature affect your predictions?

While feature importance shows what variables most affect predictions, partial dependence plots show how a feature affects predictions.

要了解 Partial Plots 如何分离出每个特征的影响,我们首先考虑单行数据。 例如,该行数据可能代表一支球队有 50% 的控球时间、传球 100 次、射门 10 次并打进 1 球。

我们将使用拟合模型来预测我们的结果(他们的球员赢得“比赛最佳球员”的概率)。 但是我们反复更改一个变量的值以进行一系列预测。 如果球队只有 40% 的时间控球,我们就可以预测结果。 然后我们预测他们有 50% 的时间控球。 然后再次预测 60%。 等等。 当我们从控球率的小值移动到大值(在水平轴上)时,我们追踪预测结果(在垂直轴上)。

在这个描述中,我们只使用了一行数据。 特征之间的相互作用可能导致单行图不典型。 因此,我们用原始数据集中的多行重复该心理实验,并在垂直轴上绘制平均预测结果。

from matplotlib import pyplot as plt
from pdpbox import pdp, get_dataset, info_plots

# Create the data that we will plot
pdp_goals = pdp.pdp_isolate(model=tree_model, dataset=val_X, model_features=feature_names, feature='Goal Scored')

# plot it
pdp.pdp_plot(pdp_goals, 'Goal Scored')
plt.show()
# 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()

考虑一个场景,你只有 2 个预测特征,我们称之为 feat_A 和 feat_B。 这两个特征的最小值为 -1,最大值为 1。feat_A 的部分依赖图在其整个范围内急剧增加,而特征 B 的部分依赖图在其整个范围内以较慢的速度(不太陡峭)增加。

这是否保证 feat_A 的排列重要性高于 feat_B。 为什么或者为什么不?

不,这并不能保证 feat_a 更重要。 例如,feat_a 在变化的情况下可能会产生很大影响,但在 99% 的情况下可能只有一个值。 在那种情况下,置换 feat_a 并不重要,因为大多数值都不会改变。

重新理解一下:https://www.kaggle.com/code/jinxinc/exercise-partial-plots/edit

SHAP Values

Understand individual predictions

使用场景:

  • 一个模型说银行不应该借钱给某人,并且法律要求银行解释每笔贷款拒绝的依据
  • 医疗保健提供者想要确定哪些因素导致每位患者患某种疾病的风险,以便他们可以通过有针对性的健康干预措施直接解决这些风险因素
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)
  • shap.DeepExplainer works with Deep Learning models.
  • shap.KernelExplainer works with all models, though it is slower than other Explainers and it offers an approximation rather than exact Shap values.
# 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)

参考:

了解 3 个方法的关系:https://www.kaggle.com/code/jinxinc/exercise-shap-values/edit

Advanced Uses of SHAP Values

Aggregate SHAP values for even more detailed model insights

Summary Plots

Permutation importance 非常重要,因为它创建了简单的数字度量来查看哪些特征对模型很重要。 这有助于我们轻松地对功能进行比较,并且您可以将生成的图表展示给非技术人员。

但它并没有告诉您每个功能的重要性。 如果一个特征具有中等排列重要性,那可能意味着它有

  • 对一些预测有很大影响,但总体上没有影响,或者
  • 所有预测的中等效果。

SHAP 摘要图为我们提供了特征重要性及其驱动因素的鸟瞰图。

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)
  • 绘图时,我们调用 shap_values[1]。 对于分类问题,每个可能的结果都有一个单独的 SHAP 值数组。 在这种情况下,我们索引以获得预测“真”的 SHAP 值。
  • 计算 SHAP 值可能很慢。 这在这里不是问题,因为这个数据集很小。 但是在运行这些以使用合理大小的数据集进行绘图时,您需要小心。 例外情况是使用 xgboost 模型时,SHAP 对其进行了一些优化,因此速度更快。

SHAP Dependence Contribution Plots

Summary Plots 提供了模型的一个很好的概述,但我们可能想深入研究一个功能。 这就是 SHAP dependence contribution plots 发挥作用的地方。

分析异常点:

  1. 控球时间的增加并没有导致 SHAP 增加,可以将其解释为一般来说,拥有球会增加球队让其球员赢得奖项的机会。 但如果他们只进了一个球,那么这种趋势就会逆转,如果他们进得那么少,评委可能会因为他们控球太多而对他们进行处罚。
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")

思考这几个问题:https://www.kaggle.com/code/jinxinc/exercise-advanced-uses-of-shap-values/edit

由于效果范围对异常值非常敏感,因此排列重要性可以更好地衡量对模型通常重要的内容。

特征的高值和低值都可能对预测产生正面和负面影响。 对这种“混乱”效果的最可能解释是变量(在本例中为 num_lab_procedures)与其他变量有交互作用

标签:plot,shap,feature,pdp,Machine,values,Learning,Explainability,model
From: https://www.cnblogs.com/windchen/p/16996638.html

相关文章