随机搜索交叉验证未应用选定参数

希望您能帮助我

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注