使用带有RandomForestClassifier的hyperopt sklearn进行超参数调整

时间:2018-04-17 07:44:51

标签: python machine-learning hyperparameters

我目前正在尝试使用RandomizedSearchCVGridSearchCV来优化超参数。为了进行比较,我想尝试使用hyperopt,也可以hyperopt-sklearnhttps://github.com/hyperopt/hyperopt-sklearn)。不幸的是没有太多文档,所以我不确定如何使用它。

是否有类似的方法来使用hyperopt,就像我现在正在进行随机和网格搜索一样?

skf = StratifiedKFold(n_splits=5, random_state=42)
params_randomSearch = {"min_samples_leaf": np.arange(1,30,1),
              "min_samples_split": np.arange(2,20,1),
              "max_depth": np.arange(2, 20, 1),
              "min_weight_fraction_leaf": np.arange(0. ,0.4, 0.1),
              "max_features" : ['auto', 'sqrt', 'log2', None],
              "criterion" : ['entropy', 'gini']}
scoring = {'Accuracy' : make_scorer(accuracy_score), 'Recall' : 'recall_weighted', 'Kappa' : make_scorer(cohen_kappa_score)}

rs = RandomizedSearchCV(DecisionTreeClassifier(random_state=42), param_distributions=params_randomSearch, scoring = scoring, cv = skf, refit = 'Accuracy', n_iter=150, n_jobs=-1, random_state=42)
        rs.fit(x_train, y_train)
        y_predict = rs.best_estimator_.predict(x_test)
        acc = accuracy_score(y_test, y_predict)

根据github上的文档/示例,它应该是这样的:

estim = HyperoptEstimator(classifier=random_forest('RF1'))
estim.fit(x_train, y_train)

这会导致以下错误:

TypeError: 'generator' object is not subscriptable

我遇到的另一个问题是,是否存在与RandomizedSearchCVGridSearchCV相关的任何集成交叉验证选项?

1 个答案:

答案 0 :(得分:0)

我可以自己解决。似乎与networkx v2存在一些冲突。降级至1.11可解决问题(https://github.com/hyperopt/hyperopt/issues/333