kdo*_*rty 4 python r lightgbm tweedie shap
Shapley 加性解释(SHAP 值)的用途是了解每个特征如何对模型的预测做出贡献。对于某些目标,例如以 RMSE 作为目标函数的回归,SHAP 值采用标签值的本机单位。例如,如果估算住房成本,SHAP 值可以表示为美元。正如您将在下面看到的,并非所有目标函数都是如此。特别是,Tweedie 回归目标不会产生以本机单位表示的 SHAP 值。这是一个需要解释的问题,因为我们想知道住房成本如何受到+/-美元特征的影响。
有了这些信息,我的问题是:在解释具有 Tweedie 回归目标的模型时,如何将每个单独特征的 SHAP 值转换为目标标签的数据空间?
我不知道目前有任何包实现了这种转换。在 shap 作者自己提出的软件包中,这个问题仍然没有解决。
我用 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, features, 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, features, rawscore = FALSE, predcontrib = TRUE)
shapRaw <- predict(mod, features, 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
Run Code Online (Sandbox Code Playgroud)
原始帖子和编辑
我对 SHAP 值的理解是,在进行回归时,它们采用标签/响应的本机单位,并且 SHAP 值的总和近似于模型的预测。
我试图使用 Tweedie 回归目标提取 LightGBM 包中的 SHAP 值,但发现 SHAP 值不是标签的本机单位,并且它们的总和不等于预测值。
看来它们必须求幂,这是正确的吗?
旁注:据我所知,SHA 值矩阵的最后一列代表基本预测,必须添加。
可重现的例子:
library(tweedie)
library(caret)
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)
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, features, predcontrib = TRUE, rawscore = FALSE)
shaps_sum <- rowSums(shap_vals)
plot(shaps_sum, labels,
main = paste('RMSE =',
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, labels,
main = paste('RMSE =',
RMSE(pred = shap_vals_exp_sum, obs = labels)))
Run Code Online (Sandbox Code Playgroud)
编辑
运算顺序是先求和,然后对 SHAP 值求幂,这将为您提供本机单位的预测。尽管我仍然不清楚如何将特征级别值转换为本机响应单位。
shap_vals_sum_exp <- exp(shaps_sum)
plot(shap_vals_sum_exp, labels,
main = paste('RMSE =',
RMSE(pred = shap_vals_sum_exp, obs = labels)))
Run Code Online (Sandbox Code Playgroud)
我将展示如何在 Python 中协调形状值和模型预测,包括原始分数和原始单位。希望它能帮助您了解您在 R 中的位置。
步骤1.生成数据集
# pip install tweedie
import tweedie
y = tweedie.tweedie(1.2,1,1).rvs(size=1000)
X = np.random.randn(1000,3)
Run Code Online (Sandbox Code Playgroud)
步骤 2. 拟合模型
from lightgbm.sklearn import LGBMRegressor
lgb = LGBMRegressor(objective = 'tweedie')
lgb.fit(X,y)
Run Code Online (Sandbox Code Playgroud)
第 3 步:了解什么是 shap 值。
第 0 个数据点的形状值
shap_values = lgb.predict(X, pred_contrib=True)
shap_values[0]
array([ 0.36841812, -0.15985678, 0.28910617, -0.27317984])
Run Code Online (Sandbox Code Playgroud)
前 3 个是模型对基线的贡献,即形状值本身:
shap_values[0,:3].sum()
0.4976675073764354
Run Code Online (Sandbox Code Playgroud)
第四个是原始分数的基线:
shap_values[0,3]
-0.2731798364061747
Run Code Online (Sandbox Code Playgroud)
它们的总和加起来就是原始分数中的模型预测:
shap_values[0,:3].sum() + shap_values[0,3]
0.22448767097026068
Run Code Online (Sandbox Code Playgroud)
让我们检查一下原始模型预测:
preds = lgb.predict(X, raw_score=True)
preds[0]
0.2244876709702609
Run Code Online (Sandbox Code Playgroud)
编辑。原始分数和原始分数之间的转换
要在 Tweedie(以及 Poisson 和 Gamma)分布的原始分数和原始单位之间进行转换,您需要了解 2 个事实:
exp生的exp的sum是product的exps演示:
# pip install tweedie
import tweedie
y = tweedie.tweedie(1.2,1,1).rvs(size=1000)
X = np.random.randn(1000,3)
Run Code Online (Sandbox Code Playgroud)
from lightgbm.sklearn import LGBMRegressor
lgb = LGBMRegressor(objective = 'tweedie')
lgb.fit(X,y)
Run Code Online (Sandbox Code Playgroud)
shap_values = lgb.predict(X, pred_contrib=True)
shap_values[0]
array([ 0.36841812, -0.15985678, 0.28910617, -0.27317984])
Run Code Online (Sandbox Code Playgroud)
又和我长得很像了。