0

我有一个使用 H2O 包创建的 automl 模型。目前,H2O 仅在基于树的模型上计算 Shapley 值。我使用 IML 包来计算 AML 模型上的值。不过,因为我有大量的特征,所以剧情太杂乱而无法阅读。我正在寻找一种仅选择/显示前 X 个功能的方法。我在 IML CRAN PDF 和谷歌搜索找到的其他文档中都找不到任何内容。

#initiate h2o
h2o.init()
h2o.no_progress()

#create automl model (data cleaning and train/test split not shown)
set.seed(1911)
num_models <- 10
aml <- h2o.automl(y = label, x = features,
                   training_frame = train.hex,
                   nfolds = 5,
                   balance_classes = TRUE,
                   leaderboard_frame = test.hex,
                   sort_metric = 'AUCPR',
                   max_models = num_models,
                   verbosity = 'info',
                   exclude_algos = "DeepLearning", #exclude for reproducibility
                   seed = 27)

# 1. create a data frame with just the features
features_eval <- as.data.frame(test) %>% dplyr::select(-target)

# 2. Create a vector with the actual responses
response <- as.numeric(as.vector(test$target))

# 3. Create custom predict function that returns the predicted values as a
#    vector (probability of purchasing in our example)
pred <- function(model, newdata)  {
  results <- as.data.frame(h2o.predict(model, as.h2o(newdata)))
  return(results[[3L]])
}

# example of prediction output
pred(aml, features_eval) %>% head()

#create predictor needed
predictor.aml <- Predictor$new(
  model = aml, 
  data = features_eval, 
  y = response, 
  predict.fun = pred,
  class = "classification"
  )

high <- predict(aml, test.hex) %>% .[,3] %>% as.vector() %>% which.max()

high_prob_ob <- features_eval[high, ]

shapley <- Shapley$new(predictor.aml, x.interest = high_prob_ob, sample.size = 200) 

plot(shapley, sort = TRUE)

任何建议/帮助表示赞赏。

谢谢你,布赖恩

4

1 回答 1

0

iml我可以提供一个 hacky 解决方案,该解决方案利用了ggplot2用于绘图的事实。

N <- 10 # number of features to show

# Capture the ggplot2 object
p <- plot(shapley, sort = TRUE)

# Modify it so it shows only top N features
print(p + scale_x_discrete(limits=rev(p$data$feature.value[order(-p$data$phi)][1:N])))
于 2021-04-15T15:57:53.173 回答