You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用shapviz::shapviz报错:维度数量不正确,求排查方法

问题

运行shapviz::shapviz时触发如下错误:

Error in s[, nms, drop = FALSE] : incorrect number of dimensions

我已确认使用了模型要求的正确预测变量(通过recipes::bake保留),并且处理了数据中的NA值,但仍出现该错误,请问原因是什么?

代码如下:

syllable_mod <- bundle::unbundle(lyricassessr::syllable_classifier_bundle)

# tidymodels预测正常运行:

preds <- predict(syllable_mod, new_data = audio_features, type = "prob") %>%
  dplyr::rename_with(~stringr::str_remove_all(.x, ".pred_")) %>%
  tidyr::pivot_longer(dplyr::everything(),
                      names_to = "Syllable", values_to = "Probability") %>%
  dplyr::arrange(dplyr::desc(Probability))

xgb_model <- parsnip::extract_fit_engine(syllable_mod)

# 确保无NA值(暂时用1填充,无实际意义)
audio_features <- audio_features %>%
  dplyr::mutate(across(everything(), ~ ifelse(is.na(.x), 1, .x)))

audio_features_prepped <- recipes::bake(
  lyricassessr::prepped_recipe,
  recipes::has_role("predictor"),
  new_data = audio_features) %>%
  dplyr::mutate(across(everything(), ~ ifelse(is.na(.x), 1, .x)))

# 确保audio_features与预处理后的特征一致
audio_features <- audio_features %>%
  dplyr::select(dplyr::all_of(names(audio_features_prepped)))


shap_values <- shapviz::shapviz(xgb_model, X_pred = as.matrix(audio_features_prepped), X = as.matrix(audio_features))
解决方案

这个错误本质是shapviz在处理SHAP值时遇到了维度不匹配的问题,常见原因及解决办法:

  • 手动计算SHAP值再传入:直接传入parsnip提取的xgboost引擎对象,shapviz可能无法正确生成SHAP矩阵。建议先手动计算SHAP贡献值,再创建可视化对象:

    # 计算SHAP贡献矩阵,包含基线值
    shap_contrib <- xgboost::predict(xgb_model, as.matrix(audio_features_prepped), predcontrib = TRUE)
    # 移除最后一列的基线值,只保留特征的SHAP值
    shap_mat <- shap_contrib[, -ncol(shap_contrib)]
    
    # 调用shapviz,传入SHAP矩阵和对应特征数据
    shap_values <- shapviz::shapviz(shap_mat, X = as.matrix(audio_features_prepped))
    
  • 确保输入数据为二维矩阵:如果你的数据集只有一行,as.matrix()会生成一维数组,导致维度错误。可以通过drop = FALSE强制生成二维矩阵:

    audio_features_prepped_mat <- as.matrix(audio_features_prepped, drop = FALSE)
    audio_features_mat <- as.matrix(audio_features, drop = FALSE)
    
  • 检查X_pred参数的使用:shapviz中X_pred是用于计算SHAP值的数据集,若直接传入模型,无需重复指定该参数。尝试简化调用:

    shap_values <- shapviz::shapviz(xgb_model, X = as.matrix(audio_features_prepped))
    

内容的提问来源于stack exchange,提问作者eartoolbox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:13:12