{"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-05-30T11:47:38.649837Z","iopub.execute_input":"2025-05-30T11:47:38.650209Z","iopub.status.idle":"2025-05-30T11:47:39.004349Z","shell.execute_reply.started":"2025-05-30T11:47:38.650179Z","shell.execute_reply":"2025-05-30T11:47:39.003464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom collections import Counter\nimport warnings\nwarnings.filterwarnings('ignore')\n\nplt.style.use('seaborn-v0_8-darkgrid')\nsns.set_palette(\"husl\")\nplt.rcParams['figure.figsize'] = (12, 6)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:47:39.006154Z","iopub.execute_input":"2025-05-30T11:47:39.006572Z","iopub.status.idle":"2025-05-30T11:47:40.220333Z","shell.execute_reply.started":"2025-05-30T11:47:39.006549Z","shell.execute_reply":"2025-05-30T11:47:40.219469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Loading data files...\")\ntrain_df = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv')\ntrain_demographics = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv')\ntest_df = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv')\ntest_demographics = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv')\n\nprint(f\"Training data shape: {train_df.shape}\")\nprint(f\"Test data shape: {test_df.shape}\")\nprint(f\"Training demographics shape: {train_demographics.shape}\")\nprint(f\"Test demographics shape: {test_demographics.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:47:40.221393Z","iopub.execute_input":"2025-05-30T11:47:40.221743Z","iopub.status.idle":"2025-05-30T11:48:17.685223Z","shell.execute_reply.started":"2025-05-30T11:47:40.221722Z","shell.execute_reply":"2025-05-30T11:48:17.684365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n\" + \"=\"*50)\nprint(\"BASIC DATA EXPLORATION\")\nprint(\"=\"*50)\n\nimu_cols = [col for col in train_df.columns if col.startswith(('acc_', 'rot_'))]\nthm_cols = [col for col in train_df.columns if col.startswith('thm_')]\ntof_cols = [col for col in train_df.columns if col.startswith('tof_')]\nmeta_cols = ['row_id', 'sequence_id', 'sequence_type', 'sequence_counter', \n             'subject', 'gesture', 'orientation', 'behavior']\n\nprint(f\"\\nColumn groups:\")\nprint(f\"- Metadata columns: {len(meta_cols)}\")\nprint(f\"- IMU columns: {len(imu_cols)}\")\nprint(f\"- Thermopile columns: {len(thm_cols)}\")\nprint(f\"- Time-of-Flight columns: {len(tof_cols)}\")\nprint(f\"- Total columns: {len(train_df.columns)}\")\n\nprint(\"\\n\\nUnique values in categorical columns:\")\nfor col in ['sequence_type', 'gesture', 'orientation', 'behavior']:\n    if col in train_df.columns:\n        unique_vals = train_df[col].nunique()\n        print(f\"- {col}: {unique_vals} unique values\")\n        if unique_vals < 20:\n            print(f\"  Values: {sorted(train_df[col].unique())}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:48:59.942988Z","iopub.execute_input":"2025-05-30T11:48:59.943929Z","iopub.status.idle":"2025-05-30T11:49:00.234759Z","shell.execute_reply.started":"2025-05-30T11:48:59.943886Z","shell.execute_reply":"2025-05-30T11:49:00.233791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n\" + \"=\"*50)\nprint(\"SEQUENCE ANALYSIS\")\nprint(\"=\"*50)\n\nsequence_info = train_df.groupby('sequence_id').agg({\n    'sequence_counter': ['count', 'max'],\n    'subject': 'first',\n    'gesture': 'first',\n    'sequence_type': 'first'\n}).reset_index()\n\nsequence_info.columns = ['sequence_id', 'length', 'max_counter', 'subject', 'gesture', 'sequence_type']\n\nprint(f\"\\nTotal sequences: {len(sequence_info)}\")\nprint(f\"Sequence length statistics:\")\nprint(sequence_info['length'].describe())\n\n\nplt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.hist(sequence_info['length'], bins=50, edgecolor='black', alpha=0.7)\nplt.xlabel('Sequence Length')\nplt.ylabel('Count')\nplt.title('Distribution of Sequence Lengths')\n\nplt.subplot(1, 2, 2)\nsequence_info.boxplot(column='length', by='sequence_type', ax=plt.gca())\nplt.title('Sequence Length by Type')\nplt.suptitle('')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:49:24.059752Z","iopub.execute_input":"2025-05-30T11:49:24.060482Z","iopub.status.idle":"2025-05-30T11:49:25.044480Z","shell.execute_reply.started":"2025-05-30T11:49:24.060455Z","shell.execute_reply":"2025-05-30T11:49:25.043410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n\" + \"=\"*50)\nprint(\"GESTURE ANALYSIS\")\nprint(\"=\"*50)\n\ngesture_counts = sequence_info['gesture'].value_counts()\nprint(f\"\\nGesture distribution:\")\nprint(gesture_counts)\n\nplt.figure(figsize=(12, 6))\ngesture_counts.plot(kind='bar', color='#ff69b4')\nplt.title('Gesture Distribution in Training Data', fontsize=14)\nplt.xlabel('Gesture', fontsize=12)\nplt.ylabel('Count', fontsize=12)\nplt.xticks(rotation=45, ha='right')\nplt.grid(axis='y', alpha=0.3)\nplt.tight_layout()\nplt.show()\n\nplt.subplot(2, 1, 2)\ngesture_type_df = pd.crosstab(sequence_info['gesture'], sequence_info['sequence_type'])\ngesture_type_df.plot(kind='bar', stacked=True)\nplt.title('Gesture Distribution by Sequence Type')\nplt.xlabel('Gesture')\nplt.ylabel('Count')\nplt.xticks(rotation=45, ha='right')\nplt.legend(title='Sequence Type')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:52:27.184324Z","iopub.execute_input":"2025-05-30T11:52:27.184644Z","iopub.status.idle":"2025-05-30T11:52:27.998563Z","shell.execute_reply.started":"2025-05-30T11:52:27.184619Z","shell.execute_reply":"2025-05-30T11:52:27.997662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n\" + \"=\"*50)\nprint(\"DEMOGRAPHIC ANALYSIS\")\nprint(\"=\"*50)\n\ndemo_sequence = sequence_info.merge(train_demographics, on='subject', how='left')\n\nprint(\"\\nDemographic statistics:\")\nprint(train_demographics.describe())\n\nfig, axes = plt.subplots(2, 3, figsize=(15, 10))\n\naxes[0, 0].hist(train_demographics['age'], bins=20, edgecolor='black')\naxes[0, 0].set_title('Age Distribution')\naxes[0, 0].set_xlabel('Age')\naxes[0, 0].set_ylabel('Count')\n\nadult_child_counts = train_demographics['adult_child'].value_counts()\naxes[0, 1].pie(adult_child_counts.values, labels=['Child', 'Adult'], autopct='%1.1f%%')\naxes[0, 1].set_title('Adult vs Child Distribution')\n\nsex_counts = train_demographics['sex'].value_counts()\naxes[0, 2].pie(sex_counts.values, labels=['Female', 'Male'], autopct='%1.1f%%')\naxes[0, 2].set_title('Sex Distribution')\n\nhand_counts = train_demographics['handedness'].value_counts()\naxes[1, 0].pie(hand_counts.values, labels=['Left', 'Right'], autopct='%1.1f%%')\naxes[1, 0].set_title('Handedness Distribution')\n\naxes[1, 1].hist(train_demographics['height_cm'], bins=20, edgecolor='black')\naxes[1, 1].set_title('Height Distribution')\naxes[1, 1].set_xlabel('Height (cm)')\naxes[1, 1].set_ylabel('Count')\n\naxes[1, 2].scatter(train_demographics['shoulder_to_wrist_cm'], \n                   train_demographics['elbow_to_wrist_cm'], alpha=0.6)\naxes[1, 2].set_xlabel('Shoulder to Wrist (cm)')\naxes[1, 2].set_ylabel('Elbow to Wrist (cm)')\naxes[1, 2].set_title('Arm Measurements')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:53:03.814995Z","iopub.execute_input":"2025-05-30T11:53:03.815416Z","iopub.status.idle":"2025-05-30T11:53:04.870680Z","shell.execute_reply.started":"2025-05-30T11:53:03.815390Z","shell.execute_reply":"2025-05-30T11:53:04.869701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n\" + \"=\"*50)\nprint(\"SENSOR DATA VISUALIZATION\")\nprint(\"=\"*50)\n\nsample_sequences = sequence_info.sample(min(3, len(sequence_info)))['sequence_id'].values\n\nfor seq_id in sample_sequences[:2]:  # Visualize first 2\n    seq_data = train_df[train_df['sequence_id'] == seq_id].sort_values('sequence_counter')\n    gesture = seq_data['gesture'].iloc[0]\n    \n    fig, axes = plt.subplots(3, 1, figsize=(12, 10))\n    fig.suptitle(f'Sensor Data for Sequence {seq_id} - Gesture: {gesture}')\n    \n    axes[0].plot(seq_data['sequence_counter'], seq_data['acc_x'], label='Acc X')\n    axes[0].plot(seq_data['sequence_counter'], seq_data['acc_y'], label='Acc Y')\n    axes[0].plot(seq_data['sequence_counter'], seq_data['acc_z'], label='Acc Z')\n    axes[0].set_ylabel('Acceleration (m/s²)')\n    axes[0].set_title('Accelerometer Data')\n    axes[0].legend()\n    axes[0].grid(True, alpha=0.3)\n    \n    for i in range(1, 6):\n        axes[1].plot(seq_data['sequence_counter'], seq_data[f'thm_{i}'], label=f'Thermopile {i}')\n    axes[1].set_ylabel('Temperature (°C)')\n    axes[1].set_title('Thermopile Sensor Data')\n    axes[1].legend()\n    axes[1].grid(True, alpha=0.3)\n    \n    behavior_map = {'Transition': 0, 'Pause': 1, 'Gesture': 2}\n    behavior_numeric = seq_data['behavior'].map(behavior_map)\n    axes[2].plot(seq_data['sequence_counter'], behavior_numeric, 'o-')\n    axes[2].set_ylabel('Behavior Phase')\n    axes[2].set_yticks([0, 1, 2])\n    axes[2].set_yticklabels(['Transition', 'Pause', 'Gesture'])\n    axes[2].set_xlabel('Sequence Counter')\n    axes[2].set_title('Behavior Phases')\n    axes[2].grid(True, alpha=0.3)\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T11:53:47.019829Z","iopub.execute_input":"2025-05-30T11:53:47.020219Z","iopub.status.idle":"2025-05-30T11:53:48.436026Z","shell.execute_reply.started":"2025-05-30T11:53:47.020192Z","shell.execute_reply":"2025-05-30T11:53:48.435086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}