{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6471295,"sourceType":"competition"},{"sourceId":6450271,"sourceType":"datasetVersion","datasetId":3706667},{"sourceId":6476642,"sourceType":"datasetVersion","datasetId":3714177}],"dockerImageVersionId":30553,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border: 10px solid #80ffff\"> TABLE OF CONTENTS<br><div>  \n\n* [IMPORTS](#1)\n* [INTRODUCTION](#2)\n* [PREPROCESSING](#3)\n* [EDA AND VISUALS](#4)    \n* [DATA TRANSFORMS](#5)\n* [ML MODELS](#6)\n    * [MODEL INITIATION](#6.1)\n    * [I-O](#6.2)\n    * [TRAINING](#6.3)\n* [SUBMISSION](#7)\n* [NEXT STEPS AND OUTRO](#8)\n    ","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> PACKAGE IMPORTS<br><div> ","metadata":{}},{"cell_type":"code","source":"%%time \n\n# General library imports:-\nfrom IPython.display import display_html, clear_output, Markdown;\nfrom gc import collect;\nfrom itertools import groupby as grp_by;\n\nfrom copy import deepcopy;\nimport pandas as pd;\nimport polars as pl;\nimport polars.selectors as cs;\nimport pandas.api.types;\nfrom typing import Dict, List, Tuple;\nimport numpy as np;\nfrom datetime import datetime;\n\nfrom pprint import pprint;\nimport os;\nfrom functools import partial;\nfrom collections import Counter;\nfrom itertools import product;\nfrom colorama import Fore, Style, init;\nfrom warnings import filterwarnings;\nfilterwarnings('ignore');\n\nfrom tqdm.notebook import tqdm;\nimport seaborn as sns;\n\nimport matplotlib.pyplot as plt;\n%matplotlib inline\n\nclear_output();\nprint();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:36.050567Z","iopub.execute_input":"2023-09-17T16:57:36.051075Z","iopub.status.idle":"2023-09-17T16:57:36.066410Z","shell.execute_reply.started":"2023-09-17T16:57:36.051038Z","shell.execute_reply":"2023-09-17T16:57:36.064978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\n# Importing model and pipeline specifics:-\n\n# Data engineering and pipeline development:-\nfrom sklearn.model_selection import (RepeatedStratifiedKFold as RSKF, \n                                     StratifiedKFold as SKF,\n                                     KFold, \n                                     RepeatedKFold as RKF, \n                                     cross_val_score\n                                    );\n\nfrom sklearn.pipeline import Pipeline, make_pipeline;\nfrom sklearn.base import BaseEstimator, TransformerMixin;\nfrom sklearn.compose import ColumnTransformer;\nfrom sklearn.metrics import average_precision_score as aps;\nfrom sklearn.calibration import CalibrationDisplay as Clb;\n\n# Model development:-\nfrom xgboost import XGBClassifier;\nfrom lightgbm import LGBMClassifier, log_evaluation, early_stopping;\nfrom catboost import CatBoostClassifier;\nfrom sklearn.ensemble import (RandomForestClassifier as RFC,\n                              ExtraTreesClassifier as ETC,\n                              GradientBoostingClassifier as GBC,\n                              HistGradientBoostingClassifier as HGBC,\n                             );\n\n# Ensemble and tuning:-\nimport optuna;\nfrom optuna import Trial, trial, create_study;\nfrom optuna.samplers import TPESampler, CmaEsSampler;\noptuna.logging.set_verbosity = optuna.logging.ERROR;\n\nclear_output();\nprint();\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:36.070834Z","iopub.execute_input":"2023-09-17T16:57:36.071301Z","iopub.status.idle":"2023-09-17T16:57:36.407805Z","shell.execute_reply.started":"2023-09-17T16:57:36.071266Z","shell.execute_reply":"2023-09-17T16:57:36.406496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\n# Defining global configurations and functions:-\n\n# Setting rc parameters in seaborn for plots and graphs- \n# Reference - https://matplotlib.org/stable/tutorials/introductory/customizing.html:-\n# To alter this, refer to matplotlib.rcParams.keys()\n\nsns.set({\"axes.facecolor\"       : \"#ffffff\",\n         \"figure.facecolor\"     : \"#ffffff\",\n         \"axes.edgecolor\"       : \"#000000\",\n         \"grid.color\"           : \"#ffffff\",\n         \"font.family\"          : ['Cambria'],\n         \"axes.labelcolor\"      : \"#000000\",\n         \"xtick.color\"          : \"#000000\",\n         \"ytick.color\"          : \"#000000\",\n         \"grid.linewidth\"       : 0.75,  \n         \"grid.linestyle\"       : \"--\",\n         \"axes.titlecolor\"      : '#0099e6',\n         'axes.titlesize'       : 8.5,\n         'axes.labelweight'     : \"bold\",\n         'legend.fontsize'      : 7.0,\n         'legend.title_fontsize': 7.0,\n         'font.size'            : 7.5,\n         'xtick.labelsize'      : 7.5,\n         'ytick.labelsize'      : 7.5,        \n        });\n\n# Color printing    \ndef PrintColor(text:str, color = Fore.BLUE, style = Style.BRIGHT):\n    \"Prints color outputs using colorama using a text F-string\";\n    print(style + color + text + Style.RESET_ALL); \n\n# Making sklearn pipeline outputs as dataframe:-\nfrom sklearn import set_config; \nset_config(transform_output = \"pandas\");\npd.set_option('display.max_columns', 50);\npd.set_option('display.max_rows', 50);\n\n# Setting global configurations for polars:-\npl.Config.activate_decimals(True).set_tbl_hide_column_data_types(True);\npl.Config(**dict(tbl_formatting = 'ASCII_FULL_CONDENSED',\n                 tbl_hide_column_data_types = True,\n                 tbl_hide_dataframe_shape = True,\n                 fmt_float = \"mixed\",\n                 tbl_cell_alignment = 'CENTER',\n                 tbl_hide_dtype_separator = True,\n                 tbl_cols = 100,\n                 tbl_rows = 100,\n                 fmt_str_lengths = 100,\n                )\n         );\n\nPrintColor(f\"\\n---> Defining the global configuration for polars for all further steps\\n\");\npprint(pl.Config.state(), indent = 10, width = 50);\n\nprint();\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:36.410406Z","iopub.execute_input":"2023-09-17T16:57:36.410900Z","iopub.status.idle":"2023-09-17T16:57:36.745813Z","shell.execute_reply.started":"2023-09-17T16:57:36.410854Z","shell.execute_reply":"2023-09-17T16:57:36.744159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> INTRODUCTION<br><div> ","metadata":{}},{"cell_type":"markdown","source":"| Version<br>Number | Version Details | Best CV score| Single/ Ensemble|LB score|\n| :-: | --- | :-: | :-: |:-:|\n| **V1** |* Used Dr. Carl's **binary classifier** dataset<br> * EDA, plots and secondary features <br>* No scaling <br>* Curated secondary date and rolling features from public work||Ensemble <br> Optuna ||\n| **V2** |* Used Dr. Carl's **multiclass classifier** dataset<br>* No scaling <br>* Curated secondary date and rolling features from public work <br> * Used Random Forest without CV|| ||\n| **V3** |* Used Dr. Carl's **multiclass classifier** dataset<br>* No scaling <br>* Curated secondary date and rolling features from public work <br> * Used Catboost classifier without CV|| ||","metadata":{}},{"cell_type":"markdown","source":"<a id=\"2.1\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > CONFIGURATION PARAMETERS<br><div> \n\n\n| Parameter         | Description                                             | Possible value choices|\n| ---               | ---                                                     | :-:                   |\n|  version_nb       | Version Number                                          | integer               |\n|  gpu_switch       | GPU switch                                              | ON/OFF                |\n|  state            | Random state for most purposes                          | integer               |\n|  target           | Target column name                                      | awake                 |\n|  path             | Path for input data files                               |                       |\n|  alt_path         | Path for alternative data file                           |                       |\n|  dtl_preproc_req  | Proprocessing required                                  | Y/N                   |    \n|  ftre_plots_req   | Feature plots required                                  | Y/N                   |\n|  sec_ftre_req     | Secondary features required                             | Y/N                   |\n|  scale_req        | Scaling required                                        | Y/N                   |\n|  scl_method       | Scaling method                                          | Z/ Robust/ MinMax     |\n|  enc_method       | Encoding method                                         | -                     |\n|  roll_nper        | Periods for roll-windows                                | integer               |\n|  pstprcs_oof      | Post-process OOF after model training                   | Y/N                   |\n|  pstprcs_train    | Post-process OOF during model training for dev-set      | Y/N                   |\n|  ML               | Machine Learning Models                                 | Y/N                   |\n|  n_splits         | Number of CV splits                                     | integer               |\n|  n_repeats        | Number of CV repeats                                    | integer               |\n|  nbrnd_erly_stp   | Number of early stopping rounds                         | integer               |\n|  mdl_cv_mthd      | Model CV method name                                    | RKF/ RSKF/ SKF/ KFold |","metadata":{}},{"cell_type":"markdown","source":"<a id=\"2.2\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > ASSIGNMENT SPECIFICS<br><div> \n    \n**In this assignment, we entail the below-**<br>\n1. This is a time series classifier for sleep state detection<br>\n2. We are provided acceleromater data for the train set and are supposed to make predictions for the sleep state in the test set<br>\n3. The target is converted to a binary state in the alternative dataset<br>\n4. Evaluation metric is derived from the adjutant notebook and is **average precision** over timestamp error tolerance thresholds, averaged over event classes.<br>","metadata":{}},{"cell_type":"code","source":"%%time\n\n# Defining a Configuration class:-\nclass CFG:\n    \"This is a Configuration class for parameters and CV strategy for tuning and training\";\n    \n    # Data preparation:-   \n    version_nb         = 2;\n    \n    # Used during code proto-typing and syntax testing:-    \n    test_req           = \"N\";\n    n_samples          = 1000;\n    \n    gpu_switch         = \"OFF\"; \n    state              = 42;\n    target             = \"awake\";\n    path               = f\"/kaggle/input/child-mind-institute-detect-sleep-states/\";\n    alt_path           = f\"/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train_multi.parquet\";\n    \n    dtl_preproc_req    = \"Y\";\n    ftre_plots_req     = \"Y\";\n    \n    # Data transforms and scaling:-    \n    sec_ftre_req       = \"Y\";\n    scale_req          = \"N\";\n    # NOTE---Keep a value here even if scale_req = N, this is used for linear models:-\n    scl_method         = \"Z\"; \n    enc_method         = 'Label';\n    roll_nper          = 50;\n    \n    # Model Training:- \n    pstprcs_oof        = \"N\";\n    pstprcs_train      = \"N\";\n    ML                 = \"Y\";\n    methods            = ['CBC', 'LGBMC', 'XGBC', 'RFC', 'HGBC'];\n    method             = \"RFC\";\n    n_splits           = 5 ;\n    n_repeats          = 1 ;\n    nbrnd_erly_stp     = 200 ;\n    mdlcv_mthd         = 'RSKF';\n    \n    # Ensemble:-    \n    ensemble_req       = \"Y\";\n    enscv_mthd         = \"RSKF\";\n    metric_obj         = 'maximize';\n    ntrials            = 10 if test_req == \"Y\" else 75;\n    \n    # Global variables for plotting:-\n    grid_specs = {'visible': True, 'which': 'both', 'linestyle': '--', \n                           'color': 'lightgrey', 'linewidth': 0.75};\n    title_specs = {'fontsize': 9, 'fontweight': 'bold', 'color': 'tab:blue'};\n\nprint();\nPrintColor(f\"--> Configuration done!\\n\");\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:36.748086Z","iopub.execute_input":"2023-09-17T16:57:36.748672Z","iopub.status.idle":"2023-09-17T16:57:37.080186Z","shell.execute_reply.started":"2023-09-17T16:57:36.748556Z","shell.execute_reply":"2023-09-17T16:57:37.078780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2.3\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > SCORING METRIC AND ADJUTANT FUNCTIONS<br><div> ","metadata":{}},{"cell_type":"code","source":"%%time \n\ndef ScoreMetric(ytrue: np.array, ypred: np.array) -> np.float32:\n    \"\"\"\n    This function defines the competition evaluation metric\n    This is a temporary metric till I incorporate the competition evaluation metric into the notebook\n    \"\"\";\n    return aps(ytrue, ypred);\n\ndef ScoreLGBM(ytrue: np.array, ypred: np.array):\n    \"\"\"\n    This function defines the interim metric as per LGBM requirements and classifier type\n    \"\"\";\n    return ('AvgPrecision', aps(ytrue, ypred), True);\n\ndef PostProcessPred(ypred: np.array, pp_req : str = \"N\") -> np.array:\n    \"This is an optional post-processing function required in certain assignments\";\n    return ypred;\n\n# Defining commonly used CV strategies for later usage:-\nall_cv= {'KF'  : KFold(n_splits= CFG.n_splits, shuffle = True, random_state= CFG.state),\n         'RKF' : RKF(n_splits= CFG.n_splits, n_repeats = CFG.n_repeats, random_state= CFG.state),\n         'RSKF': RSKF(n_splits= CFG.n_splits, n_repeats = CFG.n_repeats, random_state= CFG.state),\n         'SKF' : SKF(n_splits= CFG.n_splits, shuffle = True, random_state= CFG.state)\n        };\n\ncollect();\nprint();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:37.082175Z","iopub.execute_input":"2023-09-17T16:57:37.082588Z","iopub.status.idle":"2023-09-17T16:57:37.407987Z","shell.execute_reply.started":"2023-09-17T16:57:37.082551Z","shell.execute_reply":"2023-09-17T16:57:37.406725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> PREPROCESSING<br><div> ","metadata":{}},{"cell_type":"code","source":"%time \n\nclass Preprocessor():\n    \"\"\"\n    This class aims to do the below-\n    1. Read the datasets and change the datatype of the target column in the alternative data\n    3. Check information and description\n    4. Check unique values\n    5. Collate starting features \n    6. Curate secondary features based on public work\n    \"\"\";\n    \n    def __init__(self):\n        \"\"\"\n        We do the below here-\n        1. Read in the train data from the alternative data source\n        2. Read in the test and submission file from the competition source\n        3. We sample the training data if necessary upon testing the code semantics\n        \"\"\";\n           \n        self.test     = \\\n        pl.scan_parquet(CFG.path + f\"test_series.parquet\").\\\n        with_columns(pl.col('timestamp').\\\n                     str.strptime(pl.Datetime)\n                    );\n        self.sub_fl   = pl.read_csv(CFG.path + f\"sample_submission.csv\");\n        \n        self.target       = CFG.target;\n        self.test_req     = CFG.test_req;\n        self.sec_ftre_req = CFG.sec_ftre_req;\n        self.nper         = CFG.roll_nper;\n       \n        self.train    = \\\n        pl.scan_parquet(CFG.alt_path).\\\n        with_columns(pl.col(CFG.target).cast(pl.Int8), pl.col('timestamp').\\\n                     str.strptime(pl.Datetime)\n                    );\n        \n        if self.test_req == \"Y\":\n            PrintColor(f\"---> We are testing the code with {CFG.n_samples} data samples per series and target\", \n                       color = Fore.RED);\n            self.train = \\\n            self.train.groupby(['series_id', self.target]).\\\n            agg(pl.all().sample(CFG.n_samples)).\\\n            explode(['step', 'timestamp', 'anglez', 'enmo']);\n           \n        else:\n            PrintColor(f\"---> We are not testing the code- this is an actual and complete run\", color = Fore.RED);           \n                       \n    def PreprocessDF(self):\n        \"\"\"\n        This method ensues basic preprocessing steps for the submission file and collates the starting features\n        \"\"\";\n                \n        PrintColor(f\"\\nSubmission file head\", color = Fore.GREEN);\n        display(self.sub_fl.head(5));   \n        \n        self.strt_ftre = self.train.columns;\n        try:  self.strt_ftre.remove(CFG.target); \n        except: pass;\n        \n        PrintColor(f\"Starting features\");\n        pprint(self.strt_ftre);\n                   \n        return self; \n    \n    def _XformDF(self, X: pl.DataFrame):\n        \"\"\"\n        This method prepares secondary features if requested by the user.\n        We use the date time column and rolling statistics from public work\n        \"\"\"\n        \n        # Curating date features:-        \n        df = \\\n        X.with_columns([pl.col(\"timestamp\").dt.hour().cast(pl.Int8).alias(\"hour_nb\"),\n                        pl.col(\"timestamp\").dt.day().cast(pl.Int8).alias(\"day_nb\"),\n                        pl.col(\"timestamp\").dt.weekday().cast(pl.Int8).alias(\"weekday_nb\"),\n                        pl.col(\"timestamp\").dt.week().cast(pl.Int8).alias(\"week_nb\"),\n                        pl.col(\"timestamp\").dt.month().cast(pl.Int8).alias(\"month_nb\"),\n                        pl.col(\"timestamp\").dt.year().cast(pl.Int16).alias(\"year_nb\"),\n                        (pl.col(\"anglez\") * pl.col(\"enmo\")).alias(\"anglez_enmo\")\n                       ]);\n        \n        # Curating rolling features and window results:-\n        df = \\\n        df.\\\n        with_columns([pl.col(\"anglez\").diff(self.nper).over(\"series_id\").\\\n                      fill_null(strategy = 'backward').alias(\"d_anglez\"),\n                      pl.col(\"enmo\").diff(self.nper).over(\"series_id\").\\\n                      fill_null(strategy = 'backward').alias(\"d_enmo\"),\n                      pl.col(\"anglez\").rolling_mean(self.nper).over(\"series_id\").\\\n                      fill_null(strategy = 'backward').fill_null(strategy = 'forward').alias('ma_anglez'),\n                      pl.col(\"enmo\").rolling_mean(self.nper).over(\"series_id\").\\\n                      fill_null(strategy = 'backward').fill_null(strategy = 'forward').alias('ma_enmo')\n                     ]\n                    ).\\\n        with_columns([pl.col('d_anglez').rolling_mean(self.nper).over(\"series_id\").\\\n                      fill_null(strategy = 'backward').fill_null(strategy = 'forward').alias('ma_d_anglez'),\n                      pl.col('d_enmo').rolling_mean(self.nper).over(\"series_id\").\\\n                      fill_null(strategy = 'backward').fill_null(strategy = 'forward').alias('ma_d_enmo')\n                     ]\n                    );\n            \n        return df;\n    \n    def CurateSecFtre(self):\n        \"This method curates the dataframe with secondary features and returns a modified dataframe\";\n        \n        if self.sec_ftre_req == \"Y\":\n            PrintColor(f\"Secondary features are required\");\n            return self._XformDF(self.train), self._XformDF(self.test);\n        else:\n            PrintColor(f\"Secondary features are not required\", color = Fore.RED);\n            return self.train, self.test;\n               \ncollect();\nprint();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:37.412513Z","iopub.execute_input":"2023-09-17T16:57:37.413088Z","iopub.status.idle":"2023-09-17T16:57:37.769495Z","shell.execute_reply.started":"2023-09-17T16:57:37.413050Z","shell.execute_reply":"2023-09-17T16:57:37.768108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\n# Preprocessinng the data:-\nPrintColor(f\"\\n{'='*30} Data Pre-processing {'='*30}\\n\", Fore.MAGENTA);\npp = Preprocessor();\npp.PreprocessDF();\n\ncollect();\nprint();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:37.771510Z","iopub.execute_input":"2023-09-17T16:57:37.772239Z","iopub.status.idle":"2023-09-17T16:57:38.112711Z","shell.execute_reply.started":"2023-09-17T16:57:37.772192Z","shell.execute_reply":"2023-09-17T16:57:38.111542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> EDA AND VISUALS<br><div>  ","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\" style = \"font-family: Cambria Math;font-size: 115%; color: black; background-color: #e6f9ff; border: dashed black 1.0px; padding: 3.5px\" >\n<b>Note</b> <br>\nThis section is work in progress. I shall update it in a couple of days with relevant insights\n</div>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> DATA TRANSFORMS<br><div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\" style = \"font-family: Cambria Math;font-size: 115%; color: black; background-color: #e6f9ff; border: dashed black 1.0px; padding: 3.5px\" >\n<b>Note</b> <br>\nWe have ensued static transforms in the previous section. We shall implement this herewith\n</div>","metadata":{}},{"cell_type":"code","source":"%%time \n\nif CFG.dtl_preproc_req == \"Y\":\n    # Implementing secondary features:-\n    PrintColor(f\"\\n{'='*30} Data Transformation {'='*30}\\n\", Fore.MAGENTA);\n\n    train, test = pp.CurateSecFtre();\n    print();\n    PrintColor(f\"\\n--> Train data query plan <---\\n\");\n    display(train.show_graph(figsize = (14, 8)));\n\n    # Creating the train-test datasets:-\n    PrintColor(f\"\\n\\n--> Collating the train-test datasets <---\");\n    train, test = train.collect(), test.collect();\n\n    PrintColor(f\"\\n---> Train set after pre-processing\\n\");\n    display(train.head(5));\n    PrintColor(f\"\\n---> Test set after pre-processing\\n\");\n    display(test.head(5));\n\n    PrintColor(f\"\\n---> Train set description after pre-processing\\n\");\n    df = train.describe(percentiles = [0.01, 0.05, 0.90, 0.95, 0.99]).to_pandas();\n    cols = df.iloc[:,0].values.tolist();\n    df = df.iloc[:, 1:].transpose();\n    df.columns = cols;\n    display(df.drop(columns = ['count']).style.format(precision = 2));\n    del df, cols;\n\nprint();\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:38.114111Z","iopub.execute_input":"2023-09-17T16:57:38.114552Z","iopub.status.idle":"2023-09-17T16:57:57.410565Z","shell.execute_reply.started":"2023-09-17T16:57:38.114506Z","shell.execute_reply":"2023-09-17T16:57:57.409646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> ML MODELS<br><div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\" style = \"font-family: Cambria Math;font-size: 115%; color: black; background-color: #e6f9ff; border: dashed black 1.0px; padding: 3.5px\" >\n<b>Strategy</b> <br>\n1. We initialize basic model parameters for ensemble tree and other ML models to start off<br>\n2. We build data structures to store the model predictions and OOF predictions too<br>\n3. We train the models one by one with the chosen strategy<br>\n4. We collate predictions and store them in the prescribed data structures<br>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"6.1\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > MODEL INITIALIZATION<br><div> ","metadata":{}},{"cell_type":"code","source":"%%time \n\n# Initializing model I-O:-\n\nMdl_Master = \\\n{'CBC': CatBoostClassifier(**{'task_type'           : \"GPU\" if CFG.gpu_switch == \"ON\" else \"CPU\",\n                              'random_state'        : CFG.state,\n                              'classes_count'       : 3,\n                              'bagging_temperature' : 0.425,\n                              'colsample_bylevel'   : 0.667,\n                              'iterations'          : 1000,\n                              'learning_rate'       : 0.05,\n                              'od_wait'             : 32,\n                              'max_depth'           : 6,\n                              'l2_leaf_reg'         : 0.45,\n                              'min_data_in_leaf'    : 300,\n                              'random_strength'     : 0.15, \n                              'max_bin'             : 200,\n                              'verbose'             : 0,                        \n                           }\n                         ), \n \n  'LGBMC': LGBMClassifier(**{'device'            : \"gpu\" if CFG.gpu_switch == \"ON\" else \"cpu\",\n                             'objective'         : 'multiclass',\n                             'metric'            : 'none',\n                             'boosting_type'     : 'gbdt',\n                             'random_state'      : CFG.state,\n                             'colsample_bytree'  : 0.675,\n                             'subsample'         : 0.925,\n                             'learning_rate'     : 0.065,\n                             'max_depth'         : 7,\n                             'n_estimators'      : 2000,\n                             'num_leaves'        : 220,\n                             'min_child_samples' : 350,\n                             'reg_alpha'         : 0.01,\n                             'reg_lambda'        : 1.5,\n                             'verbose'           : -1,\n                             \"is_unbalance\"      : True,\n                         }\n                      ),\n\n  'XGBC': XGBClassifier(**{'tree_method'        : \"gpu_hist\" if CFG.gpu_switch == \"ON\" else \"hist\",\n                           'objective'          : 'multi:logistic',\n                           'random_state'       : CFG.state,\n                           'colsample_bytree'   : 0.45,\n                           'learning_rate'      : 0.05,\n                           'max_depth'          : 6,\n                           'n_estimators'       : 1500,                         \n                           'reg_alpha'          : 0.01,\n                           'reg_lambda'         : 1.25,\n                           'min_child_samples'  : 350,\n                        }\n                       ),\n \n  'HGBC': HGBC(learning_rate    = 0.055,\n               max_iter         = 2000,\n               max_depth        = 7,\n               min_samples_leaf = 300,\n               l2_regularization= 1.25,\n               max_bins         = 200,\n               n_iter_no_change = 100,\n               random_state     = CFG.state,\n              ),\n \n  'RFC' : RFC(n_estimators     = 125,\n              criterion        = 'gini',\n              max_depth        = 6,\n              min_samples_leaf = 350,\n              max_features     = \"log2\",\n              n_jobs           = -1,\n              random_state     = CFG.state,\n              verbose          = 0,\n             ),\n \n  \"GBC\" : GBC(n_estimators       = 50,\n              max_depth          = 8,\n              min_samples_leaf   = 350,\n              random_state       = CFG.state,\n             )\n};\n\nprint();\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:57.412267Z","iopub.execute_input":"2023-09-17T16:57:57.412637Z","iopub.status.idle":"2023-09-17T16:57:57.729844Z","shell.execute_reply.started":"2023-09-17T16:57:57.412605Z","shell.execute_reply":"2023-09-17T16:57:57.728666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6.2\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > MODEL I-O INITIALIZATION<br><div> ","metadata":{}},{"cell_type":"code","source":"%%time \n\nif CFG.ML == \"Y\":\n    PrintColor(f\"\\n{'='*30} Initializing model I-O {'='*30}\\n\", Fore.MAGENTA);\n\n    # Initializing output tables for the models:-\n    try: \n        methods   = CFG.methods;\n    except: \n        methods = list(Mdl_Master.keys());\n        PrintColor(f\"---> Check the methods in the configuration class\", Fore.RED);\n\n    # Converting the polars train set to pandas Xtrain, ytrain with selected features:-\n    Xtrain, ytrain = train.drop([CFG.target]).to_pandas().iloc[:, 3:], train[CFG.target].to_pandas();\n    Xtest = test.to_pandas().iloc[:, 3:];\n\n    # Dropping the train polars data to conserve memory:-\n    if CFG.test_req != \"Y\": \n        del train;\n    else: \n        PrintColor(f\"---> Polars train dataframe are not dropped in code-testing\", Fore.RED);\n\n    PrintColor(f\"\\n---> Selected model options- \");\n    pprint(methods, depth = 1, width = 100, indent = 5);\n    \n    PrintColor(f\"\\n---> Dtypes of Xtrain, ytrain = {type(Xtrain)} {type(ytrain)}\");\n    \n    PrintColor(f\"\\n---> Train set features -\");\n    pprint(Xtrain.iloc[0:2,:].columns);\n    \n    PrintColor(f\"\\n---> Test set features -\");\n    pprint(Xtest.iloc[0:2,:].columns);   \n\ncollect();\nprint();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:57.731527Z","iopub.execute_input":"2023-09-17T16:57:57.731953Z","iopub.status.idle":"2023-09-17T16:57:58.066940Z","shell.execute_reply.started":"2023-09-17T16:57:57.731919Z","shell.execute_reply":"2023-09-17T16:57:58.065670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6.3\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > BASELINE MODEL TRAINING<br><div> ","metadata":{}},{"cell_type":"code","source":"%%time \n\n# Implementing the ML models:-\nif CFG.ML == \"Y\":           \n    PrintColor(f\"\\n{'=' * 20} ML training {CFG.method} {'=' * 20}\\n\", color = Fore.MAGENTA);\n    model = Mdl_Master[CFG.method];\n    model.fit(Xtrain, ytrain);\n    \n    del Xtrain, ytrain;\n    \nelse:\n    PrintColor(f\"\\nML models are not needed\\n\", color = Fore.RED);\n    \nprint();\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:57:58.068671Z","iopub.execute_input":"2023-09-17T16:57:58.069095Z","iopub.status.idle":"2023-09-17T16:59:03.757555Z","shell.execute_reply.started":"2023-09-17T16:57:58.069033Z","shell.execute_reply":"2023-09-17T16:59:03.756232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"7\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> SUBMISSION<br><div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\" style = \"font-family: Cambria Math;font-size: 115%; color: black; background-color: #e6f9ff; border: dashed black 1.0px; padding: 3.5px\" >\n<b>Note</b> <br>\nWe follow the submission file curation from the reference notebook.<br>\nWe use pandas for the same, considering the small size of the test set.\n</div>","metadata":{}},{"cell_type":"code","source":"%%time \n\nif CFG.ML == \"Y\":\n    \n    PrintColor(f\"\\n{'=' * 20} Submission file preparation {'=' * 20}\\n\", \n               color = Fore.MAGENTA); \n    \n    # Curating the test set scores:- \n    if isinstance(test, pd.DataFrame) == True:\n        PrintColor(f\"---> Test data is a pandas dataframe\");\n        sub_fl = test.copy(deep = True);\n    else:\n        PrintColor(f\"---> Converting the polars test data into a pandas dataframe\");\n        sub_fl = test.to_pandas();\n    \n    sub_fl['not_awake'] = model.predict_proba(Xtest)[:,0];\n    sub_fl['awake']     = model.predict_proba(Xtest)[:,1];\n    \n    smoothing_length = 2 * 225;\n    sub_fl[\"score\"]  = \\\n    sub_fl[\"awake\"].\\\n    rolling(smoothing_length, center=True).mean().\\\n    fillna(method=\"bfill\").\\\n    fillna(method=\"ffill\");\n\n    sub_fl[\"smooth\"] = \\\n    sub_fl[f\"not_{CFG.target}\"].\\\n    rolling(smoothing_length, center= True).\\\n    mean().\\\n    fillna(method=\"bfill\").\\\n    fillna(method=\"ffill\");\n\n    # Re-binarizing the target:-\n    sub_fl[\"smooth\"] = sub_fl[\"smooth\"].round();\n\n    def get_event(df):\n        lstCV = zip(df.series_id, df.smooth);\n        lstPOI = [];\n        for (c, v), g in grp_by(lstCV, lambda cv: \n                                (cv[0], cv[1]!=0 and not pd.isnull(cv[1]))):\n            llg = sum(1 for item in g);\n            if v is False: \n                lstPOI.extend([0]*llg);\n            else: \n                lstPOI.extend(['onset']+(llg-2)*[0]+['wakeup'] if llg > 1 else [0]);\n        return lstPOI;\n\n    sub_fl[\"event\"] = get_event(sub_fl);\n\n    # Curating the requisite submission file:-\n    sub_fl = \\\n    sub_fl.loc[sub_fl[\"event\"] != 0][[\"series_id\",\"step\",\"event\",\"score\"]].\\\n    copy().\\\n    reset_index(drop=True).\\\n    reset_index(names=\"row_id\");\n    sub_fl.to_csv(f\"submission.csv\", index = None);\n\n    PrintColor(f\"---> Final submission file\");\n    display(sub_fl.head(10).style.format(precision = 2));\n    \nelse:\n    PrintColor(f\"---> Final submission file is not needed\", Fore.RED);\n\nprint();\ncollect();","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:59:03.759303Z","iopub.execute_input":"2023-09-17T16:59:03.759668Z","iopub.status.idle":"2023-09-17T16:59:04.152528Z","shell.execute_reply.started":"2023-09-17T16:59:03.759636Z","shell.execute_reply":"2023-09-17T16:59:04.151071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"8\"></a>\n# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border-bottom: 10px solid #80ffff\"> OUTRO<br><div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\" style = \"font-family: Cambria Math;font-size: 115%; color: black; background-color: #e6f9ff; border: dashed black 1.0px; padding: 3.5px\" >\n<b>References</b> <br>\n1. https://www.kaggle.com/code/carlmcbrideellis/zzzs-random-forest-model-starter <br>\n2. https://www.kaggle.com/datasets/carlmcbrideellis/zzzs-lightweight-training-dataset-target<br>\n3. https://www.kaggle.com/code/satheeshbhukya1/detect-sleep-states<br>\n\n<br>\n<b>Next steps:-</b> <br>   \n1. Incorporating the competition metric for a correct evaluation <br>\n2. We will try other models, especially NNs <br>\n3. Data cleaning and EDA is required to elicit trends and patterns in the existing series <br>\n4. We will try and explore the multi-class dataset too and develop models thereby<br>\n<br>\n</div>","metadata":{}}]}