%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%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%20Class-Probability%20Forecasting%0A%0A%20%20%20%20In%20this%20notebook%2C%20we%20will%20forecast%20air%20quality%20categories%20using%0A%20%20%20%20%5B%60ClassProbaReductionForecaster%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.class_proba.reduction.ClassProbaReductionForecaster%2F)%2C%0A%20%20%20%20producing%20a%20probability%20distribution%20over%20four%20WHO%20air%20quality%20classes.%0A%20%20%20%20We%20load%20a%20real%20Beijing%20PM2.5%20dataset%2C%20fit%20a%20Random%20Forest%20classifier%2C%0A%20%20%20%20and%20evaluate%20the%20probabilistic%20output%20with%20Brier%20score%20and%20accuracy.%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.ensemble%20import%20RandomForestClassifier%0A%0A%20%20%20%20from%20yohou.class_proba%20import%20ClassProbaReductionForecaster%0A%20%20%20%20from%20yohou.compose%20import%20FeaturePipeline%0A%20%20%20%20from%20yohou.datasets%20import%20fetch_air_quality_classification%0A%20%20%20%20from%20yohou.metrics%20import%20Accuracy%2C%20BrierScore%0A%20%20%20%20from%20yohou.preprocessing%20import%20LagTransformer%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20Accuracy%2C%0A%20%20%20%20%20%20%20%20BrierScore%2C%0A%20%20%20%20%20%20%20%20ClassProbaReductionForecaster%2C%0A%20%20%20%20%20%20%20%20FeaturePipeline%2C%0A%20%20%20%20%20%20%20%20LagTransformer%2C%0A%20%20%20%20%20%20%20%20RandomForestClassifier%2C%0A%20%20%20%20%20%20%20%20fetch_air_quality_classification%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%20Inspect%20the%20Data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fetch_air_quality_classification)%3A%0A%20%20%20%20bunch%20%3D%20fetch_air_quality_classification()%0A%20%20%20%20y%20%3D%20bunch.y%0A%20%20%20%20X_actual%20%3D%20bunch.X_actual%0A%20%20%20%20print(f%22Series%20length%3A%20%7Blen(y)%7D%20hours%22)%0A%20%20%20%20print(f%22Classes%3A%20%7Bbunch.classes%7D%22)%0A%20%20%20%20print(f%22Features%3A%20%7Bbunch.feature_names%7D%22)%0A%20%20%20%20y.head(3)%0A%20%20%20%20return%20X_actual%2C%20y%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.%20Train%2FTest%20Split%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_actual%2C%20y)%3A%0A%20%20%20%20from%20yohou.model_selection%20import%20train_test_split%0A%0A%20%20%20%20forecasting_horizon%20%3D%2024%0A%0A%20%20%20%20y_train%2C%20y_test%2C%20X_train%2C%20X_test%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20y%2C%20X_actual%2C%20test_size%3Dforecasting_horizon%0A%20%20%20%20)%0A%0A%20%20%20%20print(f%22Train%3A%20%7Blen(y_train)%7D%20hours%2C%20Test%3A%20%7Blen(y_test)%7D%20hours%22)%0A%20%20%20%20print(%22Test%20class%20distribution%3A%22)%0A%20%20%20%20print(y_test%5B%22air_quality%22%5D.value_counts())%0A%20%20%20%20return%20X_test%2C%20X_train%2C%20forecasting_horizon%2C%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%203.%20Fit%20ClassProbaReductionForecaster%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ClassProbaReductionForecaster%2C%0A%20%20%20%20FeaturePipeline%2C%0A%20%20%20%20LagTransformer%2C%0A%20%20%20%20RandomForestClassifier%2C%0A%20%20%20%20X_train%2C%0A%20%20%20%20forecasting_horizon%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20forecaster%20%3D%20ClassProbaReductionForecaster(%0A%20%20%20%20%20%20%20%20estimator%3DRandomForestClassifier(n_estimators%3D50%2C%20random_state%3D42)%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%2024%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%2C%20X_actual%3DX_train)%0A%20%20%20%20print(%22Fitted%20ClassProbaReductionForecaster%22)%0A%20%20%20%20return%20(forecaster%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%204.%20Predict%20Class%20Probabilities%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20forecaster%2C%20forecasting_horizon)%3A%0A%20%20%20%20y_pred_proba%20%3D%20forecaster.predict_class_proba(%0A%20%20%20%20%20%20%20%20forecasting_horizon%3Dforecasting_horizon%2C%0A%20%20%20%20%20%20%20%20X_actual%3DX_test%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22Probability%20columns%3A%22%2C%20%5Bc%20for%20c%20in%20y_pred_proba.columns%20if%20%22proba%22%20in%20c%5D)%0A%20%20%20%20y_pred_proba%0A%20%20%20%20return%20(y_pred_proba%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%205.%20Evaluate%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Accuracy%2C%20BrierScore%2C%20y_pred_proba%2C%20y_test%2C%20y_train)%3A%0A%20%20%20%20brier%20%3D%20BrierScore()%0A%20%20%20%20brier.fit(y_train)%0A%0A%20%20%20%20accuracy%20%3D%20Accuracy()%0A%20%20%20%20accuracy.fit(y_train)%0A%0A%20%20%20%20brier_score%20%3D%20brier.score(y_test%2C%20y_pred_proba)%0A%20%20%20%20acc_score%20%3D%20accuracy.score(y_test%2C%20y_pred_proba)%0A%0A%20%20%20%20print(f%22Brier%20score%3A%20%7Bbrier_score%3A.3f%7D%20%20(lower%20is%20better)%22)%0A%20%20%20%20print(f%22Accuracy%3A%20%20%20%20%7Bacc_score%3A.3f%7D%20%20(higher%20is%20better)%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%5BClass-Probability%20Forecasting%5D(%2Fpages%2Ftutorials%2Fclass-proba-forecasting%2F)%20for%20the%20full%20guide%0A%20%20%20%20-%20%5BEnsemble%20Forecasting%5D(%2Fpages%2Fhow-to%2Fensemble-forecasting%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
a278144376c4ea6b11e2fd4674529ca2