【问题标题】:randomized search CV not applying the selected parameters未应用所选参数的随机搜索 CV
【发布时间】:2019-10-04 04:35:32
【问题描述】:

希望你能帮忙

我一直在尝试使用 scikit learn 中的随机搜索功能调整我的随机森林模型。

如下,我给出了几个最大深度和几个叶子样本的选项。

# Create a based model
model = RandomForestClassifier()

# Instantiate the random search model
best = RandomizedSearchCV(model, {
'bootstrap': [True, False],
'max_depth': [80, 90, 100, 110],
'min_samples_leaf': [3, 4, 5]
}, cv=5, return_train_score=True, iid=True, n_iter = 4)

best.fit(train_features, train_labels.ravel())
print(best.best_score_)
print(best)

但是当我运行它时,我得到了下面的结果,其中最大深度和每片叶子的最小样本设置为不在我的数组中的值。

我在这里做错了什么?

RandomizedSearchCV(cv=5, error_score='raise',
          estimator=RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            **max_depth=None**, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            **min_samples_leaf=1**, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False),
          fit_params=None, iid=True, n_iter=4, n_jobs=1,
          param_distributions={'bootstrap': [True, False], 'max_depth': [80, 90, 100, 110], 'min_samples_leaf': [3, 4, 5]},
          pre_dispatch='2*n_jobs', random_state=None, refit=True,
          return_train_score=True, scoring=None, verbose=0)

【问题讨论】:

    标签: python machine-learning scikit-learn random-forest cross-validation


    【解决方案1】:

    您为RandomizedSearchCV 对象选择的名称best 实际上是用词不当:best 将包含所有参数,而不仅仅是最好的参数,包括您的参数RF 模型,其中一些将在随机搜索期间被实际覆盖。所以,print(best),正如预期的那样,准确地给出了这个结果,即 all 参数值,包括 RF 的默认值,它们实际上不会在这里使用(它们将被 覆盖 em> 由parameters 中的值)。

    你应该问的是

    print(best.best_params_)
    

    为了找到最好的参数,并且

    print(best.best_estimator_)
    

    对于找到最佳参数的整个射频模型。

    这是一个使用虹膜数据的可重现示例(以及名称 clf 而不是 best):

    from sklearn.ensemble import RandomForestClassifier
    from sklearn import datasets
    from sklearn.model_selection import RandomizedSearchCV
    
    iris = datasets.load_iris()
    
    parameters = {
    'bootstrap': [True, False],
    'max_depth': [80, 90, 100, 110],
    'min_samples_leaf': [3, 4, 5]
    }
    
    model = RandomForestClassifier()
    clf = RandomizedSearchCV(model, parameters, cv=5, return_train_score=True, iid=True, n_iter = 4)
    clf.fit(iris.data, iris.target)
    

    请注意,最后一个fit 命令的默认控制台输出,即使没有任何print 请求,也将是:

    RandomizedSearchCV(cv=5, error_score='raise-deprecating',
              estimator=RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
                max_depth=None, max_features='auto', max_leaf_nodes=None,
                min_impurity_decrease=0.0, min_impurity_split=None,
                min_samples_leaf=1, min_samples_split=2,
                min_weight_fraction_leaf=0.0, n_estimators='warn', n_jobs=None,
                oob_score=False, random_state=None, verbose=0,
                warm_start=False),
              fit_params=None, iid=True, n_iter=4, n_jobs=None,
              param_distributions={'max_depth': [80, 90, 100, 110], 'bootstrap': [True, False], 'min_samples_leaf': [3, 4, 5]},
              pre_dispatch='2*n_jobs', random_state=None, refit=True,
              return_train_score=True, scoring=None, verbose=0)
    

    这与您报告的基本相同(我在上面已经解释过):只是您的射频模型的默认值(因为您没有为model 指定任何参数),加上parameters 网格。要选择特定的参数集,您应该使用

    clf.best_params_
    # {'bootstrap': True, 'max_depth': 90, 'min_samples_leaf': 5}
    

    并要求clf.best_estimator_ 确实确认我们得到了具有这些确切参数值的RF:

    RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
                max_depth=90, max_features='auto', max_leaf_nodes=None,
                min_impurity_decrease=0.0, min_impurity_split=None,
                min_samples_leaf=5, min_samples_split=2,
                min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=None,
                oob_score=False, random_state=None, verbose=0,
                warm_start=False)
    

    【讨论】:

      猜你喜欢
      • 2023-02-09
      • 2019-06-19
      • 2021-06-02
      • 2018-11-08
      • 2019-05-11
      • 2019-11-13
      • 1970-01-01
      • 2020-08-01
      • 2021-10-15
      相关资源
      最近更新 更多