<jupyter_start><jupyter_text>Large Scale learning with ModelSelectorVery often we have many different products, regions, countries, shops...for which we need to delivery forecast. This can be easily done with `ModelSelector`. `ModelSelector` though does not bind you to use multiple data partitions and can also serve as convenient layer for accessing relevant information quickly.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = [12, 6]
from hcrystalball.model_selection import ModelSelector
from hcrystalball.utils import get_sales_data
from hcrystalball.wrappers import get_sklearn_wrapper, ThetaWrapper
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor<jupyter_output><empty_output><jupyter_text>Get Dummy Data<jupyter_code>df = get_sales_data(n_dates=365*2, 
                    n_assortments=1, 
                    n_states=1, 
                    n_stores=2)
df.head()
# let's start simple
df_minimal = df[['Store','Sales']]<jupyter_output><empty_output><jupyter_text>Get predefined sklearn models `ModelSelector` has already predefined large scale of hcrystalball models by their classes. To get this predefined gridsearch use `create_gridsearch` method. It will allow you to create hundereds of different models in a second. Here for the sake of time, we will use the advantage of the method for cv splits, default scorer etc. and just extend empty grid with two models<jupyter_code>ms_minimal = ModelSelector(horizon=10, frequency='D')
# see full default parameter grid in hands on exercise
ms_minimal.create_gridsearch(
    sklearn_models=False,
    n_splits=2,
    between_split_lag=None,
    sklearn_models_optimize_for_horizon=False,
    autosarimax_models=False,
    prophet_models=False,
    theta_models=False,
    tbats_models=False,
    exp_smooth_models=False,
    average_ensembles=False,
    stacking_ensembles=False)<jupyter_output><empty_output><jupyter_text>Extend with custom models<jupyter_code>ms_minimal.add_model_to_gridsearch(get_sklearn_wrapper(LinearRegression))
ms_minimal.add_model_to_gridsearch(get_sklearn_wrapper(RandomForestRegressor, random_state=42))
ms_minimal.add_model_to_gridsearch(ThetaWrapper())<jupyter_output><empty_output><jupyter_text>Run model selectionMethod `select_model` is doing majority of the magic for you - it creates forecast for each combination of columns specified in `partition_columns` and for each of the time series it will run grid_search mentioned above. Optionally once can select list of columns over which the model selection will run in parallel using prefect (`parallel_over_columns`). Required format for data is Datetime index, unsuprisingly numerical column for `target_col_name` all other columns except `partition_columns` will be used as exogenous variables - as additional features for modeling.<jupyter_code>ms_minimal.select_model(df=df_minimal, partition_columns=['Store'], target_col_name='Sales')
ms_minimal.plot_results(plot_from='2015-06');<jupyter_output><empty_output><jupyter_text>Persist and Load`ModelSelector` stores multiple `ModelSelectorResults` in given folder as pickle files. As we only have 1 partition, only 1 file is written and loaded back.<jupyter_code>ms_minimal
ms_minimal.persist_results('results')
from hcrystalball.model_selection import load_model_selector
ms_loaded = load_model_selector('results')
ms_loaded
# cleanup
import shutil
try:
    shutil.rmtree('results')
except:
    pass<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06696679797411367, 'fraction_numerical': 0.006471581316826111, 'mean_word_length': 6.484210526315789, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '288790', 'license': 'MIT', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1016, 'n_words': 312, 'path': 'docs/examples/tutorial/model_selection/01_model_selector_basic.ipynb', 'repo_name': 'pavelg087/hcrystalball', 'size': '3554'}