%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%22scikit-learn%22%2C%0A%23%20%20%20%20%20%22yohou%5Bplotting%5D%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.8%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%20Forecasting%20Workflow%0A%0A%20%20%20%20In%20this%20notebook%2C%20we%20will%20evaluate%20two%20continuous-valued%20forecasters%20using%0A%20%20%20%20temporal%20cross-validation%2C%20search%20for%20the%20best%20hyperparameters%20with%0A%20%20%20%20%5B%60GridSearchCV%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.model_selection.search.GridSearchCV%2F)%2C%0A%20%20%20%20and%20inspect%20residuals%20to%20diagnose%20model%20weaknesses.%0A%0A%20%20%20%20**Prerequisites%3A**%20Completed%20%5BGetting%20Started%5D(%2Fpages%2Ftutorials%2Fgetting-started%2F).%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%20from%20sklearn.linear_model%20import%20Ridge%0A%0A%20%20%20%20from%20yohou.compose%20import%20FeaturePipeline%0A%20%20%20%20from%20yohou.datasets%20import%20fetch_tourism_monthly%0A%20%20%20%20from%20yohou.metrics%20import%20MeanAbsoluteError%2C%20MeanAbsoluteScaledError%0A%20%20%20%20from%20yohou.model_selection%20import%20ExpandingWindowSplitter%2C%20GridSearchCV%0A%20%20%20%20from%20yohou.plotting%20import%20plot_forecast%2C%20plot_residuals%2C%20plot_score_summary%0A%20%20%20%20from%20yohou.point%20import%20PointReductionForecaster%2C%20SeasonalNaive%0A%20%20%20%20from%20yohou.preprocessing%20import%20LagTransformer%0A%20%20%20%20from%20yohou.stationarity%20import%20SeasonalDifferencing%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20ExpandingWindowSplitter%2C%0A%20%20%20%20%20%20%20%20FeaturePipeline%2C%0A%20%20%20%20%20%20%20%20GridSearchCV%2C%0A%20%20%20%20%20%20%20%20LagTransformer%2C%0A%20%20%20%20%20%20%20%20MeanAbsoluteError%2C%0A%20%20%20%20%20%20%20%20MeanAbsoluteScaledError%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%20SeasonalDifferencing%2C%0A%20%20%20%20%20%20%20%20SeasonalNaive%2C%0A%20%20%20%20%20%20%20%20fetch_tourism_monthly%2C%0A%20%20%20%20%20%20%20%20plot_residuals%2C%0A%20%20%20%20%20%20%20%20plot_score_summary%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%20Data%20and%20Fit%20Baselines%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20FeaturePipeline%2C%0A%20%20%20%20LagTransformer%2C%0A%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20Ridge%2C%0A%20%20%20%20SeasonalDifferencing%2C%0A%20%20%20%20SeasonalNaive%2C%0A%20%20%20%20fetch_tourism_monthly%2C%0A)%3A%0A%20%20%20%20from%20yohou.model_selection%20import%20train_test_split%0A%0A%20%20%20%20bunch%20%3D%20fetch_tourism_monthly()%0A%20%20%20%20y%20%3D%20(%0A%20%20%20%20%20%20%20%20bunch.frame%0A%20%20%20%20%20%20%20%20.select(%22time%22%2C%20%22T1__tourists%22)%0A%20%20%20%20%20%20%20%20.drop_nulls()%0A%20%20%20%20%20%20%20%20.rename(%7B%22T1__tourists%22%3A%20%22tourists%22%7D)%0A%20%20%20%20)%0A%20%20%20%20forecasting_horizon%20%3D%2012%0A%20%20%20%20y_train%2C%20y_test%20%3D%20train_test_split(y%2C%20test_size%3Dforecasting_horizon)%0A%0A%20%20%20%20baseline%20%3D%20SeasonalNaive(seasonality%3D12)%0A%20%20%20%20baseline.fit(y_train%2C%20forecasting_horizon%3Dforecasting_horizon)%0A%20%20%20%20y_pred_baseline%20%3D%20baseline.predict(forecasting_horizon%3Dforecasting_horizon)%0A%0A%20%20%20%20forecaster%20%3D%20PointReductionForecaster(%0A%20%20%20%20%20%20%20%20estimator%3DRidge()%2C%0A%20%20%20%20%20%20%20%20target_transformer%3DSeasonalDifferencing(seasonality%3D12)%2C%0A%20%20%20%20%20%20%20%20feature_transformer%3DFeaturePipeline(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22lags%22%2C%20LagTransformer(lag%3D%5B1%2C%202%2C%203%2C%2012%5D))%2C%0A%20%20%20%20%20%20%20%20%5D)%2C%0A%20%20%20%20)%0A%20%20%20%20forecaster.fit(y_train%2C%20forecasting_horizon%3Dforecasting_horizon)%0A%20%20%20%20y_pred_ridge%20%3D%20forecaster.predict(forecasting_horizon%3Dforecasting_horizon)%0A%0A%20%20%20%20print(f%22Train%3A%20%7Blen(y_train)%7D%20months%2C%20Test%3A%20%7Bforecasting_horizon%7D%20months%22)%0A%20%20%20%20y.tail()%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20forecaster%2C%0A%20%20%20%20%20%20%20%20forecasting_horizon%2C%0A%20%20%20%20%20%20%20%20y_pred_baseline%2C%0A%20%20%20%20%20%20%20%20y_pred_ridge%2C%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20y_train%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%202.%20Cross-Validation%20Settings%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20n_splits_slider%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D3%2C%0A%20%20%20%20%20%20%20%20stop%3D6%2C%0A%20%20%20%20%20%20%20%20value%3D3%2C%0A%20%20%20%20%20%20%20%20label%3D%22n_splits%20(CV%20folds)%22%2C%0A%20%20%20%20)%0A%20%20%20%20n_splits_slider%0A%20%20%20%20return%20(n_splits_slider%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.%20Hyperparameter%20Search%20with%20GridSearchCV%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ExpandingWindowSplitter%2C%0A%20%20%20%20GridSearchCV%2C%0A%20%20%20%20MeanAbsoluteScaledError%2C%0A%20%20%20%20forecaster%2C%0A%20%20%20%20forecasting_horizon%2C%0A%20%20%20%20n_splits_slider%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20n_splits%20%3D%20n_splits_slider.value%0A%20%20%20%20cv%20%3D%20ExpandingWindowSplitter(n_splits%3Dn_splits%2C%20test_size%3Dforecasting_horizon)%0A%20%20%20%20search%20%3D%20GridSearchCV(%0A%20%20%20%20%20%20%20%20forecaster%3Dforecaster%2C%0A%20%20%20%20%20%20%20%20param_grid%3D%7B%22estimator__alpha%22%3A%20%5B0.1%2C%201.0%2C%2010.0%2C%20100.0%5D%7D%2C%0A%20%20%20%20%20%20%20%20scoring%3DMeanAbsoluteScaledError(seasonality%3D12)%2C%0A%20%20%20%20%20%20%20%20cv%3Dcv%2C%0A%20%20%20%20)%0A%20%20%20%20search.fit(y_train%2C%20forecasting_horizon%3Dforecasting_horizon)%0A%20%20%20%20print(f%22Best%20params%3A%20%7Bsearch.best_params_%7D%22)%0A%20%20%20%20print(f%22CV%20MASE%3A%20%20%20%20%20%7B-search.best_score_%3A.2f%7D%22)%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.%20Compare%20Models%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20MeanAbsoluteError%2C%0A%20%20%20%20MeanAbsoluteScaledError%2C%0A%20%20%20%20plot_score_summary%2C%0A%20%20%20%20y_pred_baseline%2C%0A%20%20%20%20y_pred_ridge%2C%0A%20%20%20%20y_test%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20mae%20%3D%20MeanAbsoluteError()%0A%20%20%20%20mae.fit(y_train)%0A%20%20%20%20mase%20%3D%20MeanAbsoluteScaledError(seasonality%3D12)%0A%20%20%20%20mase.fit(y_train)%0A%0A%20%20%20%20for%20name%2C%20y_pred%20in%20%5B(%22SeasonalNaive%22%2C%20y_pred_baseline)%2C%20(%22Ridge%22%2C%20y_pred_ridge)%5D%3A%0A%20%20%20%20%20%20%20%20print(f%22%7Bname%3A15s%7D%20%20MAE%3D%7Bmae.score(y_test%2C%20y_pred)%3A.2f%7D%20%20MASE%3D%7Bmase.score(y_test%2C%20y_pred)%3A.2f%7D%22)%0A%0A%20%20%20%20fig_summary%20%3D%20plot_score_summary(%0A%20%20%20%20%20%20%20%20mae%2C%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20%7B%22SeasonalNaive%22%3A%20y_pred_baseline%2C%20%22Ridge%22%3A%20y_pred_ridge%7D%2C%0A%20%20%20%20)%0A%20%20%20%20fig_summary%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.%20Residual%20Diagnostics%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plot_residuals%2C%20y_pred_ridge%2C%20y_test)%3A%0A%20%20%20%20fig_resid%20%3D%20plot_residuals(y_pred_ridge%2C%20y_test)%0A%20%20%20%20fig_resid%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%20Next%20Steps%0A%0A%20%20%20%20-%20%5BForecasting%20Workflow%5D(%2Fpages%2Ftutorials%2Fforecasting-workflow%2F)%20for%20the%20full%20guide%0A%20%20%20%20-%20%5BEvaluate%20Forecast%20Accuracy%5D(%2Fpages%2Fhow-to%2Fevaluate-forecast-accuracy%2F)%20for%20related%20techniques%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
da402201ce84332e8a02a76d66b4c9f6