{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"},{"sourceId":244130716,"sourceType":"kernelVersion"},{"sourceId":244134226,"sourceType":"kernelVersion"}],"dockerImageVersionId":31041,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **FOREWORD**\n\nThis is my submission kernel from the training kernel [here](https://www.kaggle.com/code/ravi20076/cmi2025-public-baseline-train-v4)\n\nI load the trained models and feature engineering script and infer and submit here","metadata":{}},{"cell_type":"markdown","source":"# **IMPORTS**","metadata":{}},{"cell_type":"code","source":"%%time \n\n!pip install -q polars==1.29.0      --no-index --find-links=/kaggle/input/cmi2025-public-imports-v3/packages\n!pip install -q xgboost==3.0.1      --no-index --find-links=/kaggle/input/cmi2025-public-imports-v3/packages\n!pip install -q lightgbm==4.6.0     --no-index --find-links=/kaggle/input/cmi2025-public-imports-v3/packages\n!pip install -q scikit-learn==1.6.1 --no-index --find-links=/kaggle/input/cmi2025-public-imports-v3/packages\n!pip install -q tabpfn              --no-index --find-links=/kaggle/input/cmi2025-public-imports-v3/packages\n!pip install -q pytorch_tabnet      --no-index --find-links=/kaggle/input/cmi2025-public-imports-v3/packages\n\nexec( open(f\"/kaggle/input/cmi2025-public-imports-v3/myimports.py\", \"r\").read() )\nexec( open(f\"/kaggle/input/cmi2025-public-imports-v3/myutils.py\", \"r\").read() )\nexec( open(f\"/kaggle/input/cmi2025-public-imports-v3/training.py\", \"r\").read() )\n\nprint()","metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-06-06T21:30:43.439893Z","iopub.execute_input":"2025-06-06T21:30:43.4401Z","iopub.status.idle":"2025-06-06T21:34:19.974855Z","shell.execute_reply.started":"2025-06-06T21:30:43.44008Z","shell.execute_reply":"2025-06-06T21:34:19.973689Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **CONFIGURATION**","metadata":{}},{"cell_type":"code","source":"%%time\n\nclass CFG:\n    \"\"\"\n    Configuration class for parameters and CV strategy for tuning and training\n    Some parameters may be unused here as this is a general configuration class\n    \"\"\";\n\n    # Data preparation:-\n    version_nb         = 4\n    model_id           = \"V4_1\"\n    model_label        = \"ML\"\n    test_req           = False\n    test_iter          = 150\n    gpu_switch         = \"ON\" if torch.cuda.is_available() else \"OFF\"\n    state              = 42\n    \n    target             = f'gesture'\n    grouper            = f\"subject\"\n    tgt_mapper         = {\n                            \"Above ear - pull hair\" : 0,\n                            \"Cheek - pinch skin\" : 1,\n                            \"Eyebrow - pull hair\" : 2,\n                            \"Eyelash - pull hair\" : 3, \n                            \"Forehead - pull hairline\" : 4,\n                            \"Forehead - scratch\" : 5,\n                            \"Neck - pinch skin\" : 6, \n                            \"Neck - scratch\" : 7,\n                            \n                            \"Drink from bottle/cup\" : 8,\n                            \"Feel around in tray and pull out an object\" : 9,\n                            \"Glasses on/off\" : 10,\n                            \"Pinch knee/leg skin\" : 11, \n                            \"Pull air toward your face\" : 12,\n                            \"Scratch knee/leg skin\" : 13,\n                            \"Text on phone\" : 14,\n                            \"Wave hello\" : 15,\n                            \"Write name in air\" : 16,\n                            \"Write name on leg\" : 17,\n                        }\n    \n    ip_path            = f\"/kaggle/input/cmi-detect-behavior-with-sensor-data\"\n    op_path            = f\"/kaggle/working\"\n    orig_path          = f\"\"\n    data_path          = f\"/kaggle/input/cmi2025-public-baseline-train-v4\"\n    dtl_preproc_req    = True\n    ftre_plots_req     = True\n    ftre_imp_req       = True\n    nb_orig            = 0\n    orig_all_folds     = False\n\n    # Model Training:-\n    pstprcs_oof        = False\n    pstprcs_train      = False\n    pstprcs_test       = False\n    ML                 = True\n    test_preds_req     = True\n    n_splits           = 5\n    n_repeats          = 1\n    nbrnd_erly_stp     = 100\n    mdlcv_mthd         = 'SGKF'\n    metric_obj         = 'maximize'\n\n    # Global variables for plotting:-\n    grid_specs = {'visible'  : True,\n                  'which'    : 'both',\n                  'linestyle': '--',\n                  'color'    : 'lightgrey',\n                  'linewidth': 0.75\n                 }\n\n    title_specs = {'fontsize'   : 9,\n                   'fontweight' : 'bold',\n                   'color'      : '#992600',\n                  }\n\ncollect()","metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-06-06T21:34:26.74407Z","iopub.execute_input":"2025-06-06T21:34:26.744444Z","iopub.status.idle":"2025-06-06T21:34:26.982984Z","shell.execute_reply.started":"2025-06-06T21:34:26.744409Z","shell.execute_reply":"2025-06-06T21:34:26.982013Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **PREPROCESSING**","metadata":{}},{"cell_type":"code","source":"%%time \n\ntest     = pl.read_csv(f\"{CFG.ip_path}/test.csv\")\nsel_cols = [ c for c in test.collect_schema().names() if \"thm\" not in c and \"tof\" not in c ]\n\nexec( open( f\"{CFG.data_path}/myfe.py\", \"r\").read())\n\nfitted_models = joblib.load(\n    f\"{CFG.data_path}/FittedModels{CFG.model_label}{CFG.model_id}.joblib\"\n)\n\nvmms = []\nfor method, estimators in fitted_models.items() :\n    vmm = VotingModelMaker(estimators, method, problem_type = \"multiclass\", as_numpy = False)\n    vmms.append(vmm)","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-06-06T21:34:36.564712Z","iopub.execute_input":"2025-06-06T21:34:36.565028Z","iopub.status.idle":"2025-06-06T21:34:41.848706Z","shell.execute_reply.started":"2025-06-06T21:34:36.565006Z","shell.execute_reply":"2025-06-06T21:34:41.847809Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **INFERENCING**","metadata":{}},{"cell_type":"code","source":"%%time \n\nimport kaggle_evaluation.cmi_inference_server\n\ndef predict(\n    sequence     : pl.DataFrame, \n    demographics : pl.DataFrame,\n) -> str:\n    \"\"\"\n    Prediction function for Kaggle evaluation\n    \"\"\"\n\n    Xtest     = make_ftre(sequence, demographics)\n    Mdl_Preds = []\n    for model in vmms:\n        Mdl_Preds.append(model.predict_proba(Xtest))\n\n    pred = \\\n    (\n        pd.concat(Mdl_Preds, axis = 0, ignore_index = False).\n        groupby(level = 0).\n        mean().\n        idxmax(axis=1).\n        map({v: k for k, v in CFG.tgt_mapper.items()}).\n        values[0]\n    )\n    \n    print(f\"Final prediction = {pred}\")\n    return pred","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-06-06T21:34:45.665935Z","iopub.execute_input":"2025-06-06T21:34:45.666485Z","iopub.status.idle":"2025-06-06T21:34:45.997472Z","shell.execute_reply.started":"2025-06-06T21:34:45.666453Z","shell.execute_reply":"2025-06-06T21:34:45.996595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time \n\ninference_server = \\\nkaggle_evaluation.cmi_inference_server.CMIInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            '/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv',\n            '/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv',\n        )\n    )\n\nprint()","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-06-06T21:34:48.313385Z","iopub.execute_input":"2025-06-06T21:34:48.314889Z","iopub.status.idle":"2025-06-06T21:34:49.784701Z","shell.execute_reply.started":"2025-06-06T21:34:48.31485Z","shell.execute_reply":"2025-06-06T21:34:49.783918Z"}},"outputs":[],"execution_count":null}]}