{"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,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"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-08-07T11:07:15.495128Z","iopub.execute_input":"2025-08-07T11:07:15.495439Z","iopub.status.idle":"2025-08-07T11:07:16.937374Z","shell.execute_reply.started":"2025-08-07T11:07:15.495406Z","shell.execute_reply":"2025-08-07T11:07:16.936065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:07:37.223726Z","iopub.execute_input":"2025-08-07T11:07:37.224334Z","iopub.status.idle":"2025-08-07T11:08:10.965168Z","shell.execute_reply.started":"2025-08-07T11:07:37.224307Z","shell.execute_reply":"2025-08-07T11:08:10.964401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:11:48.804530Z","iopub.execute_input":"2025-08-07T11:11:48.804806Z","iopub.status.idle":"2025-08-07T11:11:49.034887Z","shell.execute_reply.started":"2025-08-07T11:11:48.804783Z","shell.execute_reply":"2025-08-07T11:11:49.033978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:11:39.929580Z","iopub.execute_input":"2025-08-07T11:11:39.929910Z","iopub.status.idle":"2025-08-07T11:11:39.934559Z","shell.execute_reply.started":"2025-08-07T11:11:39.929887Z","shell.execute_reply":"2025-08-07T11:11:39.933491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[df['phase']=='Transition'].head()\n# orientation\tbehavior\tphase\tgesture","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:41:27.387546Z","iopub.execute_input":"2025-08-07T11:41:27.387838Z","iopub.status.idle":"2025-08-07T11:41:28.174906Z","shell.execute_reply.started":"2025-08-07T11:41:27.387815Z","shell.execute_reply":"2025-08-07T11:41:28.174029Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_filtered = df[['subject',\t'behavior',\t'phase',\t'gesture',\t'acc_x',\t'acc_y',\t'acc_z',\t'rot_w',\t'rot_x',\t'rot_y',\t'rot_z'\t,'thm_1',\t'thm_2'\t,'thm_3'\t,'thm_4'\t,'thm_5']]\ndf_filtered.head()\n\n# sequence_type orientation  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:46:14.495418Z","iopub.execute_input":"2025-08-07T11:46:14.495714Z","iopub.status.idle":"2025-08-07T11:46:14.561280Z","shell.execute_reply.started":"2025-08-07T11:46:14.495693Z","shell.execute_reply":"2025-08-07T11:46:14.560533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_filtered['behavior'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:51:58.969754Z","iopub.execute_input":"2025-08-07T11:51:58.970427Z","iopub.status.idle":"2025-08-07T11:51:59.021258Z","shell.execute_reply.started":"2025-08-07T11:51:58.970399Z","shell.execute_reply":"2025-08-07T11:51:59.020505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_filtered[df_filtered['gesture']=='Text on phone'].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:51:08.080751Z","iopub.execute_input":"2025-08-07T11:51:08.081061Z","iopub.status.idle":"2025-08-07T11:51:08.145171Z","shell.execute_reply.started":"2025-08-07T11:51:08.081020Z","shell.execute_reply":"2025-08-07T11:51:08.144340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv\")\ndf_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:15:53.765986Z","iopub.execute_input":"2025-08-07T11:15:53.766367Z","iopub.status.idle":"2025-08-07T11:15:54.018463Z","shell.execute_reply.started":"2025-08-07T11:15:53.766341Z","shell.execute_reply":"2025-08-07T11:15:54.017434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['gesture'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T11:25:05.322161Z","iopub.execute_input":"2025-08-07T11:25:05.322445Z","iopub.status.idle":"2025-08-07T11:25:05.376950Z","shell.execute_reply.started":"2025-08-07T11:25:05.322426Z","shell.execute_reply":"2025-08-07T11:25:05.376144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_filtered.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T12:03:59.743773Z","iopub.execute_input":"2025-08-07T12:03:59.744357Z","iopub.status.idle":"2025-08-07T12:03:59.760241Z","shell.execute_reply.started":"2025-08-07T12:03:59.744332Z","shell.execute_reply":"2025-08-07T12:03:59.759314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.utils import to_categorical\n\n# Load your dataset\ndf = df_filtered\n\n# Separate features and labels\nX = df.drop(columns=[\"gesture\"])\ny = df[\"gesture\"]\n\n# Encode categorical input features\nX = pd.get_dummies(X, columns=[\"subject\", \"behavior\", \"phase\"])\n\n# Normalize numerical features\nnumeric_cols = ['acc_x', 'acc_y', 'acc_z', 'rot_w', 'rot_x', 'rot_y', 'rot_z', \n                'thm_1', 'thm_2', 'thm_3', 'thm_4', 'thm_5']\nscaler = StandardScaler()\nX[numeric_cols] = scaler.fit_transform(X[numeric_cols])\n\n# Encode gesture (label)\nlabel_encoder = LabelEncoder()\ny_encoded = label_encoder.fit_transform(y)\ny_categorical = to_categorical(y_encoded)\n\n# Split into train and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T12:08:38.900862Z","iopub.execute_input":"2025-08-07T12:08:38.901196Z","iopub.status.idle":"2025-08-07T12:08:39.923506Z","shell.execute_reply.started":"2025-08-07T12:08:38.901171Z","shell.execute_reply":"2025-08-07T12:08:39.922639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T12:08:45.778592Z","iopub.execute_input":"2025-08-07T12:08:45.778899Z","iopub.status.idle":"2025-08-07T12:08:45.784928Z","shell.execute_reply.started":"2025-08-07T12:08:45.778876Z","shell.execute_reply":"2025-08-07T12:08:45.784107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T12:05:32.681989Z","iopub.execute_input":"2025-08-07T12:05:32.682352Z","iopub.status.idle":"2025-08-07T12:05:32.744736Z","shell.execute_reply.started":"2025-08-07T12:05:32.682326Z","shell.execute_reply":"2025-08-07T12:05:32.743576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build the neural network\nmodel = Sequential([\n    Dense(128, activation='relu', input_shape=(X_train.shape[1],)),\n    Dropout(0.3),\n    Dense(64, activation='relu'),\n    Dropout(0.2),\n    Dense(y_categorical.shape[1], activation='softmax')  # Softmax for multi-class\n])\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(X_train, y_train, epochs=30, batch_size=32, validation_split=0.2)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-07T12:08:49.690292Z","iopub.execute_input":"2025-08-07T12:08:49.690593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model\nloss, accuracy = model.evaluate(X_test, y_test)\nprint(f\"Test Accuracy: {accuracy:.2f}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}