小程序开发资讯 R谈话SHAP模子评释注解

💡专注R谈话在🩺生物医学中的使用

app

设为“星标”,精彩可以过

剖判评释注解高度依赖于展望变量的划定,科罚设施有两个,一个是通过把最遑急的变量放在最前边,另一种等于识别变量间的交互作用并使用特意的设施。

可是以上两种设施都不是很好。是以出现了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值。

范闲双色球第2024079期红球012路分析:上期红球012路比为2:1:3,2路红球较热,1路红球较冷;最近7期红球012路比为16:12:14,0路红球较热,1路红球较冷。

还可以不展示箱线图:

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的包,我会写多篇推文,把常用的皆备先容一遍。

本站仅提供存储作事,所有实验均由用户发布,如发现存害或侵权实验,请点击举报。

Powered by 企业小程序小程序定制开发 @2013-2022 RSS地图 HTML地图

Copyright Powered by365站群 © 2013-2024 云迈科技 版权所有