我使用的是GBM模型,我想与其他机器学习方法进行比较。我跑了5次。正如我所知,他们将把数据分成5个折叠,并选择其中一个进行测试,另一个进行训练。如何从H2o-lib的gbm中获得5倍的数据?
我用Python语言运行它。
folds = 5
cars_gbm = H2OGradientBoostingEstimator(nfolds = folds, seed = 1234)
有两种方法:
- 您可以手动创建和指定折叠
- 您可以要求H2O保存折叠索引(对于每一行,它属于哪个折叠ID?(,并通过设置
keep_cross_validation_fold_assignment=True
将其作为单列数据返回
以下是一些代码示例:
import h2o
from h2o.estimators import *
h2o.init()
# Import cars dataset
cars = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/junit/cars_20mpg.csv")
cars["economy_20mpg"] = cars["economy_20mpg"].asfactor()
x = ["displacement","power","weight","acceleration","year"]
y = "economy_20mpg"
nfolds = 5
第一种方式:
# Create a k-fold column and append to the cars dataset
# Or you can use an existing fold id column
cars["fold_id"] = cars.kfold_column(n_folds=nfolds, seed=1)
# Train a GBM
cars_gbm = H2OGradientBoostingEstimator(seed=1, fold_column = "fold_id",
keep_cross_validation_fold_assignment=True)
cars_gbm.train(x=x, y=y, training_frame=cars)
# View the fold ids (identical to cars["fold_id"])
print(cars_gbm.cross_validation_fold_assignment())
第二种方式:
# Train a GBM & save fold IDs
cars_gbm = H2OGradientBoostingEstimator(seed=1, nfolds=nfolds,
keep_cross_validation_fold_assignment=True)
cars_gbm.train(x=x, y=y, training_frame=cars)
# View the fold ids
print(cars_gbm.cross_validation_fold_assignment())