问题描述
我有一个用 H2O 包创建的 automl 模型。目前,H2O 仅在基于树的模型上计算 Shapley 值。我已经使用 IML 包来计算 AML 模型上的值。不过因为我的特点比较多,情节太乱,看不下去了。我正在寻找一种仅选择/显示前 X 个功能的方法。我在 IML CRAN PDF 或通过 Google 搜索找到的其他文档中都找不到任何内容。
#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)
感谢任何建议/帮助。
谢谢, 布莱恩
解决方法
我可以提供一个利用 iml
使用 ggplot2
进行绘图的事实的hacky 解决方案。
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])))