Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from pycaret.regression import *
all_ts = data['time_series'].unique()
all_results = []
final_model = {}
for i in tqdm(all_ts):
df_subset = data[data['time_series'] == i]
# initialize setup from pycaret.regression
s = setup(df_subset, target = 'sales', train_size = 0.95,
data_split_shuffle = False, fold_strategy = 'timeseries', fold = 3,
ignore_features = ['date', 'time_series'],
numeric_features = ['day_of_year', 'year'],
categorical_features = ['month', 'day_of_week'],
silent = True, verbose = False, session_id = 123)
# compare all models and select best one based on MAE
best_model = compare_models(sort = 'MAE', verbose=False)
# capture the compare result grid and store best model in list
p = pull().iloc[0:1]
p['time_series'] = str(i)
all_results.append(p)
# finalize model i.e. fit on entire data including test set
f = finalize_model(best_model)
# attach final model to a dictionary
final_model[i] = f
# save transformation pipeline and model as pickle file
save_model(f, model_name='trained_models/' + str(i), verbose=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment