optuna を使って、最適なパラメータを見つけたいです。
python
1print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
(976, 315) (976,) (482, 315) (482,)
データは上の様になっています。
optunaを利用するにあたって、以下のメソッドを組みました。
python
1def objective(trial): 2 3 params = { 4 'alphas': trial.suggest_int('alphas', 1, 100) 5 } 6 model = make_pipeline(RobustScaler(), RidgeCV(cv = kfolds, **params)) 7 model.fit(X_train, y_train) 8 pre = model.predict(X_test) 9 acc = metrics.accuracy_score(pre, y_test) 10 score = 1 - acc 11 return score
objectiveメソッドを使って、
python
1study = optuna.create_study(sampler = optuna.samplers.RandomSampler(seed = 0)) 2study.optimize(objective, n_trials = 100) 3print(study.best_params)
を実行したところ、
text
1 Trial 0 failed because of the following error: TypeError('len() of unsized object') 2Traceback (most recent call last): 3 File "/opt/conda/lib/python3.7/site-packages/optuna/study/_optimize.py", line 213, in _run_trial 4 value_or_values = func(trial) 5 File "/tmp/ipykernel_57/646466574.py", line 7, in objective 6 model.fit(X_train, y_train) 7 File "/opt/conda/lib/python3.7/site-packages/sklearn/pipeline.py", line 335, in fit 8 self._final_estimator.fit(Xt, y, **fit_params_last_step) 9 File "/opt/conda/lib/python3.7/site-packages/sklearn/linear_model/_ridge.py", line 1617, in fit 10 parameters, cv=cv, scoring=self.scoring) 11 File "/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py", line 72, in inner_f 12 return f(**kwargs) 13 File "/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_search.py", line 1184, in __init__ 14 _check_param_grid(param_grid) 15 File "/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_search.py", line 395, in _check_param_grid 16 if len(v) == 0: 17TypeError: len() of unsized object
のエラーが出てしまいます。
違うデータセットなのですが、以下のコードだと動きます。
python
1def objective(trial): 2 params = { 3 'kernel': 'rbf', 4 'C': trial.suggest_loguniform('C', 1e-10, 1e10), 5 'gamma': trial.suggest_loguniform('gamma', 1e-10, 3.0) 6 } 7 model_ = svm.SVC(**params) 8 model_.fit(train_X, train_Y) 9 pre = model_.predict(test_X) 10 acc = metrics.accuracy_score(pre, test_Y) 11 score = 1 - acc 12 return score 13 14study = optuna.create_study(sampler = optuna.samplers.RandomSampler(seed = 0)) 15study.optimize(objective, n_trials = 100) 16print(study.best_params)
そもそも、RidgeをOptunaにかけること自体が間違っているのでしょうか?どなたか、ご教示いただけると有難いです。
回答1件
あなたの回答
tips
プレビュー