{"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":"none","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"}],"dockerImageVersionId":31040,"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-07-19T19:12:22.119978Z","iopub.execute_input":"2025-07-19T19:12:22.120210Z","iopub.status.idle":"2025-07-19T19:12:23.867979Z","shell.execute_reply.started":"2025-07-19T19:12:22.120184Z","shell.execute_reply":"2025-07-19T19:12:23.867027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport warnings\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\n\n\nwarnings.filterwarnings(\"ignore\")\n\npd.set_option('display.max_columns', 500)\npd.set_option('display.max_rows', 200)\npd.set_option('display.width', 500)\npd.set_option('display.float_format', lambda x: '{:,.3f}'.format(x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:16:15.270269Z","iopub.execute_input":"2025-07-19T19:16:15.270607Z","iopub.status.idle":"2025-07-19T19:16:17.355354Z","shell.execute_reply.started":"2025-07-19T19:16:15.270578Z","shell.execute_reply":"2025-07-19T19:16:17.354456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv')\ndf_train_demographics = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv')\ndf_test = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv')\ndf_test_demographics = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:12:23.877147Z","iopub.execute_input":"2025-07-19T19:12:23.877779Z","iopub.status.idle":"2025-07-19T19:13:01.896434Z","shell.execute_reply.started":"2025-07-19T19:12:23.877746Z","shell.execute_reply":"2025-07-19T19:13:01.895391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(list(df_train.columns))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:14:49.196239Z","iopub.execute_input":"2025-07-19T19:14:49.196540Z","iopub.status.idle":"2025-07-19T19:14:49.201985Z","shell.execute_reply.started":"2025-07-19T19:14:49.196498Z","shell.execute_reply":"2025-07-19T19:14:49.200843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x='gesture', data=df_train)\nplt.title('Distribution of Gesture Labels')\nplt.xticks(rotation=90)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:16:40.146154Z","iopub.execute_input":"2025-07-19T19:16:40.146629Z","iopub.status.idle":"2025-07-19T19:16:41.025039Z","shell.execute_reply.started":"2025-07-19T19:16:40.146602Z","shell.execute_reply":"2025-07-19T19:16:41.023506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"seq_lengths = df_train.groupby('sequence_id')['sequence_counter'].count()\nsns.histplot(seq_lengths, bins=30, kde=True)\nplt.title(\"Sequence Length Distribution\")\nplt.xlabel(\"Number of Rows in a Sequence\")\nplt.ylabel(\"Frequency\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:17:14.729639Z","iopub.execute_input":"2025-07-19T19:17:14.731364Z","iopub.status.idle":"2025-07-19T19:17:15.146446Z","shell.execute_reply.started":"2025-07-19T19:17:14.731328Z","shell.execute_reply":"2025-07-19T19:17:15.145314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"acc_cols = ['acc_x', 'acc_y', 'acc_z']\ndf_grouped = df_train.groupby('sequence_counter')[acc_cols].mean()\n\ndf_grouped.plot(figsize=(10, 5), title='Mean Acceleration over Sequence Step')\nplt.xlabel('Sequence Counter')\nplt.ylabel('Acceleration (m/s²)')\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:17:43.109488Z","iopub.execute_input":"2025-07-19T19:17:43.109835Z","iopub.status.idle":"2025-07-19T19:17:43.468029Z","shell.execute_reply.started":"2025-07-19T19:17:43.109813Z","shell.execute_reply":"2025-07-19T19:17:43.467050Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert all thm_ columns to a heatmap-like matrix\nthm_cols = [col for col in df_train.columns if col.startswith('thm_')]\nmean_thm = df_train[thm_cols].mean().values.reshape(1, -1)\n\nsns.heatmap(mean_thm, annot=True, fmt=\".2f\", cmap='coolwarm', xticklabels=thm_cols)\nplt.title(\"Mean Thermopile Sensor Values\")\nplt.yticks([])\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:18:16.091374Z","iopub.execute_input":"2025-07-19T19:18:16.092335Z","iopub.status.idle":"2025-07-19T19:18:16.337486Z","shell.execute_reply.started":"2025-07-19T19:18:16.092303Z","shell.execute_reply":"2025-07-19T19:18:16.336406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tof_cols = [col for col in df_train.columns if col.startswith('tof_1_v')]\ntof_grid = df_train[tof_cols].iloc[0].values.reshape(8, 8)\n\nsns.heatmap(tof_grid, cmap='viridis', cbar=True)\nplt.title(\"TOF Sensor 1 (Sample Frame)\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:18:39.561139Z","iopub.execute_input":"2025-07-19T19:18:39.561475Z","iopub.status.idle":"2025-07-19T19:18:39.986646Z","shell.execute_reply.started":"2025-07-19T19:18:39.561452Z","shell.execute_reply":"2025-07-19T19:18:39.985585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nsns.countplot(x='behavior', data=df_train, order=df_train['behavior'].value_counts().index)\nplt.xticks(rotation=45)\nplt.title(\"Behavior Distribution\")\n\nplt.figure(figsize=(12, 5))\nsns.countplot(x='orientation', data=df_train, order=df_train['orientation'].value_counts().index)\nplt.xticks(rotation=45)\nplt.title(\"Orientation Distribution\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:19:25.499174Z","iopub.execute_input":"2025-07-19T19:19:25.499561Z","iopub.status.idle":"2025-07-19T19:19:26.373860Z","shell.execute_reply.started":"2025-07-19T19:19:25.499505Z","shell.execute_reply":"2025-07-19T19:19:26.372606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sensor_cols = [col for col in df_train.columns if col.startswith(('acc_', 'rot_', 'thm_'))]\ncorr_matrix = df_train[sensor_cols].corr()\n\nplt.figure(figsize=(15, 12))\nsns.heatmap(corr_matrix, cmap='coolwarm', center=0, vmax=1, vmin=-1)\nplt.title(\"Sensor Correlation Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:19:58.733385Z","iopub.execute_input":"2025-07-19T19:19:58.734248Z","iopub.status.idle":"2025-07-19T19:19:59.747861Z","shell.execute_reply.started":"2025-07-19T19:19:58.734221Z","shell.execute_reply":"2025-07-19T19:19:59.746939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = px.scatter_3d(df_train, x='acc_x', y='acc_y', z='acc_z', color='gesture')\nfig.update_layout(title=\"3D Acceleration by Gesture\")\nfig.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:20:40.694091Z","iopub.execute_input":"2025-07-19T19:20:40.694446Z","iopub.status.idle":"2025-07-19T19:20:41.287704Z","shell.execute_reply.started":"2025-07-19T19:20:40.694421Z","shell.execute_reply":"2025-07-19T19:20:41.286105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Summary stats for accelerometer, rotation, and thermopile\nsensor_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']\n\nplt.figure(figsize=(14, 6))\nsns.boxplot(data=df_train[sensor_cols])\nplt.xticks(rotation=45)\nplt.title(\"Sensor Value Distribution (acc, rot, thm)\")\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:22:25.337169Z","iopub.execute_input":"2025-07-19T19:22:25.337860Z","iopub.status.idle":"2025-07-19T19:22:26.059634Z","shell.execute_reply.started":"2025-07-19T19:22:25.337829Z","shell.execute_reply":"2025-07-19T19:22:26.058618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\nfor i in range(1, 6):\n    tof_cols = [f'tof_{i}_v{j}' for j in range(64)]\n    sample = df_train[tof_cols].iloc[0].values.reshape(8, 8)\n\n    plt.figure(figsize=(4, 4))\n    sns.heatmap(sample, cmap=\"viridis\", cbar=True)\n    plt.title(f\"TOF Sensor {i} - Sample Grid\")\n    plt.axis(\"off\")\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:23:34.971191Z","iopub.execute_input":"2025-07-19T19:23:34.971593Z","iopub.status.idle":"2025-07-19T19:23:36.597540Z","shell.execute_reply.started":"2025-07-19T19:23:34.971563Z","shell.execute_reply":"2025-07-19T19:23:36.596586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Include only continuous columns\nselected_cols = sensor_cols + [f'thm_{i}' for i in range(1, 6)]\ncorr = df_train[selected_cols].corr()\n\nplt.figure(figsize=(12, 10))\nsns.heatmap(corr, cmap='coolwarm', center=0, square=True)\nplt.title(\"Sensor Correlation Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:23:42.300594Z","iopub.execute_input":"2025-07-19T19:23:42.301322Z","iopub.status.idle":"2025-07-19T19:23:43.219013Z","shell.execute_reply.started":"2025-07-19T19:23:42.301294Z","shell.execute_reply":"2025-07-19T19:23:43.218122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"acc_cols = ['acc_x', 'acc_y', 'acc_z']\ndf_grouped = df_train.groupby('gesture')[acc_cols].mean().reset_index()\n\ndf_grouped.plot(x='gesture', kind='bar', figsize=(10, 5))\nplt.title(\"Mean Acceleration per Gesture\")\nplt.xticks(rotation=45)\nplt.ylabel(\"Acceleration (m/s²)\")\nplt.grid(True)\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:23:47.575289Z","iopub.execute_input":"2025-07-19T19:23:47.575711Z","iopub.status.idle":"2025-07-19T19:23:48.043074Z","shell.execute_reply.started":"2025-07-19T19:23:47.575683Z","shell.execute_reply":"2025-07-19T19:23:48.042247Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x='sequence_type', data=df_train)\nplt.title(\"Sequence Type Distribution\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T19:24:02.561078Z","iopub.execute_input":"2025-07-19T19:24:02.561421Z","iopub.status.idle":"2025-07-19T19:24:03.007229Z","shell.execute_reply.started":"2025-07-19T19:24:02.561397Z","shell.execute_reply":"2025-07-19T19:24:03.006342Z"}},"outputs":[],"execution_count":null}]}