Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to incorporate pipeline in bayesian_optuna_search? #176

Closed
andreale28 opened this issue Jul 16, 2022 · 4 comments
Closed

How to incorporate pipeline in bayesian_optuna_search? #176

andreale28 opened this issue Jul 16, 2022 · 4 comments
Labels
question Further information is requested

Comments

@andreale28
Copy link

Hi there, recently I have tried skforecast version 0.5.x with _bayesian_optuna_search. In order to make sklearn SVR work properly, I have to add StandardScaler and intend to use Pipeline.

However, after several trials, I cannot implement the pipeline in _bayesian_optuna_search. Are there any solutions?

@JoaquinAmatRodrigo
Copy link
Owner

Hi @andreale28
Could you add a example of code and the output error?

Thanks

@andreale28
Copy link
Author

hi @JoaquinAmatRodrigo , this is my code:

lags_grid = [i for i in range(5, 20, 1)]
steps = 18
pipeline = Pipeline(steps=[
	('scaler', preprocessor),
	('regression', SVR(gamma='scale'))
])
svr_forecaster = ForecasterAutoreg(
		regressor=pipeline,
		lags=5
)
def search_space_svr(trial):
	space = {

		'kernel' : trial.suggest_categorical('kernel', ['linear', 'poly', 'rbf']),
		'degree' : trial.suggest_int('degree', 2, 5),
		'C'      : trial.suggest_float('C', 1e-2, 1e+2, log=True),
		'epsilon': trial.suggest_float('epsilon', 1e-3, 1e-1, log=True),
	}

	return space


results_svr, results_opt_dict_svr = _bayesian_search_optuna(
		forecaster=svr_forecaster,
		y=data_vn.loc [:end_val, 'new_confirmed'],
		exog=data_vn.loc [:end_val, exog_var],
		search_space=search_space_svr,
		lags_grid=lags_grid,
		steps=steps,
		refit=False,
		metric='mean_absolute_percentage_error',
		n_trials=10,
		initial_train_size=len(df_confirmed_train),
		fixed_train_size=False,
		return_best=True,
		verbose=False,
		kwargs_create_study={'direction': 'minimize'},
		kwargs_study_optimize={'gc_after_trial': True}
)
results_svr

The error return is
ValueError: Invalid parameter kernel for estimator Pipeline(steps=[('scaler', StandardScaler()), ('regression', SVR())]). Check the list of available parameters with estimator.get_params().keys().

@JoaquinAmatRodrigo
Copy link
Owner

Hi @andreale28,
The model's name precedes the parameters' name when performing grid search over a sklearn pipeline.
https://joaquinamatrodrigo.github.io/skforecast/0.4.3/notebooks/sklearn-pipeline.html#grid-search

I think the problem may be solved if you use:

`
def search_space_svr(trial):
space = {

	'regression__kernel' : trial.suggest_categorical('kernel', ['linear', 'poly', 'rbf']),
	'regression__degree' : trial.suggest_int('degree', 2, 5),
	'regression__C'      : trial.suggest_float('C', 1e-2, 1e+2, log=True),
	'regression__epsilon': trial.suggest_float('epsilon', 1e-3, 1e-1, log=True),
}

return space

`

@andreale28
Copy link
Author

Hi @andreale28, The model's name precedes the parameters' name when performing grid search over a sklearn pipeline. https://joaquinamatrodrigo.github.io/skforecast/0.4.3/notebooks/sklearn-pipeline.html#grid-search

I think the problem may be solved if you use:

` def search_space_svr(trial): space = {

	'regression__kernel' : trial.suggest_categorical('kernel', ['linear', 'poly', 'rbf']),
	'regression__degree' : trial.suggest_int('degree', 2, 5),
	'regression__C'      : trial.suggest_float('C', 1e-2, 1e+2, log=True),
	'regression__epsilon': trial.suggest_float('epsilon', 1e-3, 1e-1, log=True),
}

return space

`

I will try and report it later, thanks for help

@JavierEscobarOrtiz JavierEscobarOrtiz added the question Further information is requested label Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants