我正在尝试使用 BayesSearchCV 来调整 SGDClassifier 的参数。下面是我尝试过的代码。
import seaborn
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from skopt import BayesSearchCV
from sklearn.linear_model import SGDClassifier
df = 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'
我还使用相同的 model_param 列表测试了 GridSearchCV 和 RandomizedSearchCV 并且它们工作正常。如何正确使用 BayesSearchCV?我必须在哪里更改或必须删除哪个参数?
[更新]
如果我从 model_param 中删除“l1_ratio”,那么上面的代码将起作用。如何执行保持'l1_ratio'?