h2o SHAP值/用于交叉验证的predict_contributions

时间:2019-05-06 16:39:26

标签: r h2o

我研究了h2o.predict_contributions函数,该函数公开了xgb和gbm模型的Shap值。此功能是否还会从交叉验证预测中提供这些指标?我似乎找不到它们。

library(h2o)
library(mlbench)
data(Sonar)

Sonar.h2o = as.h2o(Sonar)

mdl = h2o.xgboost(x=names(Sonar), y='Class', training_frame = Sonar, nfolds=5, keep_cross_validation_predictions = TRUE)

1 个答案:

答案 0 :(得分:1)

是的,您可以将函数应用于单个感兴趣的地方,这是一些示例代码:

library(h2o)
h2o.init()
prostate_path <- system.file("extdata", "prostate.csv", package = "h2o")
prostate <- h2o.uploadFile(path = prostate_path)
prostate_gbm <- h2o.gbm(3:9, "AGE", prostate, nfolds = 3)
h2o.predict(prostate_gbm, prostate)
h2o.predict_contributions(prostate_gbm, prostate)

# take a look at the output to see which key you want to use
# there are also other options to key names
prostate_gbm@model$cross_validation_models

# update this with the key of interest
key = 'GBM_model_R_1557326910287_7702_cv_2'
cv2 = h2o.getModel(key)
h2o.predict_contributions(cv2, prostate)

# RACE       DPROS      DCAPS         PSA        VOL     GLEASON __internal_cv_weights__ BiasTerm
# 1 -0.006481315 -0.19211742 -0.0836791 -0.06186131 -0.9217098 -0.20128664                       0 66.37209
# 2 -0.005238285 -1.09128833  0.9614767 -0.95340544 -0.7698430  0.06820074                       0 66.37209
# 3 -0.006481315  0.98101193  0.1770813  1.21195042 -1.0359415 -0.23213011                       0 66.37209
# 4  0.069538474 -0.01738315 -0.2000238  4.11799049  0.1177490 -0.01457024                       0 66.37209
# 5  0.012923095  0.40362182 -0.1132747  1.21669090  0.9920316 -0.37245926                       0 66.37209
# 6 -0.002282504 -0.91798097  0.9024866 -0.17398398 -0.6048008  0.42300656                       0 66.37209

注意:您可以忽略__internal_cv_weights__列。我创建了一个票证来清理可以跟踪here的输出。