发布日期:2024-08-24 04:46 点击次数:65
?专注R言语在?生物医学中的使用麻豆 艾鲤
设为“星标”,精彩可以过
明白解说高度依赖于揣测变量的章程,惩处要害有两个,一个是通过把最进军的变量放在最前边,另一种即是识别变量间的交互作用并使用突出的要害。
然则以上两种要害王人不是很好。是以出现了SHAP(SHapley Additive exPlanations),汉文称为Shaply加性解说。SHapley加性解说(SHAP)基于Shapley(东谈主名)在博弈论中建议的“Shapley值(Shaply-values)”。SHAP是专为揣测模子狡计的要害的首字母缩写词。
浅近来说,Shaply加性解说即是计较变量间的扫数可能的成列,然后计较每个变量的平均孝敬(粗略叫平均归因)。这种要害叫作念重排(permutation)SHAP粗略置换SHAP。
算作一种与模子无关(model-agnostic)的解说,这种要害是适用于任何模子的,本文是以当场丛林模子为例进行演示的。
若是还是了解了明白解说的旨趣,那么这里的重排SHAP就颠倒好联结了。它的瞩目公式计较历程这里就不展示了,感好奇瞻仰好奇瞻仰的可以我方了解。
今天先先容下R中的instance-level的SHAP,依然是使用DALEX,3行代码惩处!对于SHAP的实质其实还有颠倒多哈,以后再迟缓先容。
公众号后台薪金shap即可得到SHAP解评话籍。
library(DALEX)data("titanic_imputed")# 成果变量酿成因子型titanic_imputed$survived <- factor(titanic_imputed$survived)dim(titanic_imputed)
[1] 2207 8
str(titanic_imputed)
'data.frame': 2207 obs. of 8 variables: $ gender : Factor w/ 2 levels "female","male": 2 2 2 1 1 2 2 1 2 2 ... $ age : num 42 13 16 39 16 25 30 28 27 20 ... $ class : Factor w/ 7 levels "1st","2nd","3rd",..: 3 3 3 3 3 3 2 2 3 3 ... $ embarked: Factor w/ 4 levels "Belfast","Cherbourg",..: 4 4 4 4 4 4 2 2 2 4 ... $ fare : num 7.11 20.05 20.05 20.05 7.13 ... $ sibsp : num 0 0 1 1 0 0 1 1 0 0 ... $ parch : num 0 2 1 1 0 0 0 0 0 0 ... $ survived: Factor w/ 2 levels "0","1": 1 1 1 2 2 2 1 2 2 2 ...
成就一个当场丛林模子:
library(randomForest)set.seed(123)titanic_rf <- randomForest(survived ~ ., data = titanic_imputed)
成就解说器:
explain_rf <- DALEX::explain(model = titanic_rf, data = titanic_imputed[,-8], y = titanic_imputed$survived == 1, label = "randomforest" )
Preparation of a new explainer is initiated -> model label : randomforest -> data : 2207 rows 7 cols -> target variable : 2207 values -> predict function : yhat.randomForest will be used ( default ) -> predicted values : No value for predict function target column. ( default ) -> model_info : package randomForest , ver. 4.7.1.1 , task classification ( default ) -> model_info : Model info detected classification task but 'y' is a logical . Converted to numeric. ( NOTE ) -> predicted values : numerical, min = 0 , mean = 0.2350131 , max = 1 -> residual function : difference between y and yhat ( default ) -> residuals : numerical, min = -0.886 , mean = 0.08714363 , max = 1 A new explainer has been created!
使用predict_parts解说,要害遴荐SHAP:
shap_rf <- predict_parts(explainer = explain_rf, new_observation = titanic_imputed[15,-8], type = "shap", B = 25 # 遴荐若干个成列组合 )shap_rf
min q1 medianrandomforest: age = 18 -0.010423199 0.006507476 0.02422882randomforest: class = 3rd -0.201079293 -0.126367830 -0.06920344randomforest: embarked = Southampton -0.022489352 -0.010681242 -0.01012868randomforest: fare = 9.07 -0.154593566 -0.058991844 -0.02455460randomforest: gender = female 0.293671047 0.384545537 0.43246217randomforest: parch = 1 -0.031936565 0.080251817 0.10775804randomforest: sibsp = 0 0.008140462 0.014347757 0.02413484 mean q3 maxrandomforest: age = 18 0.067138668 0.1240188038 0.19714907randomforest: class = 3rd -0.090971092 -0.0672904395 -0.01977254randomforest: embarked = Southampton -0.006165292 -0.0006504304 0.01238423randomforest: fare = 9.07 -0.037531346 -0.0193303126 0.04265791randomforest: gender = female 0.436079928 0.4822868147 0.54142003randomforest: parch = 1 0.092327612 0.1308228364 0.17770367randomforest: sibsp = 0 0.028108382 0.0478994110 0.05099230
绘制:
plot(shap_rf)
图片
这个图中的箱线图暗意揣测变量在扫数成列的散布情况,条形图暗意平均值,也即是shaply值。
还可以不展示箱线图:
plot(shap_rf, show_boxplots = F)
图片
DALEX中的plot函数对ggplot2的包装,是可以径直联结ggplot2语法的。
波多野结衣作品集除此除外,咱们也可以索取数据我方绘制。
library(tidyverse)library(ggsci)shap_rf %>% as.data.frame() %>% mutate(mean_con = mean(contribution), .by = variable) %>% mutate(variable = fct_reorder(variable, abs(mean_con))) %>% ggplot() + geom_bar(data = \(x) distinct(x,variable,mean_con), aes(mean_con, variable,fill= mean_con > 0), alpha = 0.5, stat = "identity")+ geom_boxplot(aes(contribution,variable,fill= mean_con > 0), width = 0.4)+ scale_fill_lancet()+ labs(y = NULL)+ theme(legend.position = "none")
图片
OVER!
SHAP的使用率颠倒高,在R言语中也有颠倒多齐全SHAP的包,我会写多篇推文,把常用的十足先容一遍。
本站仅提供存储职业,扫数实质均由用户发布,如发现存害或侵权实质,请点击举报。