这是构建R:
中的回归问题(预测年龄)的堆叠合奏的示例
library('h2o')
h2o.init()
files3 = "http://h2o-public-test-data.s3.amazonaws.com/smalldata/prostate/prostate.csv"
col_types <- c("Numeric","Numeric","Numeric","Enum","Enum","Numeric","Numeric","Numeric","Numeric")
dat <- h2o.importFile(files3,destination_frame = "prostate.hex",col.types = col_types)
ss <- h2o.splitFrame(dat, ratios = 0.8, seed = 1)
train <- ss[[1]]
test <- ss[[2]]
x <- c("CAPSULE","GLEASON","RACE","DPROS","DCAPS","PSA","VOL")
y <- "AGE"
nfolds <- 5
# Train & Cross-validate a GBM
my_gbm <- h2o.gbm(x = x,
y = y,
training_frame = train,
distribution = "gaussian",
max_depth = 3,
learn_rate = 0.2,
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE,
seed = 1)
# Train & Cross-validate a RF
my_rf <- h2o.randomForest(x = x,
y = y,
training_frame = train,
ntrees = 30,
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE,
seed = 1)
# Train & Cross-validate a extremely-randomized RF
my_xrf <- h2o.randomForest(x = x,
y = y,
training_frame = train,
ntrees = 50,
histogram_type = "Random",
nfolds = nfolds,
fold_assignment = "Modulo",
keep_cross_validation_predictions = TRUE,
seed = 1)
# Train a stacked ensemble using the models above
stack <- h2o.stackedEnsemble(x = x,
y = y,
training_frame = train,
validation_frame = test, #also test that validation_frame is working
model_id = "my_ensemble_gaussian",
base_models = list(my_gbm@model_id, my_rf@model_id, my_xrf@model_id))
# predict
pred <- h2o.predict(stack, newdata = test)