【发布时间】:2017-08-04 22:09:05
【问题描述】:
如何查看 h2o 中堆叠集成中各种基本模型的系数或重要性?例如,如果我有 GBM、GLM 和 RF,我如何知道每一个在堆叠中的重要性?这可能吗?
例如使用python代码...这里....
http://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/stacked-ensembles.html
【问题讨论】:
如何查看 h2o 中堆叠集成中各种基本模型的系数或重要性?例如,如果我有 GBM、GLM 和 RF,我如何知道每一个在堆叠中的重要性?这可能吗?
例如使用python代码...这里....
http://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/stacked-ensembles.html
【问题讨论】:
H2O 中的 Stacked Ensemble 算法使用 GLM 作为元学习算法,因此您可以将 GLM 元学习器的系数大小解释为每个基学习器在进行集成预测时的“重要性”。
在 Stacked Ensemble 文档中的简单 example 中,我们训练了一个 2 模型(GBM、RF)集成。这就是您在 Python 中检查 metalearner GLM 系数的方式:
import h2o
from h2o.estimators.random_forest import H2ORandomForestEstimator
from h2o.estimators.gbm import H2OGradientBoostingEstimator
from h2o.estimators.stackedensemble import H2OStackedEnsembleEstimator
h2o.init()
# Import a sample binary outcome train/test set into H2O
train = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_train_10k.csv")
test = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_test_5k.csv")
# Identify predictors and response
x = train.columns
y = "response"
x.remove(y)
# For binary classification, response should be a factor
train[y] = train[y].asfactor()
test[y] = test[y].asfactor()
# Number of CV folds (to generate level-one data for stacking)
nfolds = 5
# Generate a 2-model ensemble (GBM + RF)
# Train and cross-validate a GBM
my_gbm = H2OGradientBoostingEstimator(distribution="bernoulli",
ntrees=10,
max_depth=3,
min_rows=2,
learn_rate=0.2,
nfolds=nfolds,
fold_assignment="Modulo",
keep_cross_validation_predictions=True,
seed=1)
my_gbm.train(x=x, y=y, training_frame=train)
# Train and cross-validate a RF
my_rf = H2ORandomForestEstimator(ntrees=50,
nfolds=nfolds,
fold_assignment="Modulo",
keep_cross_validation_predictions=True,
seed=1)
my_rf.train(x=x, y=y, training_frame=train)
# Train a stacked ensemble using the GBM and RF above
ensemble = H2OStackedEnsembleEstimator(base_models=[my_gbm.model_id, my_rf.model_id])
ensemble.train(x=x, y=y, training_frame=train)
# Grab the metalearner GLM fit & print normalized coefficients
metafit = h2o.get_model(ensemble.metalearner()['name'])
metafit.coef_norm()
这将打印以下内容:
{u'DRF_model_python_1502159734743_250': 0.6967886117663271,
u'GBM_model_python_1502159734743_1': 0.48518914691349374,
u'Intercept': 0.1466358030144971}
因此,在这种情况下,随机森林的预测对集成预测的贡献大于 GBM。
如果您在测试集上评估基础模型,您会发现随机森林的性能略好于 GBM,因此集成比 GBM 更喜欢 RF 预测是有道理的(尽管并不总是测试集性能和元学习变量重要性之间的直接 1-1 对应关系,就像这样)。
my_gbm.model_performance(test).auc() # 0.7522498803447679
my_rf.model_performance(test).auc() # 0.7698039263004212
计划expose the metalearner as an argument 以便用户将来可以使用任何 H2O 的监督 ML 算法作为金属学习者,在这种情况下,您可以查看算法的可变重要性以获得相同的信息,因为所有的 H2O 算法都会计算变量的重要性。
【讨论】: