%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22optuna%22%2C%0A%23%20%20%20%20%20%22polars%22%2C%0A%23%20%20%20%20%20%22scikit-learn%22%2C%0A%23%20%20%20%20%20%22yohou%22%2C%0A%23%20%20%20%20%20%22yohou-optuna%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.5%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20OptunaSearchCV%20Quickstart%0A%0A%20%20%20%20In%20this%20notebook%2C%20we%20will%20run%20a%20Bayesian%20hyperparameter%20search%20on%20the%0A%20%20%20%20Air%20Passengers%20dataset%20using%0A%20%20%20%20%5B%60OptunaSearchCV%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou_optuna.search.OptunaSearchCV%2F).%0A%20%20%20%20We%20will%20load%20the%20data%2C%20define%20a%20search%20space%2C%20fit%20the%20search%2C%20inspect%0A%20%20%20%20cross-validation%20results%2C%20and%20generate%20a%20forecast%20with%20the%20best%20model.%0A%0A%20%20%20%20**Prerequisites**%3A%20basic%20familiarity%20with%20scikit-learn's%20fit%2Fpredict%20API%20and%20time%20series%20forecasting%20concepts.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20optuna%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20optuna.distributions%20import%20CategoricalDistribution%2C%20FloatDistribution%0A%20%20%20%20from%20sklearn.linear_model%20import%20Ridge%0A%0A%20%20%20%20from%20yohou.datasets%20import%20fetch_sunspot%0A%20%20%20%20from%20yohou.metrics%20import%20MeanAbsoluteError%0A%20%20%20%20from%20yohou.plotting%20import%20plot_cv_results_scatter%2C%20plot_forecast%2C%20plot_time_series%0A%20%20%20%20from%20yohou.point%20import%20PointReductionForecaster%0A%0A%20%20%20%20from%20yohou_optuna%20import%20OptunaSearchCV%2C%20Sampler%0A%0A%20%20%20%20optuna.logging.set_verbosity(optuna.logging.WARNING)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20CategoricalDistribution%2C%0A%20%20%20%20%20%20%20%20FloatDistribution%2C%0A%20%20%20%20%20%20%20%20MeanAbsoluteError%2C%0A%20%20%20%20%20%20%20%20OptunaSearchCV%2C%0A%20%20%20%20%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20%20%20%20%20Ridge%2C%0A%20%20%20%20%20%20%20%20Sampler%2C%0A%20%20%20%20%20%20%20%20fetch_sunspot%2C%0A%20%20%20%20%20%20%20%20optuna%2C%0A%20%20%20%20%20%20%20%20pl%2C%0A%20%20%20%20%20%20%20%20plot_cv_results_scatter%2C%0A%20%20%20%20%20%20%20%20plot_forecast%2C%0A%20%20%20%20%20%20%20%20plot_time_series%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.%20Load%20and%20Explore%20the%20Data%0A%0A%20%20%20%20We%20use%20the%20Sunspot%20dataset%3A%20monthly%20observations%20of%20sunspot%20activity.%0A%20%20%20%20This%20univariate%20series%20exhibits%20strong%20cyclical%20patterns%20(roughly%2011-year%0A%20%20%20%20solar%20cycles)%2C%20making%20it%20a%20good%20benchmark%20for%20forecasting.%20We%20take%20the%0A%20%20%20%20last%20144%20observations%20to%20keep%20the%20search%20fast.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fetch_sunspot%2C%20plot_time_series)%3A%0A%20%20%20%20y%20%3D%20fetch_sunspot().frame.tail(144)%0A%20%20%20%20plot_time_series(y%2C%20title%3D%22Sunspot%20Activity%20(Last%20144%20Months)%22)%0A%20%20%20%20return%20(y%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20split%20into%20training%20(first%20120%20months)%20and%20test%20(last%2024%20months)%20sets.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(y)%3A%0A%20%20%20%20y_train%20%3D%20y.head(120)%0A%20%20%20%20y_test%20%3D%20y.tail(24)%0A%20%20%20%20return%20y_test%2C%20y_train%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.%20Define%20Forecaster%20and%20Search%20Space%0A%0A%20%20%20%20We%20wrap%20a%20Ridge%20regressor%20in%20a%20%60PointReductionForecaster%60%20and%20define%0A%20%20%20%20distributions%20for%20%60alpha%60%20and%20%60fit_intercept%60.%20The%20TPE%20sampler%20will%0A%20%20%20%20guide%20the%20search%20through%20this%20space.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20CategoricalDistribution%2C%0A%20%20%20%20FloatDistribution%2C%0A%20%20%20%20MeanAbsoluteError%2C%0A%20%20%20%20OptunaSearchCV%2C%0A%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20Ridge%2C%0A%20%20%20%20Sampler%2C%0A%20%20%20%20optuna%2C%0A)%3A%0A%20%20%20%20param_distributions%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22estimator__alpha%22%3A%20FloatDistribution(0.001%2C%20100.0%2C%20log%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%22estimator__fit_intercept%22%3A%20CategoricalDistribution(%5BTrue%2C%20False%5D)%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20forecaster%3DPointReductionForecaster(estimator%3DRidge())%2C%0A%20%20%20%20%20%20%20%20param_distributions%3Dparam_distributions%2C%0A%20%20%20%20%20%20%20%20scoring%3DMeanAbsoluteError()%2C%0A%20%20%20%20%20%20%20%20sampler%3DSampler(sampler%3Doptuna.samplers.TPESampler%2C%20seed%3D42)%2C%0A%20%20%20%20%20%20%20%20n_trials%3D20%2C%0A%20%20%20%20%20%20%20%20cv%3D3%2C%0A%20%20%20%20%20%20%20%20refit%3DTrue%2C%0A%20%20%20%20%20%20%20%20verbose%3D0%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203.%20Run%20the%20Search%0A%0A%20%20%20%20Let's%20fit%20the%20search%20on%20the%20training%20data.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(search%2C%20y_train)%3A%0A%20%20%20%20search.fit(y_train%2C%20forecasting_horizon%3D12)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20search)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20**Best%20parameters%3A**%20%60%7Bsearch.best_params_%7D%60%0A%0A%20%20%20%20%20%20%20%20**Best%20score%20(MAE)%3A**%20%60%7Bsearch.best_score_%3A.4f%7D%60%0A%0A%20%20%20%20%20%20%20%20**Number%20of%20trials%3A**%20%60%7Blen(search.trials_)%7D%60%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204.%20Inspect%20Cross-Validation%20Results%0A%0A%20%20%20%20Let's%20look%20at%20how%20regularization%20strength%20relates%20to%20the%20MAE%20score.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pl%2C%20search)%3A%0A%20%20%20%20results_df%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22trial%22%3A%20list(range(len(search.cv_results_%5B%22params%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22alpha%22%3A%20%5Bp.get(%22estimator__alpha%22%2C%20None)%20for%20p%20in%20search.cv_results_%5B%22params%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22fit_intercept%22%3A%20%5Bp.get(%22estimator__fit_intercept%22%2C%20None)%20for%20p%20in%20search.cv_results_%5B%22params%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean_test_score%22%3A%20search.cv_results_%5B%22mean_test_score%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22std_test_score%22%3A%20search.cv_results_%5B%22std_test_score%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22rank%22%3A%20search.cv_results_%5B%22rank_test_score%22%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).sort(%22rank%22)%0A%20%20%20%20results_df%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plot_cv_results_scatter%2C%20search)%3A%0A%20%20%20%20plot_cv_results_scatter(%0A%20%20%20%20%20%20%20%20search.cv_results_%2C%0A%20%20%20%20%20%20%20%20param_name%3D%22estimator__alpha%22%2C%0A%20%20%20%20%20%20%20%20higher_is_better%3DFalse%2C%0A%20%20%20%20%20%20%20%20title%3D%22CV%20Score%20vs%20Regularization%20Strength%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205.%20Forecast%20with%20the%20Best%20Model%0A%0A%20%20%20%20Since%20we%20set%20%60refit%3DTrue%60%2C%20the%20best%20forecaster%20is%20already%20fitted%20on%20the%0A%20%20%20%20full%20training%20data.%20Let's%20generate%20predictions%20and%20compare%20against%20the%0A%20%20%20%20held-out%20test%20set.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(search)%3A%0A%20%20%20%20y_pred%20%3D%20search.predict(forecasting_horizon%3D12)%0A%20%20%20%20y_pred%0A%20%20%20%20return%20(y_pred%2C)%0A%0A%0A%40app.cell%0Adef%20_(plot_forecast%2C%20y_pred%2C%20y_test%2C%20y_train)%3A%0A%20%20%20%20plot_forecast(%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20y_pred%2C%0A%20%20%20%20%20%20%20%20y_train%3Dy_train%2C%0A%20%20%20%20%20%20%20%20n_history%3D36%2C%0A%20%20%20%20%20%20%20%20title%3D%22Best%20Forecaster%3A%20Predicted%20vs%20Actual%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20We%20Built%0A%0A%20%20%20%20We%20ran%20a%20complete%20hyperparameter%20search%3A%20loaded%20the%20Air%20Passengers%0A%20%20%20%20dataset%2C%20defined%20a%20search%20space%20over%20Ridge%20parameters%2C%20ran%2020%0A%20%20%20%20Bayesian-optimized%20trials%20with%20%60OptunaSearchCV%60%2C%20inspected%0A%20%20%20%20cross-validation%20rankings%2C%20and%20generated%20a%20forecast%20with%20the%20best%20model.%0A%0A%20%20%20%20%23%23%20Next%20Steps%0A%0A%20%20%20%20-%20%5BHow%20to%20Tune%20Composed%20Forecasters%5D(%2Fexamples%2Fcomposed_tuning%2F)%3A%20tune%20forecasters%20with%20feature%20transformers%20like%20%60LagTransformer%60%0A%20%20%20%20-%20%5BHow%20to%20Run%20a%20Multi-Metric%20Search%5D(%2Fexamples%2Fmulti_metric_search%2F)%3A%20track%20multiple%20metrics%20(MAE%2C%20RMSE%2C%20MSE)%20simultaneously%0A%20%20%20%20-%20%5BHow%20to%20Visualize%20Search%20Results%5D(%2Fexamples%2Fsearch_visualization%2F)%3A%20use%20Optuna's%20built-in%20optimization%20plots%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ad2087f8dc35bbfdd92ba7b126e36d28