%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%22yohou%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%20How%20to%20Save%20and%20Load%20Forecasters%0A%0A%20%20%20%20This%20notebook%20shows%20how%20to%20serialize%20a%20fitted%20forecaster%20to%20disk%0A%20%20%20%20and%20reload%20it%20in%20a%20new%20session%20to%20produce%20predictions%20without%20retraining.%0A%0A%20%20%20%20**Prerequisites%3A**%20Familiarity%20with%20fit%2Fpredict%0A%20%20%20%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_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.%20Fit%20a%20Forecaster%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20polars%20as%20pl%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.model_selection%20import%20train_test_split%0A%20%20%20%20from%20yohou.point%20import%20PointReductionForecaster%0A%20%20%20%20from%20yohou.preprocessing%20import%20LagTransformer%0A%0A%20%20%20%20bunch%20%3D%20fetch_sunspot()%0A%20%20%20%20y%20%3D%20bunch.frame.group_by_dynamic(%22time%22%2C%20every%3D%221mo%22).agg(%0A%20%20%20%20%20%20%20%20pl.col(%22sunspot_number%22).mean()%0A%20%20%20%20)%0A%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%20forecaster%20%3D%20PointReductionForecaster(%0A%20%20%20%20%20%20%20%20estimator%3DRidge()%2C%0A%20%20%20%20%20%20%20%20feature_transformer%3DLagTransformer(lag%3D%5B1%2C%206%2C%2012%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%20%3D%20forecaster.predict(forecasting_horizon%3Dforecasting_horizon)%0A%20%20%20%20y_pred.head()%0A%20%20%20%20return%20forecaster%2C%20forecasting_horizon%2C%20y_pred%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.%20Save%20with%20joblib%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(forecaster)%3A%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20joblib%0A%0A%20%20%20%20tmp_dir%20%3D%20Path(tempfile.mkdtemp())%0A%20%20%20%20joblib_path%20%3D%20tmp_dir%20%2F%20%22forecaster.joblib%22%0A%0A%20%20%20%20joblib.dump(forecaster%2C%20joblib_path)%0A%20%20%20%20print(f%22Saved%20to%20%7Bjoblib_path%7D%20(%7Bjoblib_path.stat().st_size%3A%2C%7D%20bytes)%22)%0A%20%20%20%20return%20joblib%2C%20joblib_path%2C%20tmp_dir%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.%20Load%20and%20Predict%20with%20joblib%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(forecasting_horizon%2C%20joblib%2C%20joblib_path%2C%20y_pred)%3A%0A%20%20%20%20loaded%20%3D%20joblib.load(joblib_path)%0A%20%20%20%20y_pred_loaded%20%3D%20loaded.predict(forecasting_horizon%3Dforecasting_horizon)%0A%0A%20%20%20%20%23%20Verify%20predictions%20match%20the%20original%0A%20%20%20%20assert%20y_pred.equals(y_pred_loaded)%2C%20%22Predictions%20should%20be%20identical%22%0A%20%20%20%20print(%22Loaded%20forecaster%20produces%20identical%20predictions.%22)%0A%20%20%20%20y_pred_loaded.head()%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.%20Save%20with%20pickle%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(forecaster%2C%20tmp_dir)%3A%0A%20%20%20%20import%20pickle%0A%0A%20%20%20%20pickle_path%20%3D%20tmp_dir%20%2F%20%22forecaster.pkl%22%0A%0A%20%20%20%20with%20open(pickle_path%2C%20%22wb%22)%20as%20_f%3A%0A%20%20%20%20%20%20%20%20pickle.dump(forecaster%2C%20_f)%0A%0A%20%20%20%20print(f%22Saved%20to%20%7Bpickle_path%7D%20(%7Bpickle_path.stat().st_size%3A%2C%7D%20bytes)%22)%0A%20%20%20%20return%20pickle%2C%20pickle_path%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.%20Load%20and%20Predict%20with%20pickle%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(forecasting_horizon%2C%20pickle%2C%20pickle_path%2C%20y_pred)%3A%0A%20%20%20%20with%20open(pickle_path%2C%20%22rb%22)%20as%20_f%3A%0A%20%20%20%20%20%20%20%20loaded_pkl%20%3D%20pickle.load(_f)%20%20%23%20noqa%3A%20S301%0A%0A%20%20%20%20y_pred_pkl%20%3D%20loaded_pkl.predict(forecasting_horizon%3Dforecasting_horizon)%0A%20%20%20%20assert%20y_pred.equals(y_pred_pkl)%2C%20%22Predictions%20should%20be%20identical%22%0A%20%20%20%20print(%22Pickle-loaded%20forecaster%20produces%20identical%20predictions.%22)%0A%20%20%20%20y_pred_pkl.head()%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%20Security%20Warning%0A%0A%20%20%20%20!!!%20warning%20%22Never%20load%20untrusted%20pickle%20files%22%0A%0A%20%20%20%20Pickle%20files%20can%20execute%20arbitrary%20code%20when%20loaded.%20Only%20load%20files%20you%0A%20%20%20%20created%20yourself%20or%20received%20from%20a%20trusted%20source.%20For%20sharing%20models%0A%20%20%20%20across%20trust%20boundaries%2C%20consider%20exporting%20predictions%20instead%20of%0A%20%20%20%20serialized%20objects.%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%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Next%20Steps%0A%0A%20%20%20%20-%20%5BHow%20to%20Save%20and%20Load%20Forecasters%5D(%2Fpages%2Fhow-to%2Fsave-load-forecasters%2F)%20for%20the%20full%20guide%0A%20%20%20%20-%20%5BForecasting%20Workflow%5D(%2Fpages%2Ftutorials%2Fforecasting-workflow%2F)%20for%20the%20training%20and%20evaluation%20pipeline%20that%20produces%20a%20fitted%20forecaster%0A%20%20%20%20-%20%5BCore%20Concepts%5D(%2Fpages%2Fexplanation%2Fcore-concepts%2F)%20for%20the%20fit%2Fobserve%2Fpredict%20lifecycle%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
10a126e715d3c9784d5066da8ea55274