如何解决有没有办法限制 IML 包 Shapley 值中的特征数量?
我有一个用 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])))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。