{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"gpu","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"332b96b0","cell_type":"markdown","source":"# Imports","metadata":{}},{"id":"192b37dd","cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split,KFold\nimport joblib\nfrom lightgbm import LGBMClassifier\nfrom catboost import CatBoostClassifier\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import f1_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:15:29.155852Z","iopub.execute_input":"2025-06-19T12:15:29.156109Z","iopub.status.idle":"2025-06-19T12:15:37.163140Z","shell.execute_reply.started":"2025-06-19T12:15:29.156089Z","shell.execute_reply":"2025-06-19T12:15:37.162561Z"}},"outputs":[],"execution_count":null},{"id":"7403ad34","cell_type":"markdown","source":"# Read","metadata":{}},{"id":"276714a2","cell_type":"code","source":"train_=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv')\ntrain_d=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv')\ntest_=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv')\ntest_d=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:15:37.164275Z","iopub.execute_input":"2025-06-19T12:15:37.164804Z","iopub.status.idle":"2025-06-19T12:16:06.958117Z","shell.execute_reply.started":"2025-06-19T12:15:37.164783Z","shell.execute_reply":"2025-06-19T12:16:06.957505Z"}},"outputs":[],"execution_count":null},{"id":"1b019353","cell_type":"code","source":"train=train_.merge(train_d,on='subject',how='left')\ntest=test_.merge(test_d,on='subject',how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:16:06.958797Z","iopub.execute_input":"2025-06-19T12:16:06.958989Z","iopub.status.idle":"2025-06-19T12:16:07.614018Z","shell.execute_reply.started":"2025-06-19T12:16:06.958972Z","shell.execute_reply":"2025-06-19T12:16:07.613325Z"}},"outputs":[],"execution_count":null},{"id":"a71beb4b","cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"id":"9c426dc4","cell_type":"code","source":"# train.drop(['row_id','sequence_id','subject'],axis=1,inplace=True)\n# test.drop(['row_id','sequence_id','subject'],axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:16:07.615774Z","iopub.execute_input":"2025-06-19T12:16:07.616028Z","iopub.status.idle":"2025-06-19T12:16:07.619477Z","shell.execute_reply.started":"2025-06-19T12:16:07.616009Z","shell.execute_reply":"2025-06-19T12:16:07.618602Z"}},"outputs":[],"execution_count":null},{"id":"f2834847","cell_type":"code","source":"train.gesture=train.gesture.replace(['Cheek - pinch skin', 'Forehead - pull hairline',\n       'Write name on leg', 'Feel around in tray and pull out an object',\n       'Neck - scratch', 'Neck - pinch skin', 'Eyelash - pull hair',\n       'Eyebrow - pull hair', 'Forehead - scratch',\n       'Above ear - pull hair', 'Wave hello', 'Write name in air',\n       'Text on phone', 'Pull air toward your face',\n       'Pinch knee/leg skin', 'Scratch knee/leg skin',\n       'Drink from bottle/cup', 'Glasses on/off'],[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:16:07.620298Z","iopub.execute_input":"2025-06-19T12:16:07.620583Z","iopub.status.idle":"2025-06-19T12:16:08.052900Z","shell.execute_reply.started":"2025-06-19T12:16:07.620566Z","shell.execute_reply":"2025-06-19T12:16:08.052066Z"}},"outputs":[],"execution_count":null},{"id":"67424380","cell_type":"code","source":"X=train.drop(['gesture', 'phase', 'behavior', 'orientation', 'sequence_type', 'sequence_id','row_id','sequence_id','subject'],axis=1)\ny_0=train['gesture'].copy()\nX_train_0,X_test_0,y_train_0,y_test_0=train_test_split(X,y_0,test_size=0.2,random_state=21)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:16:08.053620Z","iopub.execute_input":"2025-06-19T12:16:08.053859Z","iopub.status.idle":"2025-06-19T12:16:09.997451Z","shell.execute_reply.started":"2025-06-19T12:16:08.053841Z","shell.execute_reply":"2025-06-19T12:16:09.996894Z"}},"outputs":[],"execution_count":null},{"id":"74866cdd","cell_type":"code","source":"model_0=LGBMClassifier(device= 'gpu',)\nmodel_0.fit(X_train_0,y_train_0)\ny_pred_0=model_0.predict(X_test_0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:16:09.998131Z","iopub.execute_input":"2025-06-19T12:16:09.998406Z","iopub.status.idle":"2025-06-19T12:18:31.672213Z","shell.execute_reply.started":"2025-06-19T12:16:09.998382Z","shell.execute_reply":"2025-06-19T12:18:31.671554Z"}},"outputs":[],"execution_count":null},{"id":"f986aa04","cell_type":"code","source":"f1_score(y_pred_0,y_test_0,average='micro')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:18:44.456877Z","iopub.execute_input":"2025-06-19T12:18:44.457670Z","iopub.status.idle":"2025-06-19T12:18:44.486192Z","shell.execute_reply.started":"2025-06-19T12:18:44.457618Z","shell.execute_reply":"2025-06-19T12:18:44.485673Z"}},"outputs":[],"execution_count":null},{"id":"bc7eda93","cell_type":"code","source":"model_0.fit(X,y_0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:18:50.862478Z","iopub.execute_input":"2025-06-19T12:18:50.862810Z","iopub.status.idle":"2025-06-19T12:21:06.557932Z","shell.execute_reply.started":"2025-06-19T12:18:50.862786Z","shell.execute_reply":"2025-06-19T12:21:06.557140Z"}},"outputs":[],"execution_count":null},{"id":"9fb50255","cell_type":"code","source":"def predict(sequence: pd.DataFrame, demographics: pd.DataFrame) -> str:\n    # Convert from Polars to Pandas if needed\n    if not isinstance(sequence, pd.DataFrame):\n        sequence = sequence.to_pandas()\n    if not isinstance(demographics, pd.DataFrame):\n        demographics = demographics.to_pandas()\n    \n    # Merge with demographics\n    sequence = sequence.merge(demographics, on=\"subject\", how=\"left\")\n    \n    # Store sequence_id if it exists\n    sequence_id = None\n    if 'sequence_id' in sequence.columns:\n        sequence_id = sequence['sequence_id'].iloc[0]\n    \n    # Drop unnecessary columns but keep sequence_id for now\n    drop_cols = ['row_id', 'subject','sequence_id']\n    sequence = sequence.drop(columns=[col for col in drop_cols if col in sequence.columns])\n    \n    # Drop sequence_id before prediction if it exists\n    if 'sequence_id' in sequence.columns:\n        sequence = sequence.drop('sequence_id', axis=1)\n    \n    # Make prediction using our trained model\n    prediction = model_0.predict(sequence)\n    \n    # Get the most common prediction (mode)\n    mode_pred = int(pd.Series(prediction).mode()[0])\n    \n    # Map back to gesture labels\n    gesture_mapping = {\n        0: 'Cheek - pinch skin',\n        1: 'Forehead - pull hairline',\n        2: 'Write name on leg',\n        3: 'Feel around in tray and pull out an object',\n        4: 'Neck - scratch',\n        5: 'Neck - pinch skin',\n        6: 'Eyelash - pull hair',\n        7: 'Eyebrow - pull hair',\n        8: 'Forehead - scratch',\n        9: 'Above ear - pull hair',\n        10: 'Wave hello',\n        11: 'Write name in air',\n        12: 'Text on phone',\n        13: 'Pull air toward your face',\n        14: 'Pinch knee/leg skin',\n        15: 'Scratch knee/leg skin',\n        16: 'Drink from bottle/cup',\n        17: 'Glasses on/off'\n    }\n    \n    return gesture_mapping.get(mode_pred, 'Text on phone')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:25:28.559218Z","iopub.execute_input":"2025-06-19T12:25:28.559941Z","iopub.status.idle":"2025-06-19T12:25:28.566188Z","shell.execute_reply.started":"2025-06-19T12:25:28.559920Z","shell.execute_reply":"2025-06-19T12:25:28.565497Z"}},"outputs":[],"execution_count":null},{"id":"191a580b","cell_type":"code","source":"# results = []\n# for sequence_id, group in test.groupby('sequence_id'):\n#     sequence = group.drop(columns=['sequence_id'])  # Extract features\n#     gesture = predict(sequence, test_d)\n#     results.append([sequence_id, gesture])\n    \n# predictions_df = pd.DataFrame(results, columns=['sequence_id', 'gesture'])\n# predictions_df.to_parquet('submission.parquet', index=False)\n# print(predictions_df)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:21:22.008947Z","iopub.execute_input":"2025-06-19T12:21:22.009595Z","iopub.status.idle":"2025-06-19T12:21:22.103222Z","shell.execute_reply.started":"2025-06-19T12:21:22.009573Z","shell.execute_reply":"2025-06-19T12:21:22.102655Z"}},"outputs":[],"execution_count":null},{"id":"41acc85c","cell_type":"code","source":"import kaggle_evaluation.cmi_inference_server\nimport os\ninference_server = kaggle_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    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-19T12:25:35.589808Z","iopub.execute_input":"2025-06-19T12:25:35.590297Z","iopub.status.idle":"2025-06-19T12:25:56.710280Z","shell.execute_reply.started":"2025-06-19T12:25:35.590275Z","shell.execute_reply":"2025-06-19T12:25:56.709492Z"}},"outputs":[],"execution_count":null}]}