首页 > 解决方案 > 使用 sklearn RandomizedSearchCV 进行 H2o 参数搜索

问题描述

我正在尝试使用 sklearn 为 h2o 模型优化最佳参数RandomizedSearchCV。代码(取自本文档):

from sklearn.model_selection import RandomizedSearchCV
from sklearn.pipeline import Pipeline
from h2o.estimators.gbm import H2OGradientBoostingEstimator
iris_df = h2o.import_file(path="iris.data")

params = {
          "gbm__ntrees":            [10,20],
          "gbm__max_depth":         [1,2,3],
          "gbm__learn_rate":        [0.1,0.2]
         }

pipeline = Pipeline([("gbm", H2OGradientBoostingEstimator(distribution="gaussian"))])

random_search = RandomizedSearchCV(pipeline, params,
                                   n_iter=5,
                                   scoring="roc_auc",
                                   random_state=42,
                                   n_jobs=1)

random_search.fit(iris_df[1:], iris_df[0])

但它给了我以下错误:

ValueError: Unexpected __getitem__ selector: <class 'numpy.ndarray'> <class 'numpy.ndarray'>.

我尝试了不同的数据集,也尝试通过pandas.DataFrame而不是h2o.frame,它给出了以下内容:

AttributeError: 'DataFrame' object has no attribute 'cbind'

怎么了?h2o现在不兼容sklearn

标签: pythonmachine-learningscikit-learnh2o

解决方案


看起来您正在关注较旧的文档。H2O 有自己的随机网格搜索方法。见下文。

#GBM hyperparameters
gbm_params = {'ntrees': [10, 20], 'max_depth': [1, 2, 3], 'learn_rate': [0.1, 0.2]}

# Search criteria
search_criteria = {'strategy': 'RandomDiscrete', 'max_models': 5, 'seed': 42}

# Train and validate a random grid of GBMs
gbm_grid = H2OGridSearch(model=H2OGradientBoostingEstimator(distribution="gaussian"), grid_id='gbm_grid', hyper_params=gbm_params, search_criteria=search_criteria)
gbm_grid.train(x=predictor_columns, y=response_column, training_frame=iris_df)

有关更多信息,请参阅文档


推荐阅读