{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":12481184,"sourceType":"datasetVersion","datasetId":7875324},{"sourceId":12481187,"sourceType":"datasetVersion","datasetId":7875327}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:33:57.643141Z","iopub.execute_input":"2025-07-15T21:33:57.643418Z","iopub.status.idle":"2025-07-15T21:33:58.088226Z","shell.execute_reply.started":"2025-07-15T21:33:57.643398Z","shell.execute_reply":"2025-07-15T21:33:58.086851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport os\nimport polars as pl\n\nimport kaggle_evaluation.cmi_inference_server","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:33:58.090327Z","iopub.execute_input":"2025-07-15T21:33:58.090947Z","iopub.status.idle":"2025-07-15T21:34:00.103031Z","shell.execute_reply.started":"2025-07-15T21:33:58.090917Z","shell.execute_reply":"2025-07-15T21:34:00.102026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv\")\ntrain = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv\")\ntest = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:00.104032Z","iopub.execute_input":"2025-07-15T21:34:00.104595Z","iopub.status.idle":"2025-07-15T21:34:39.147810Z","shell.execute_reply.started":"2025-07-15T21:34:00.104523Z","shell.execute_reply":"2025-07-15T21:34:39.146754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Shape of the data:\nprint(\"train_data :\", train_data.shape)\nprint(\"train :\", train.shape)\nprint(\"test_data :\", test_data.shape)\nprint(\"test :\", test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.149663Z","iopub.execute_input":"2025-07-15T21:34:39.150013Z","iopub.status.idle":"2025-07-15T21:34:39.154922Z","shell.execute_reply.started":"2025-07-15T21:34:39.149991Z","shell.execute_reply":"2025-07-15T21:34:39.153834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.156039Z","iopub.execute_input":"2025-07-15T21:34:39.156331Z","iopub.status.idle":"2025-07-15T21:34:39.222509Z","shell.execute_reply.started":"2025-07-15T21:34:39.156304Z","shell.execute_reply":"2025-07-15T21:34:39.221601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['gesture'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.223807Z","iopub.execute_input":"2025-07-15T21:34:39.224080Z","iopub.status.idle":"2025-07-15T21:34:39.281277Z","shell.execute_reply.started":"2025-07-15T21:34:39.224057Z","shell.execute_reply":"2025-07-15T21:34:39.280185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['sequence_id'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.282366Z","iopub.execute_input":"2025-07-15T21:34:39.282670Z","iopub.status.idle":"2025-07-15T21:34:39.340707Z","shell.execute_reply.started":"2025-07-15T21:34:39.282639Z","shell.execute_reply":"2025-07-15T21:34:39.339830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.341720Z","iopub.execute_input":"2025-07-15T21:34:39.342073Z","iopub.status.idle":"2025-07-15T21:34:39.363689Z","shell.execute_reply.started":"2025-07-15T21:34:39.342044Z","shell.execute_reply":"2025-07-15T21:34:39.362826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.364677Z","iopub.execute_input":"2025-07-15T21:34:39.365065Z","iopub.status.idle":"2025-07-15T21:34:39.400120Z","shell.execute_reply.started":"2025-07-15T21:34:39.365036Z","shell.execute_reply":"2025-07-15T21:34:39.399231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.402819Z","iopub.execute_input":"2025-07-15T21:34:39.403099Z","iopub.status.idle":"2025-07-15T21:34:39.425742Z","shell.execute_reply.started":"2025-07-15T21:34:39.403077Z","shell.execute_reply":"2025-07-15T21:34:39.424745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = train_data.merge(train,on='subject',how='left')\ntest_data = test_data.merge(test,on='subject',how='left')\ntrain_data.shape, test_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:39.427021Z","iopub.execute_input":"2025-07-15T21:34:39.427391Z","iopub.status.idle":"2025-07-15T21:34:40.187204Z","shell.execute_reply.started":"2025-07-15T21:34:39.427359Z","shell.execute_reply":"2025-07-15T21:34:40.186092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Encode target\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder\ntarget_le = LabelEncoder()\ntrain_data['gesture'] = target_le.fit_transform(train_data['gesture'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:40.188312Z","iopub.execute_input":"2025-07-15T21:34:40.188827Z","iopub.status.idle":"2025-07-15T21:34:40.349564Z","shell.execute_reply.started":"2025-07-15T21:34:40.188639Z","shell.execute_reply":"2025-07-15T21:34:40.348593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#cols = test_data.columns\ncols = test_data.columns.tolist()\nlen(cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:40.350708Z","iopub.execute_input":"2025-07-15T21:34:40.351112Z","iopub.status.idle":"2025-07-15T21:34:40.357967Z","shell.execute_reply.started":"2025-07-15T21:34:40.351083Z","shell.execute_reply":"2025-07-15T21:34:40.357154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX = train_data[cols]\nX = train_data.drop(['gesture', 'phase', 'behavior', 'orientation', 'sequence_type', 'sequence_id','row_id','sequence_id','subject'],axis=1)\ny = train_data['gesture']\ntest = test_data.drop(['sequence_id','row_id','sequence_id','subject'],axis=1)\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:40.358561Z","iopub.execute_input":"2025-07-15T21:34:40.358858Z","iopub.status.idle":"2025-07-15T21:34:44.004416Z","shell.execute_reply.started":"2025-07-15T21:34:40.358832Z","shell.execute_reply":"2025-07-15T21:34:44.003338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\nmodel = LGBMClassifier(verbosity=-1)\nmodel.fit(X,y)\npreds = model.predict(test)\n\npredictions = target_le.inverse_transform(preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:34:44.005456Z","iopub.execute_input":"2025-07-15T21:34:44.005839Z","iopub.status.idle":"2025-07-15T21:49:14.351246Z","shell.execute_reply.started":"2025-07-15T21:34:44.005748Z","shell.execute_reply":"2025-07-15T21:49:14.349905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import joblib\n\n# Save trained model and label encoder\njoblib.dump(model, 'model.pkl')\njoblib.dump(target_le, 'label_encoder.pkl')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:49:14.352954Z","iopub.execute_input":"2025-07-15T21:49:14.354437Z","iopub.status.idle":"2025-07-15T21:49:14.518390Z","shell.execute_reply.started":"2025-07-15T21:49:14.354406Z","shell.execute_reply":"2025-07-15T21:49:14.516962Z"}},"outputs":[],"execution_count":null},{"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\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.predict(sequence)\n    \n    # Get the most common prediction (mode)\n    mode_pred = int(pd.Series(prediction).mode()[0])\n\n\n    return target_le.inverse_transform([mode_pred])[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T21:49:14.520028Z","iopub.execute_input":"2025-07-15T21:49:14.520329Z","iopub.status.idle":"2025-07-15T21:49:14.528088Z","shell.execute_reply.started":"2025-07-15T21:49:14.520307Z","shell.execute_reply":"2025-07-15T21:49:14.526738Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import joblib\n\n# Load pre-trained model and label encoder\nmodel = joblib.load('/kaggle/input/model-data/model.pkl')\ntarget_le = joblib.load('/kaggle/input/target-encoder-data/label_encoder.pkl')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T22:15:52.771742Z","iopub.execute_input":"2025-07-15T22:15:52.772177Z","iopub.status.idle":"2025-07-15T22:15:52.898455Z","shell.execute_reply.started":"2025-07-15T22:15:52.772149Z","shell.execute_reply":"2025-07-15T22:15:52.897522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_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-07-15T22:15:55.756579Z","iopub.execute_input":"2025-07-15T22:15:55.757272Z","iopub.status.idle":"2025-07-15T22:15:56.746697Z","shell.execute_reply.started":"2025-07-15T22:15:55.757241Z","shell.execute_reply":"2025-07-15T22:15:56.745852Z"}},"outputs":[],"execution_count":null}]}