#!/usr/bin/env python # coding: utf-8 # # πŸ‘‰ What is PyCaret? # # PyCaret is an open-source, low-code machine learning library in Python that automates machine learning workflows. It is an end-to-end machine learning and model management tool that speeds up the experiment cycle exponentially and makes you more productive. # # In comparison with the other open-source machine learning libraries, PyCaret is an alternate low-code library that can be used to replace hundreds of lines of code with few words only. This makes experiments exponentially fast and efficient. PyCaret is essentially a Python wrapper around several machine learning libraries and frameworks such as scikit-learn, XGBoost, LightGBM, CatBoost, spaCy, Optuna, Hyperopt, Ray, and many more. # # The design and simplicity of PyCaret is inspired by the emerging role of citizen data scientists, a term first used by Gartner. Citizen Data Scientists are power users who can perform both simple and moderately sophisticated analytical tasks that would previously have required more expertise. Seasoned data scientists are often difficult to find and expensive to hire but citizen data scientists can be an effective way to mitigate this gap and address data-related challenges in the business setting. # # Official Website: https://www.pycaret.org # Documentation: https://pycaret.readthedocs.io/en/latest/ # ![image.png](attachment:image.png) # # πŸ‘‰ Install PyCaret # Installing PyCaret is very easy and takes only a few minutes. We strongly recommend using a virtual environment to avoid potential conflicts with other libraries. PyCaret's default installation is a slim version of pycaret that only installs hard dependencies that are listed in [requirements.txt](https://github.com/pycaret/pycaret/blob/master/requirements.txt). To install the default version: # # - `pip install pycaret` # # When you install the full version of pycaret, all the optional dependencies as listed [here](https://github.com/pycaret/pycaret/blob/master/requirements-optional.txt) are also installed.To install version: # # - `pip install pycaret[full]` # # πŸ‘‰Dataset # In[1]: import pandas as pd import numpy as np data = pd.read_csv('C:/Users/moezs/pycaret-demo-mlflow/store_demand.csv') data['date'] = pd.to_datetime(data['date']) data.head() # In[2]: data.shape # In[3]: # select 1 store and 10 item only for demo-purpose data = data[(data['store'] <= 1) & (data['item'] <= 5)] # In[4]: # extract features data['store'] = ['store_' + str(i) for i in data['store']] data['item'] = ['item_' + str(i) for i in data['item']] data['time_series'] = data[['store', 'item']].apply(lambda x: '_'.join(x), axis=1) data.drop(['store', 'item'], axis=1, inplace=True) data['month'] = [i.month for i in data['date']] data['year'] = [i.year for i in data['date']] data['day_of_week'] = [i.dayofweek for i in data['date']] data['day_of_year'] = [i.dayofyear for i in data['date']] data.head() # In[5]: data['time_series'].nunique() # In[6]: import plotly.express as px for i in data['time_series'].unique()[:3]: subset = data[data['time_series'] == i] subset['moving_average'] = subset['sales'].rolling(30).mean() fig = px.line(subset, x="date", y=["sales","moving_average"], title = i, template = 'plotly_dark') fig.show() # # πŸ‘‰ Training Loop # In[7]: get_ipython().run_cell_magic('time', '', "\nfrom tqdm import tqdm\n\nfrom pycaret.regression import *\n\nall_ts = data['time_series'].unique()\n\nall_results = []\nfinal_model = {}\n\n\nfor i in tqdm(all_ts):\n \n df_subset = data[data['time_series'] == i]\n \n s = setup(df_subset, target = 'sales', session_id = 123, train_size = 0.95,\n data_split_shuffle = False, fold_strategy = 'timeseries', fold = 3,\n ignore_features = ['date', 'time_series'],\n numeric_features = ['day_of_year', 'year'],\n categorical_features = ['month', 'day_of_week'],\n silent = True, verbose = False,\n log_experiment = True, experiment_name = i, log_plots = True)\n \n best_model = compare_models(sort = 'MAE', verbose=False)\n \n p = pull().iloc[0:1]\n p['time_series'] = str(i)\n all_results.append(p)\n \n f = finalize_model(best_model)\n \n final_model[i] = f\n \n save_model(f, model_name='trained_models/' + str(i), verbose=False)\n") # In[8]: concat_results = pd.concat(all_results,axis=0) concat_results.head() # In[9]: get_ipython().system('mlflow ui') # # Create Future DataFrame # In[10]: all_dates = pd.date_range(start='2013-01-01', end = '2019-12-31', freq = 'D') score_df = pd.DataFrame() score_df['date'] = all_dates score_df['month'] = [i.month for i in score_df['date']] score_df['year'] = [i.year for i in score_df['date']] score_df['day_of_week'] = [i.dayofweek for i in score_df['date']] score_df['day_of_year'] = [i.dayofyear for i in score_df['date']] score_df.head() # In[11]: from pycaret.regression import load_model, predict_model all_score_df = [] loaded_models = {} for i in tqdm(data['time_series'].unique()): l = load_model('trained_models/' + str(i), verbose=False) loaded_models[i] = l p = predict_model(l, data=score_df) p['time_series'] = i all_score_df.append(p) # In[12]: concat_df = pd.concat(all_score_df, axis=0) concat_df.head() # In[13]: final_df = pd.merge(concat_df, data, how = 'left', left_on=['date', 'time_series'], right_on = ['date', 'time_series']) final_df.head() # In[14]: final_df.set_index('date', drop=True, inplace=True) all_subs = [] for i in final_df['time_series'].unique(): sub = final_df[final_df['time_series'] == i] sub = sub[['Label','sales']] sub = sub.resample('M').sum() sub['time_series'] = i all_subs.append(sub) concat_sub = pd.concat(all_subs, axis=0) concat_sub['sales'] = concat_sub['sales'].replace(0,np.nan) concat_sub.head() # In[15]: for i in concat_sub['time_series'].unique()[:5]: sub_df = concat_sub[concat_sub['time_series'] == i] import plotly.express as px fig = px.line(sub_df, x=sub_df.index, y=['sales', 'Label'], title=i, template = 'plotly_dark') fig.show() # ## THE END # In[ ]: