不应用所选参数的随机搜索 CV
randomized search CV not applying the selected parameters
希望能帮到你
我一直在尝试使用 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 的默认值(它们将是 被 parameters
).
中的值覆盖
你应该问的是
print(best.best_params_)
找到最佳参数,
print(best.best_estimator_)
找到具有最佳参数的整个 RF 模型。
这是一个使用虹膜数据的可重现示例(名称 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
指定任何参数),加上 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)
希望能帮到你
我一直在尝试使用 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 的默认值(它们将是 被 parameters
).
你应该问的是
print(best.best_params_)
找到最佳参数,
print(best.best_estimator_)
找到具有最佳参数的整个 RF 模型。
这是一个使用虹膜数据的可重现示例(名称 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
指定任何参数),加上 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)