【问题标题】:Error when in `shap.prep()` - Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent在 `shap.prep()` 中出错 - dimnames(x) <- dn 中出错:\'dimnames\' [2] 的长度不等于数组范围
【发布时间】:2022-12-08 08:21:02
【问题描述】:

按照 Michael Mayer 的 post 中的步骤,我尝试使用以下示例代码在 R 中拟合 LightGBM(多类)分类器后进行快速 SHAP 分析:

library(dplyr)
library(ggplot2)
library(SHAPforxgboost)
library(lightgbm)

set.seed(111)
x1 <- rnorm(1:2000)
x2 <- rnorm(1:2000)
y <- rnorm(1:2000)

df <- data.frame(x1,x2,y)
df <- 
    df |> 
    mutate(y = abs(y),
                 y = round(y, digits = 0),
                 y = ifelse(y >= 2, 2, y),
                 y = as.character(y)) 


# Define response and features
y <- "y"
x <- c("x1","x2")

# random split
set.seed(83454)
ix <- sample(nrow(df), 0.8 * nrow(df))

dtrain <- lgb.Dataset(data.matrix(df[ix, x]),
                                            label = df[ix, y])
dvalid <- lgb.Dataset(data.matrix(df[-ix, x]),
                                            label = df[-ix, y])

params <- list(
    objective = "multiclass",
    metric = "multi_error",
    learning_rate = 0.05,
    num_leaves = 15,
    num_class = 3
)

fit_lgb <- lgb.train(params,
                     dtrain,
                     nrounds = 89L,
                     valids = list(valid = dvalid),
                     early_stopping_rounds = 20L
)

# SHAP IMPORTANCE
shap <- shap.prep(fit_lgb, X_train = as.matrix(df[,-3]))

但是,在运行 shap.prep(fit_lgb, X_train = as.matrix(df[,-3])) 后,我收到以下错误:“dimnames(x) <- dn 中的错误:‘dimnames’[2] 的长度不等于数组范围”

知道出了什么问题吗?

谢谢!

【问题讨论】:

    标签: r machine-learning lightgbm shap


    【解决方案1】:

    “SHAPforxgboost”不直接包装多类模型,因此您需要自己准备感兴趣类别的 SHAP 矩阵,然后使用“SHAPforxgboost”的通用接口。

    虽然它也不提供多类 SHAP 图,但我的姐妹包“shapviz”提供了一个 which_class 参数来简化上述步骤:

    library(shapviz)
    
    shap_values <- shapviz(fit_lgb, X_pred = as.matrix(df[,-3]), which_class = 3)
    
    sv_importance(shap_values, kind = "bee")
    sv_dependence(shap_values, "x1", color_var = "auto")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      相关资源
      最近更新 更多