希望能帮到你
我一直在尝试使用 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)
最佳答案
您为 RandomizedSearchCV
对象选择的名称 best
实际上用词不当:best
将包含全部参数,不仅是最好的参数,还包括 RF 模型的参数,其中一些参数在随机搜索过程中实际上会被覆盖。因此,print(best)
正如预期的那样,准确地给出了这个结果,即所有参数值,包括 RF 的默认值,这些值实际上不会在这里使用(它们将被参数
中的值覆盖)。
你应该问的是
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)
请注意,即使没有任何 print
请求,最后一个 fit
命令的默认控制台输出也将是:
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
指定任何参数),加上参数
网格。要选择特定的参数集,您应该使用
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)
关于python - 不应用所选参数的随机搜索 CV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176338/