首页 > 解决方案 > 未应用所选参数的随机搜索 CV

问题描述

我希望你能帮忙

我一直在尝试使用 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)

标签: pythonmachine-learningscikit-learnrandom-forestcross-validation

解决方案


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

你应该问的是

print(best.best_params_)

找到最好的参数,和

print(best.best_estimator_)

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

clf这是一个使用 iris 数据(和名称而不是)的可重现示例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)

这与您报告的基本相同(我在上面已经解释过):只是您的 RF 模型的默认值(因为您没有为 指定任何参数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)

推荐阅读