使用lightgbm Tweedie目标将SHAP值从原始单位转换为原始单位吗?

如何解决使用lightgbm Tweedie目标将SHAP值从原始单位转换为原始单位吗?

编辑

Shapley附加说明(SHAP值)的实用程序是为了了解每个特征如何有助于模型的预测。对于某些目标,例如以RMSE作为目标函数的回归,SHAP值以标签值的本机单位表示。例如,如果估算住房成本,SHAP值可以表示为美元。正如您将在下面看到的,并非所有目标函数都如此。特别是,Tweedie回归目标不会以本机单位生成SHAP值。这是一个解释上的问题,因为我们想知道房屋成本如何受+/-美元影响。

鉴于此信息,我的问题是:在解释具有Tweedie回归目标的模型时,如何将每个单个要素的SHAP值转换到目标标签的数据空间中?

我不知道当前有任何实现此类转换的软件包。 This remains unresolved in the package put out by the shap authors themselves.

我在下面用lightgbm的R实现说明了这个问题的精妙之处:

library(tweedie)
library(lightgbm)

set.seed(123)

tweedie_variance_power <- 1.2

labels <- rtweedie(1000,mu = 1,phi = 1,power = tweedie_variance_power)
hist(labels)

feat1 <- labels + rnorm(1000) #good signal for label with some noise
feat2 <-rnorm(1000) #garbage feature 
feat3 <-rnorm(1000) #garbage feature 

features <- cbind(feat1,feat2,feat3)

dTrain <- lgb.Dataset(data = features,label = labels)

params <- c(objective = 'tweedie',tweedie_variance_power = tweedie_variance_power)

mod <- lgb.train(data = dTrain,params = params,nrounds = 100)

#Predictions in the native units of the labels
predsNative <- predict(mod,features,rawscore = FALSE)
#Predictions in the raw format
predsRaw <- predict(mod,rawscore = TRUE)

#We do not expect these values to be equal
all.equal(predsTrans,predsRaw)
"Mean relative difference: 1.503072"

#We expect values to be equal if raw scores are exponentiated
all.equal(predsTrans,exp(predsRaw))
"TRUE" #... our expectations are correct

#SHAP values 
shapNative <- predict(mod,rawscore = FALSE,predcontrib = TRUE)
shapRaw <- predict(mod,rawscore = TRUE,predcontrib = TRUE )

#Are there differences between shap values when rawscore is TRUE or FALSE?
all.equal(shapNative,shapRaw)
"TRUE" #outputs are identical,that is surprising!

#So are the shap values in raw or native formats?
#To anwser this question we can sum them

#testing raw the raw case first
all.equal(rowSums(shapRaw),predsRaw)
"TRUE" 

#from this we can conclude that shap values are not in native units,#regardless of whether rawscore is TRUE or FALSE

#Test native scores just to prove point
all.equal(rowSums(shapNative),predsNative)
"Mean relative difference: 1.636892" # reaffirms that shap values are not in native units

#However,we can perform this operation on the raw shap scores
#to get the prediction in the native value
all.equal(exp(rowSums(shapRaw)),predsNative)
'TRUE'

#reversing the operations does not yield the same result
all.equal(rowSums(exp(shapRaw)),predsNative)
"Mean relative difference: 0.7662481"

#The last line is relevant because it implies 
#The relationship between native predictions
#and exponentiated shap values is not linear

#So,given the point of SHAP is to understand how each 
#feature impacts the prediction in its native units
#the raw shap values are not as useful as they could be

#Thus,how how would we convert 
#each of these four raw shap value elements to native units,#thus understanding their contributions to their predictions
#in currency of native units?
shapRaw[1,]
-0.15429227  0.04858757 -0.27715359 -0.48454457

原始帖子和编辑

我对SHAP值的理解是,进行回归时,它们以标签/响应的本机单位为单位,并且SHAP值的总和近似于模型的预测。

我正在尝试使用Tweedie回归目标在LightGBM包中提取SHAP值,但是发现SHAP值不在标签的本机单位中,并且它们不等于预测值。

似乎必须对它们求幂,这是正确的吗?

旁注:我了解SHAP值矩阵的最后一列代表基本预测,必须添加。

可复制的示例:

library(tweedie)
library(caret)
library(lightgbm)

set.seed(123)

tweedie_variance_power <- 1.2

labels <- rtweedie(1000,nrounds = 100)

preds <- predict(mod,features)

plot(preds,labels,main = paste('RMSE =',RMSE(pred = preds,obs = labels)))

#shap values are summing to negative values?
shap_vals <- predict(mod,predcontrib = TRUE,rawscore = FALSE)
shaps_sum <- rowSums(shap_vals)
plot(shaps_sum,RMSE(pred = shaps_sum,obs = labels)))

#maybe we need to exponentiate?
shap_vals_exp <- exp(shap_vals)
shap_vals_exp_sum <- rowSums(shap_vals_exp)
#still looks a little weird,overpredicting 
plot(shap_vals_exp_sum,RMSE(pred = shap_vals_exp_sum,obs = labels)))

编辑

操作的顺序是先求和,然后取幂SHAP值,这将使您以本机单位进行预测。尽管我仍然不清楚如何将要素级别值转换为本地响应单位。

shap_vals_sum_exp <- exp(shaps_sum)
plot(shap_vals_sum_exp,RMSE(pred = shap_vals_sum_exp,obs = labels)))

解决方法

我将展示如何在原始分数和原始单位中调和Python中的shap值和模型预测。希望它将帮助您了解您在R中的位置。

步骤1。生成数据集

# pip install tweedie
import tweedie
y = tweedie.tweedie(1.2,1,1).rvs(size=1000)
X = np.random.randn(1000,3)

第2步。拟合模型

from lightgbm.sklearn import LGBMRegressor
lgb = LGBMRegressor(objective = 'tweedie')
lgb.fit(X,y)

第3步。了解什么是shap值。

第0个数据点的Shap值

shap_values = lgb.predict(X,pred_contrib=True)
shap_values[0]
array([ 0.36841812,-0.15985678,0.28910617,-0.27317984])

前3个是模型对基线的贡献,即shap值本身:

shap_values[0,:3].sum()
0.4976675073764354

第四是原始分数的基线:

shap_values[0,3]
-0.2731798364061747

它们的总和加到原始分数的模型预测中:

shap_values[0,:3].sum() + shap_values[0,3]
0.22448767097026068

让我们检查一下原始模型的预测:

preds = lgb.predict(X,raw_score=True)
preds[0]
0.2244876709702609

编辑。原始分数和原始utut之间的转换

要在Tweedie(以及Poisson和Gamma)分布的原始得分和原始单位之间进行转换,您需要了解以下两个事实:

  1. 原始物是exp的原始物
  2. exp
  3. sumproduct的{​​{1}}

演示:

  1. 第0个原始单位的预测:
exps
  1. 原始分数空间中第0行的Shap值:
lgb.predict([X[0,:]])
array([0.39394102])
  1. 将shap值转换为原始单位(指数的乘积):
shap_values = lgb.predict(X,pred_contrib=True,raw_score=True)
shap_values[0]
array([-0.77194274,-0.08343294,0.22740536,-0.30358374])

再次看起来类似于我。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res