希望您能帮助我
我一直在尝试使用scikit-learn的随机搜索功能来调整我的随机森林模型。
如下所示,我提供了几个最大深度和几个叶子样本的选项。
# 创建基础模型model = RandomForestClassifier()# 实例化随机搜索模型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模型。
这里是一个使用iris数据(并使用clf
而不是best
作为名称)的可重现示例:
from sklearn.ensemble import RandomForestClassifierfrom sklearn import datasetsfrom sklearn.model_selection import RandomizedSearchCViris = 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)