{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\ntrain=pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv\")\ntrain_demograph = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv\")    ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-07T03:51:38.122641Z","iopub.execute_input":"2025-06-07T03:51:38.123635Z","iopub.status.idle":"2025-06-07T03:52:13.686381Z","shell.execute_reply.started":"2025-06-07T03:51:38.123598Z","shell.execute_reply":"2025-06-07T03:52:13.685561Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 📝 Overview\n\nThis notebook focuses on understanding the structure and meaning of the dataset before jumping into EDA or modeling. Since the data comes from wearable sensors and captures complex human gestures, it's essential to first grasp the physical interpretation of each attribute.\n\nI have also included a categorization of attributes into categories, which is provided in the final section.\n\n> **Disclaimer:** This is based solely on my exploration and understanding. Please feel free to suggest corrections if anything appears to be misinterpreted or incomplete.\n\n---\n\n**Let’s begin by understanding the attributes.**","metadata":{}},{"cell_type":"markdown","source":" ## **1. `sequence_id`**\n\nA `sequence_id` (e.g., `\"SEQ_015261\"`) represents:\n\n>A **single gesture attempt** recorded from a participant, using wearable sensors, over time.\n\nA single full gesture attempt lasts upto many time steps (represented by `sequence_counter`). The number of time steps is different for different gesture attempts. \n\n#### 🧬 Each Row Corresponding to a Particular `sequence_id` Is:\n\n- One **time step** (or frame) in that gesture\n  \n- With **multisensor data** (acceleration, rotation, temperature, distance, etc.)\n  \n- Recorded in **chronological order** using `sequence_counter`\n  \n\nSo, for example, if `SEQ_015261` has **84 rows**, it means the gesture lasted **84 time steps**, and each row tells us:\n\n- What sensors read at that specific moment\n  \n- What part of the gesture was happening (`behavior`: \"Transition\" or \"Gesture\")\n\nOur goal is to classify that sequence into **one of 18 gestures** and predict if this sequence came from BFRB or non-BFRB action.","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 2. `sequence_counter`\n\n- It's a **time step index** within a `sequence_id`.\n  \n- Starts from **0** and increases by **1** for each row in that sequence.\n  \n- Resets to 0 for every new `sequence_id`.\n  \n- Represents the **order of sensor readings** within a gesture.\n\n- It iss essential for maintaining the **temporal order** of the time series.\n  \n\n### 🧠 Example:\n","metadata":{}},{"cell_type":"code","source":"train.loc[train['sequence_id']=='SEQ_009199'].loc[:,['sequence_id','sequence_counter']].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-06T14:08:01.847916Z","iopub.execute_input":"2025-06-06T14:08:01.848616Z","iopub.status.idle":"2025-06-06T14:08:01.887247Z","shell.execute_reply.started":"2025-06-06T14:08:01.848592Z","shell.execute_reply":"2025-06-06T14:08:01.886467Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 3. **`subject`**\n\n* A unique **anonymized ID** for the person who performed the gesture.\n* There are **81 unique subjects** in the training data.\n* Each subject may perform **multiple sequences** (gesture attempts).\n* Related demographic info (like age, sex, handedness) is stored in `train_demographics.csv`.\n\n### 🔍 Why it matters:\n\n* Helps analyze **inter-subject variability**.\n* Can be used to:\n\n  * Group data by subject\n  * Create subject-wise splits for **cross-validation** (e.g., to avoid data leakage)\n  * Add demographic features to the model\n","metadata":{}},{"cell_type":"code","source":"# demographic information of a particular subject\ntrain_demograph.loc[train_demograph.subject=='SUBJ_040733']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-06T14:16:29.557417Z","iopub.execute_input":"2025-06-06T14:16:29.558238Z","iopub.status.idle":"2025-06-06T14:16:29.567549Z","shell.execute_reply.started":"2025-06-06T14:16:29.558202Z","shell.execute_reply":"2025-06-06T14:16:29.566447Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 4. **`behavior`**\nThe `behavior` column describes the subject's **phase of motion** at each time step within a `sequence_id`. It helps contextualize what part of the gesture process the subject is in.\n\n### ✳️ Observed Values:\n\n| Behavior Text | Interpreted Phase | Meaning |\n| --- | --- | --- |\n| **Relaxes and moves hand to target location** | Transition *(with implicit Pause)* | The subject **briefly pauses** or returns to a neutral state (\"relaxes\") and **begins moving** the hand toward the gesture location. This is the closest thing to a **Pause** phase, though it's not separately labeled. |\n| **Moves hand to target location** | Transition | The subject directly **moves the hand** from rest or a previous state toward the gesture location. |\n| **Hand at target location** | Gesture | The hand is held steady at the gesture location — often a **preparation or holding** phase. |\n| **Performs gesture** | Gesture | The subject actively **executes the gesture**, either BFRB or non-BFRB. |\n\n---\n\n### 🧠 Practical Notes:\n\n- The two \"Move\" behaviors belong to the **Transition** phase.\n  \n- The \"Hand at\" and \"performs\" behaviors belong to the **Gesture** phase.\n  \n- The **\"Relaxes\"** part may imply a `Pause`, even though no distinct \"Pause\" label appears in the dataset.\n\n- They are mapped to the `sequence_type` indirectly but `behavior` gives more **granular timing** per row\n  \n\nWe can simplify them like this during preprocessing:","metadata":{}},{"cell_type":"code","source":"behavior_mapping = {\n    'Relaxes and moves hand to target location': 'Transition',\n    'Moves hand to target location': 'Transition',\n    'Hand at target location': 'Gesture',\n    'Performs gesture': 'Gesture'\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-06T14:56:31.772860Z","iopub.execute_input":"2025-06-06T14:56:31.773155Z","iopub.status.idle":"2025-06-06T14:56:31.777011Z","shell.execute_reply.started":"2025-06-06T14:56:31.773137Z","shell.execute_reply":"2025-06-06T14:56:31.776349Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 5. **`phase` — Motion Stage Within a Sequence**\n\nEach `sequence_id` consists of multiple time steps, and each time step is labeled with a **phase**, describing the stage of motion during that sequence:\n\n### ✨ Possible Values:\n\n* **`Transition`**:\n  The subject is **moving their hand toward the gesture position** (i.e., approaching or setting up the gesture).\n  Often overlaps with behavior like *\"Moves hand to target location\"* or *\"Relaxes and moves hand to target location\"*.\n\n* **`Gesture`**:\n  The subject is **performing the actual gesture** (e.g., scratching, clapping, pulling hair).\n  Typically aligns with behavior like *\"Performs gesture\"*.\n\n### 🧠 Notes:\n\n* While the dataset documentation mentions a `\"Pause\"` phase, it **does not appear** in the actual data.\n* Some \"Relaxing\" or \"Hand at location\" behaviors may implicitly represent a **pause**, but it is not labeled as such.\n\n","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"train.orientation.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-07T03:52:13.688032Z","iopub.execute_input":"2025-06-07T03:52:13.688340Z","iopub.status.idle":"2025-06-07T03:52:13.757784Z","shell.execute_reply.started":"2025-06-07T03:52:13.688312Z","shell.execute_reply":"2025-06-07T03:52:13.756535Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 6. **`orientation` — Subject's Body Position During the Sequence (Train Only)**\n\n* Describes the **posture or position** of the subject's body during the entire sequence.\n* Available **only in the training set** and **not included** in the test set or submission file.\n* It is **not a target variable**, but can be valuable for **data analysis**, **model stratification**, or **training enhancements**.\n\n\n### 🧠 Usage Insight:\n\n\nIf we train a model using the `orientation` column, and then try to use it during test time), where `orientation` is **missing**, the model will indeed **fail**, because:\n\n* It expects that feature.\n* But it’s missing in test sequences.\n\nSo, we just include `orientation` as a normal input feature**, however we can use it in following ways:\n\n\n\n ✅ 1. **For Exploratory Analysis or Stratified Modeling (No test-time issue)**\n\n ✅ 2. **For Representation Learning / Pretraining (Indirect Use)**\n\n* Use `orientation` during training to:\n\n  * **Learn embeddings** or pretrain a representation that understands posture-dependent motion.\n  * Then **discard orientation input** and fine-tune the model using only sensor features.\n\n👉 This works because we are **guiding the model to learn better representations**, even though we **don’t use `orientation` at test time**.\n","metadata":{}},{"cell_type":"markdown","source":"## 7. **`acc_x`, `acc_y`, `acc_z` — Linear Acceleration**\n\n- These represent **linear acceleration** readings from the IMU (Inertial Measurement Unit).\n  \n- Measured in **meters per second squared (m/s²)**.\n  \n- Recorded along the **three spatial axes**:\n  \n  - `acc_x`: left–right (horizontal)\n    \n  - `acc_y`: forward–backward (depth)\n    \n  - `acc_z`: up–down (vertical)\n \n### 🧠 Use in Modeling:\n\n- These are core **motion features** that capture how the hand is moving over time.\n  \n- Especially useful for detecting movement patterns associated with specific gestures or BFRBs.","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 8. **`rot_w`, `rot_x`, `rot_y`, `rot_z` — Orientation (Quaternions)**\n\n* These columns represent the **3D orientation** of the device (on the wrist) using **quaternions**.\n* Quaternions are a 4D format used to avoid issues like gimbal lock when tracking rotation.\n* Derived from the IMU’s:\n\n  * **Gyroscope** (angular velocity)\n  * **Accelerometer** (linear acceleration)\n  * **Magnetometer** (magnetic direction / compass)\n\n\n### 📐 Component Roles:\n\n| Component | Role                          |\n| --------- | ----------------------------- |\n| `rot_w`   | Scalar part (rotation amount) |\n| `rot_x`   | X-axis vector component       |\n| `rot_y`   | Y-axis vector component       |\n| `rot_z`   | Z-axis vector component       |\n\nThey together define a **unit quaternion** describing the wrist's orientation in 3D space at each time step.\n\n\n\n### 🧠 Use in Modeling:\n\n* Captures **how the hand is turned/rotated**, which complements acceleration data.\n* Important for detecting gestures that differ in **posture**, not just motion (e.g., scratching vs. tapping).\n\nWe convert these quaternions to Euler angles (pitch, roll, yaw) for easier interpretation or plotting.","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"\n## 9. **`thm_1` to `thm_5` — Thermopile Sensor Readings**\n\n* These are **temperature readings** from **five thermopile sensors** on the Helios wrist device.\n* Units: **Degrees Celsius (°C)**\n* Sensor Type: **Non-contact infrared temperature sensors**\n* Each sensor (`thm_1` to `thm_5`) measures **thermal radiation** from nearby objects — typically parts of the user’s body.\n\n\n### 📌 Interpretation:\n\n* Higher readings may suggest the sensor is **closer to the skin**.\n* Changes in these values can indicate **hand movement closer to or away from the body**.\n* Helpful in identifying gestures that involve **face-touching, scratching, or proximity-based behavior**.\n\n\n### 🧠 Use in Modeling:\n\n* These features provide **contextual proximity** to warm objects (e.g., face, neck, arm).\n* Useful for detecting **BFRBs**, as they often involve hand-to-body contact.\n","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"\n\n## 10. **`tof_[1–5]_v[0–63]` — Time-of-Flight Sensor Grid**\n\n* These columns represent **distance measurements** from 5 **Time-of-Flight (ToF)** sensors on the Helios device.\n* Each ToF sensor outputs an **8×8 pixel grid** of distance readings:\n\n  * `tof_1_v0` → Top-left pixel of sensor 1\n  * `tof_1_v63` → Bottom-right pixel of sensor 1\n  * … and similarly for sensors 2 to 5\n\n\n### 📏 Data Characteristics:\n\n| Attribute        | Details                                                     |\n| ---------------- | ----------------------------------------------------------- |\n| **Pixel Count**  | 64 values per sensor (v0 to v63) × 5 sensors = 320 features |\n| **Layout**       | Pixels are arranged row-wise in an 8×8 matrix               |\n| **Units**        | Uncalibrated distance (range: 0–254)                        |\n| **Missing Data** | `-1` indicates no signal (e.g., nothing nearby)             |\n\n\n\n### 📌 Interpretation:\n\n* Measures **proximity to nearby surfaces/objects** in the environment.\n* A **lower value** = closer object.\n* Used to infer **spatial position and orientation** of the hand relative to the body or surroundings.\n\n\n\n### 🧠 Use in Modeling:\n\n* Helps detect gestures that involve **closeness to the face, head, or other body parts**.\n* Essential for capturing **fine-grained spatial patterns** of BFRBs.\n\n","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 11. **`sequence_type` — BFRB vs Non-BFRB Indicator (Train Only)**\n\n- Indicates whether the gesture in a sequence is **BFRB-like (`target`)** or **not (`non-target`)**.\n  \n- Available **only in the training set**, **not required in the submission**.\n  \n- Serves as an **auxiliary label** — it helps understand or structure the classification problem but is **not directly predicted**.\n\n### 🧠 How to Use It:\n\n- Can be used for **exploratory analysis**, **class balancing**, or even a **two-stage model**:\n  \n  1. First predict `sequence_type` (target/non-target)\n    \n  2. Then classify the specific `gesture`","metadata":{}},{"cell_type":"code","source":"train.sequence_type.value_counts(normalize=True)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-05T07:33:33.478583Z","iopub.execute_input":"2025-06-05T07:33:33.478871Z","iopub.status.idle":"2025-06-05T07:33:33.544819Z","shell.execute_reply.started":"2025-06-05T07:33:33.478851Z","shell.execute_reply":"2025-06-05T07:33:33.544049Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"\n## 12. **`phase` — Motion Stage Within a Sequence**\n\nEach `sequence_id` consists of multiple time steps, and each time step is labeled with a **phase**, describing the stage of motion during that sequence:\n\n### ✨ Possible Values:\n\n* **`Transition`**:\n  The subject is **moving their hand toward the gesture position** (i.e., approaching or setting up the gesture).\n  Alighns with behaviors *\"Moves hand to target location\"*, *\"Relaxes and moves hand to target location\"*, and *\"Hand at target location\"*\n\n* **`Gesture`**:\n  The subject is **performing the actual gesture** (e.g., picking the skin,pulling hair).\n  Typically aligns with behavior *\"Performs gesture\"*.\n\n### 🧠 Notes:\n\n* While the dataset documentation mentions a `\"Pause\"` phase, it **does not appear** in the actual data.\n* Some \"Relaxing\" or \"Hand at location\" behaviors may implicitly represent a **Pause**, but it is not labeled as such.","metadata":{}},{"cell_type":"code","source":"train.loc[train.phase=='Transition'].behavior.unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-07T04:17:57.352437Z","iopub.execute_input":"2025-06-07T04:17:57.352781Z","iopub.status.idle":"2025-06-07T04:17:57.825833Z","shell.execute_reply.started":"2025-06-07T04:17:57.352756Z","shell.execute_reply":"2025-06-07T04:17:57.824797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.loc[train.phase=='Gesture'].behavior.unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-07T04:18:39.831162Z","iopub.execute_input":"2025-06-07T04:18:39.831901Z","iopub.status.idle":"2025-06-07T04:18:40.241758Z","shell.execute_reply.started":"2025-06-07T04:18:39.831870Z","shell.execute_reply":"2025-06-07T04:18:40.240999Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 13. **`gesture` — **Main Target Label (Train Only)****\n\n- This is the **primary target** column we need to predict in this competition.\n  \n- It represents the **specific gesture** performed during each sequence.\n  \n- Includes both:\n  \n  - **BFRB-like gestures** (e.g., Above ear - Pull hair, Neck - Pinch skin)\n    \n  - **Non-BFRB gestures** (e.g., Text on phone, Write name on leg)\n \n  - There are 18 such unique gestures in total.\n  \n- This column is:\n  \n  - ✅ Present in the **training data**\n    \n  - ❌ Hidden in the **test data**\n    \n  - ✅ Required in the **submission file","metadata":{}},{"cell_type":"code","source":"train.gesture.unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-07T03:58:42.455160Z","iopub.execute_input":"2025-06-07T03:58:42.455945Z","iopub.status.idle":"2025-06-07T03:58:42.497785Z","shell.execute_reply.started":"2025-06-07T03:58:42.455901Z","shell.execute_reply":"2025-06-07T03:58:42.496887Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"# ✅ Attributes Categorization\n\nHere is a meaningful categorization of the columns:\n\n\n| **#** | **Category**                            | **Feature(s)**                                                                                                 | **Description**                                                                 |\n| ----: | --------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |\n|   1️⃣ | 🧩 **Target Variable**                  | `gesture`                                                                                                      | The main prediction target — type of gesture performed.                         |\n|   2️⃣ | 📥 **Sensor-Based Input Features**      |                                                                                                                | Per-time-step sensor readings from the Helios device.                           |\n|       |   🟦 IMU Sensors                        | `acc_x, acc_y, acc_z`, `rot_w, rot_x, rot_y, rot_z`                                                            | Acceleration and orientation at each time step.                                 |\n|       |   🟨 Thermopile Sensors                 | `thm_1` to `thm_5`                                                                                             | Temperature readings from 5 sensors.                                            |\n|       |   🟩 Time-of-Flight Sensors             | `tof_1_v0` to `tof_5_v63`                                                                                      | 5 sensors × 64 pixels = 320 distance features per time step.                    |\n|   3️⃣ | ⏱️ **Time & Sequence Metadata**         | `sequence_id`, `sequence_counter`, `row_id`, `behavior`                                                        | Metadata to organize and label time-series segments.                            |\n|   4️⃣ | 👤 **Subject Demographics**             | `subject`, `age`, `sex`, `adult_child`, `handedness`, `height_cm`, `shoulder_to_wrist_cm`, `elbow_to_wrist_cm` | Static attributes describing the subject.                                       |\n|   5️⃣ | 🧭 **Contextual Features (Train Only)** | `orientation`, `sequence_type`                                                                                 | Available only in train set. Useful for stratified training or auxiliary tasks. |\n\n---\n\n","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}