在SGDClassifier参数调优过程中BayesSearchCV无法正常工作

我试图使用BayesSearchCV来调整SGDClassifier的参数。以下是我尝试过的代码。

import seabornfrom sklearn.preprocessing import LabelEncoderfrom sklearn.model_selection import train_test_splitfrom skopt import BayesSearchCVfrom sklearn.linear_model import SGDClassifierdf = seaborn.load_dataset("iris")df_features = df.drop(['species'], axis=1)df_target = df[['species']]label_encoder = LabelEncoder()df_target['species'] = list(label_encoder.fit_transform(df['species'].values.tolist()))X_train, X_test, y_train, y_test = train_test_split(df_features, df_target, test_size=0.25, random_state=0)model = SGDClassifier()model_param = {    'penalty': ['l2', 'l1', 'elasticnet'],    'l1_ratio': [0, 0.05, 0.1, 0.2, 0.5, 0.8, 0.9, 0.95, 1],    'loss': ['hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron', 'squared_loss', 'huber',             'epsilon_insensitive', 'squared_epsilon_insensitive'],    'alpha': [10 ** x for x in range(-6, 1)],    'random_state': [0]}opt = BayesSearchCV(model, model_param, n_iter=32, cv=3)opt.fit(X_train, y_train)opt_pred_values = opt.predict(X_test)

这会导致以下错误:

ValueError: invalid literal for int() with base 10: '0.8'

我还测试了GridSearchCV和RandomizedSearchCV,使用相同的model_param列表,这些都正常工作。我如何才能不出现错误地使用BayesSearchCV?我需要更改哪里或删除哪个参数?

[更新]

如果我从model_param中移除’l1_ratio’,那么上面的代码就可以工作。我如何在保留’l1_ratio’的情况下执行代码?


回答:

经过多次参数组合尝试后,我发现如果移除’l1_ratio’,代码就能正常工作。然后我尝试了以下’l1_ratio’设置:

'l1_ratio': [0.0, 0.05, 0.1, 0.2, 0.5, 0.8, 0.9, 0.95, 1.0]'l1_ratio': [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.85, 0.9, 1]'l1_ratio': [10 ** x for x in range(-1, 1)]'l1_ratio': [float(x/10) for x in range(1, 10)]

这些都有效。所以最后我在’l1_ratio’的搜索空间中将0改为0.0,将1改为1.0。

我将解决方案留在这里以备将来之需。或许某天会有人从中受益。

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

发表回复

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