python - 未应用所选参数的随机搜索 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)
解决方案
您为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)
推荐阅读
- r - dbFetch,写入另一个表,然后再次 dbFetch?
- javascript - 如何让“dialog.showMessageBoxSync”中的按钮在 Electron.js 中工作
- c# - 用单行语法实例化一个对象并调用返回 C# 或 C++ 中的值的方法?
- java - 如何使状态栏完全变白?
- php - 如何将 WEI 转换为 ETHER php
- c++ - 在矩阵模板类中重载运算符 *= 和 Operator+
- java - 键入字符时 onKeyReleased 不起作用
- c++ - Assimp - 如何使用任何文件格式导入带有纹理的网格?
- apache-ranger - Ranger 增量 AD 同步是否每次都会覆盖或保留以前同步的用户(例如,如果过滤器更改)?Unix用户重新创建?
- bash - 如何使用 bash 将包含“姓氏,名字”的 csv 列分成两个不同的 csv 列?