为什么在RandomizedSearchCV的参数’param_distributions’传入字典列表时会报错?

我在文档中看到,参数param_distribution可以接受字典或字典列表。我的代码在传入字典时能够正常工作,但是一旦我传入字典列表就会报错。

from sklearn.model_selection import train_test_splitfrom sklearn.model_selection import RandomizedSearchCVfrom sklearn.linear_model import LogisticRegressionfrom sklearn.datasets import load_breast_cancerimport pandas as pdimport numpy as npbreast_cancer = load_breast_cancer()df = pd.DataFrame(load_breast_cancer().data, columns = breast_cancer.feature_names)df['target'] = pd.Series(load_breast_cancer().target)df.head()Xi = df.iloc[:,:-1]Yi = df.iloc[:,-1]x_train1, x_test1, y_train1, y_test1 = train_test_split(Xi, Yi, train_size = 0.9)dist = [{'C': np.random.uniform(34,89,4)}, {"C": np.random.uniform(2, 16, 5)}]    # {"C": uniform(4, 97)}rcv = RandomizedSearchCV(estimator = LogisticRegression(), cv = 5, scoring= 'roc_auc', n_jobs= 5,                         param_distributions= dist, n_iter = 10)rcv.fit(x_train1, y_train1)

输出:

AttributeError Traceback (most recent call last)

AttributeError: ‘list’ object has no attribute ‘values’

当我用单个字典替换这个字典列表时,我的代码能够正常工作,例如:

dist = {'C': np.random.uniform(34,89,45)}rcv = RandomizedSearchCV(estimator = LogisticRegression(), cv = 5, scoring= 'roc_auc', n_jobs= 5,                         param_distributions= dist, n_iter = 20)rcv.fit(x_train1, y_train1)

输出:

RandomizedSearchCV(cv=5, error_score='raise-deprecating',                   estimator=LogisticRegression(C=1.0, class_weight=None,                                                dual=False, fit_intercept=True,                                                intercept_scaling=1,                                                l1_ratio=None, max_iter=100,                                                multi_class='warn', n_jobs=None,                                                penalty='l2', random_state=None,                                                solver='warn', tol=0.0001,                                                verbose=0, warm_start=False),                   iid='warn', n_iter=20, n_jobs=5,                   param_distributions...       68.32247988, 53.2886396 , 64.71957325, 53.42115708, 66.06577109,       54.09200687, 87.22769322, 81.02240252, 55.25783926, 84.31009298,       71.13884939, 85.74823239, 87.23400718, 54.48527833, 59.49131351,       63.59157499, 38.9348315 , 51.5738502 , 82.72414647, 75.27901268,       42.63960409, 40.65314118, 56.97608301, 66.41059041, 58.37528729])},                   pre_dispatch='2*n_jobs', random_state=None, refit=True,                   return_train_score=False, scoring='roc_auc', verbose=0)

回答:

上面的代码在sklearn版本v0.22.2下能够正常工作,如@SergeyBushmanov所建议。我还需要调整n_iter参数的值以避免警告信息。之前该参数设置为20,因为这个设置引发了那些警告。这些警告也是合理的,因为我有两个超参数(“C”) dist = [{'C': np.random.uniform(34,89,4)}, {"C": np.random.uniform(2, 16, 5)}]。现在,总共有4×5=20种超参数组合需要尝试。n_iter指定了要尝试的组合数量。如果n_iter = 10,这意味着在20种组合中,RandomSearchCV将随机尝试其中的10种超参数值组合。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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