{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"gpu","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1. Import library","metadata":{}},{"cell_type":"code","source":"import kaggle_evaluation.cmi_inference_server  # noqa: F401   | Kaggle runner hook","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:27:09.526969Z","iopub.execute_input":"2025-06-04T04:27:09.527645Z","iopub.status.idle":"2025-06-04T04:27:09.531386Z","shell.execute_reply.started":"2025-06-04T04:27:09.527621Z","shell.execute_reply":"2025-06-04T04:27:09.530588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport re\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom scipy.stats import gaussian_kde\nfrom scipy import stats\n\nfrom sklearn.metrics import f1_score, classification_report, accuracy_score\nfrom sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV\n\nimport torch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:27:14.556383Z","iopub.execute_input":"2025-06-04T04:27:14.556666Z","iopub.status.idle":"2025-06-04T04:27:17.904678Z","shell.execute_reply.started":"2025-06-04T04:27:14.556642Z","shell.execute_reply":"2025-06-04T04:27:17.904040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Number of GPU: \", torch.cuda.device_count())\nprint(\"GPU Name: \", torch.cuda.get_device_name())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:27:20.035730Z","iopub.execute_input":"2025-06-04T04:27:20.036444Z","iopub.status.idle":"2025-06-04T04:27:20.126497Z","shell.execute_reply.started":"2025-06-04T04:27:20.036421Z","shell.execute_reply":"2025-06-04T04:27:20.125772Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint('Using device:', device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:27:23.055359Z","iopub.execute_input":"2025-06-04T04:27:23.055621Z","iopub.status.idle":"2025-06-04T04:27:23.060273Z","shell.execute_reply.started":"2025-06-04T04:27:23.055585Z","shell.execute_reply":"2025-06-04T04:27:23.059586Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2. Dataset loaded","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv', nrows=200000)\ntrain_demo = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv')\nprint(f'train {train.shape} \\n train demographics : {train_demo.shape}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:27:46.128846Z","iopub.execute_input":"2025-06-04T04:27:46.129351Z","iopub.status.idle":"2025-06-04T04:27:53.080443Z","shell.execute_reply.started":"2025-06-04T04:27:46.129330Z","shell.execute_reply":"2025-06-04T04:27:53.079815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:27:57.061587Z","iopub.execute_input":"2025-06-04T04:27:57.061900Z","iopub.status.idle":"2025-06-04T04:27:57.099277Z","shell.execute_reply.started":"2025-06-04T04:27:57.061879Z","shell.execute_reply":"2025-06-04T04:27:57.098642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_demo.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:00.203390Z","iopub.execute_input":"2025-06-04T04:28:00.203702Z","iopub.status.idle":"2025-06-04T04:28:00.213492Z","shell.execute_reply.started":"2025-06-04T04:28:00.203678Z","shell.execute_reply":"2025-06-04T04:28:00.212841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Merge demographic information into the main training dataframe\ntrain_ds = train.merge(train_demo, on='subject', how='left')\ntrain_ds.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:03.393624Z","iopub.execute_input":"2025-06-04T04:28:03.394186Z","iopub.status.idle":"2025-06-04T04:28:03.624572Z","shell.execute_reply.started":"2025-06-04T04:28:03.394163Z","shell.execute_reply":"2025-06-04T04:28:03.624022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display the first few rows of the merged dataframe\ntrain.head().style.background_gradient(cmap='gist_rainbow')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:08.126926Z","iopub.execute_input":"2025-06-04T04:28:08.127688Z","iopub.status.idle":"2025-06-04T04:28:08.497921Z","shell.execute_reply.started":"2025-06-04T04:28:08.127653Z","shell.execute_reply":"2025-06-04T04:28:08.497123Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 3. Exploratory Data Analysis (EDA)","metadata":{}},{"cell_type":"markdown","source":"#### . Gesture Distribution: A countplot to understand the balance (or imbalance) of gesture classes. Imbalanced classes can significantly impact model performance.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.countplot(data=train_ds, x='gesture', color=\"#39A499\")\nplt.title('Distribution of Gestures', fontsize=16, fontweight='bold', color='darkblue')\nplt.xticks(rotation=45, ha='right')\nplt.gca().set_facecolor('#ecfce5')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:14.923459Z","iopub.execute_input":"2025-06-04T04:28:14.923763Z","iopub.status.idle":"2025-06-04T04:28:15.330505Z","shell.execute_reply.started":"2025-06-04T04:28:14.923742Z","shell.execute_reply":"2025-06-04T04:28:15.329866Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Subject Distribution: Checking how many samples are available per subject.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(14,6))\nsns.countplot(data=train_ds, x='subject',color=\"#CF4EB3\")\nplt.title('Distribution of Subject', fontsize=16, fontweight='bold', color='darkblue')\nplt.xticks(rotation=45, ha='right')\nplt.gca().set_facecolor(\"#d5e096\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:19.868741Z","iopub.execute_input":"2025-06-04T04:28:19.869295Z","iopub.status.idle":"2025-06-04T04:28:20.551172Z","shell.execute_reply.started":"2025-06-04T04:28:19.869272Z","shell.execute_reply":"2025-06-04T04:28:20.550501Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Pairwise Relationships: A pairplot to visualize the relationships between numerical features (acceleration components) and gesture labels. It's crucial for identifying potential feature interactions. The hue parameter is used to color points by gesture, making it easier to spot patterns. The sample size is reduced to speed this up.","metadata":{}},{"cell_type":"code","source":"subset = train_ds.sample(n=1000, random_state=42)\nsns.pairplot(subset[['acc_x','acc_y', 'acc_z','gesture']], hue='gesture')\nplt.suptitle('Pairwise Relationships of Acceleration and Gesture (Subset)', y=1.02, fontsize=14,\n              fontweight='bold', color='maroon')\n\nplt.gca().set_facecolor(\"#d5e096\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:24.984815Z","iopub.execute_input":"2025-06-04T04:28:24.985436Z","iopub.status.idle":"2025-06-04T04:28:29.604417Z","shell.execute_reply.started":"2025-06-04T04:28:24.985416Z","shell.execute_reply":"2025-06-04T04:28:29.603727Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Correlation Heatmap: A heatmap to show the correlation between all numerical features. This helps identify highly correlated features, which might be redundant. It's especially important to check the correlation between the different sensor axes (e.g., acc_x, acc_y, acc_z).","metadata":{}},{"cell_type":"code","source":"subset = train_ds.sample(n=5000, random_state=42)\nsubset.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:34.758838Z","iopub.execute_input":"2025-06-04T04:28:34.759483Z","iopub.status.idle":"2025-06-04T04:28:34.810220Z","shell.execute_reply.started":"2025-06-04T04:28:34.759452Z","shell.execute_reply":"2025-06-04T04:28:34.809438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# numerical_cols = subset.select_dtypes(include=np.number).columns\n# corr_matrics = subset[numerical_cols].corr()\n# plt.figure(figsize=(14,10))\n# sns.heatmap(corr_matrics, cmap='coolwarm', annot=True)\n# plt.title('Correlation Heatmap of Numerical Features', fontsize=14, fontweight='bold', color='darkblue')\n# plt.show()","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"####  Distribution of Ages","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.histplot(train_ds['age'].dropna(), kde=True)  # Drop NaN values to avoid errors\nplt.title('Distribution of Ages', fontsize=14, fontweight='bold', color='darkorange')\nplt.xlabel('Age')\nplt.ylabel('Frequency')\nplt.gca().set_facecolor('#ecfce5')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:39.219794Z","iopub.execute_input":"2025-06-04T04:28:39.220067Z","iopub.status.idle":"2025-06-04T04:28:40.210502Z","shell.execute_reply.started":"2025-06-04T04:28:39.220051Z","shell.execute_reply":"2025-06-04T04:28:40.209767Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Acc_x by Gesture (Boxplot): Boxplots are used to compare the distribution of acc_x for each gesture. This will help us see if certain gestures have significantly different acceleration profiles.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(14, 8))\nsns.boxplot(x='gesture', y='acc_x', data=train_ds)\nplt.title('Acc_x values for each Gesture', fontsize=14, fontweight='bold', color='darkred')\nplt.xticks(rotation=45, ha='right')\nplt.gca().set_facecolor('#ecfce5')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:43.871849Z","iopub.execute_input":"2025-06-04T04:28:43.872128Z","iopub.status.idle":"2025-06-04T04:28:44.286018Z","shell.execute_reply.started":"2025-06-04T04:28:43.872109Z","shell.execute_reply":"2025-06-04T04:28:44.284999Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Behavior Distribution: A countplot of the 'behavior' column, to understand the types of behaviors recorded.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nsns.countplot(data=train, x='behavior')\nplt.title('Distribution of Behaviors', fontsize=14, fontweight='bold', color='darkgreen')\nplt.xticks(rotation=45, ha='right')\nplt.gca().set_facecolor('#ecfce5')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:28:56.542242Z","iopub.execute_input":"2025-06-04T04:28:56.542530Z","iopub.status.idle":"2025-06-04T04:28:56.777360Z","shell.execute_reply.started":"2025-06-04T04:28:56.542509Z","shell.execute_reply":"2025-06-04T04:28:56.776643Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Time Series Plot: A plot of acc_x values over time (sequence counter) for a single sequence. This will help us understand the temporal nature of the sensor data.\n","metadata":{}},{"cell_type":"code","source":"sequence_id = train_ds['sequence_id'].iloc[0]\nsubset = train_ds[train_ds['sequence_id'] == sequence_id]\n\nplt.figure(figsize=(12, 6))\nplt.plot(subset['sequence_counter'], subset['acc_x'])\nplt.title(f'Time Series of acc_x for sequence_id: {sequence_id}',fontsize=14, fontweight='bold', color='maroon')\nplt.xlabel('Sequence Counter')\nplt.ylabel('acc_x')\nplt.gca().set_facecolor('#ecfce5')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:29:01.955752Z","iopub.execute_input":"2025-06-04T04:29:01.956024Z","iopub.status.idle":"2025-06-04T04:29:02.122795Z","shell.execute_reply.started":"2025-06-04T04:29:01.956005Z","shell.execute_reply":"2025-06-04T04:29:02.122025Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 4. Univariate Analysis Plot","metadata":{}},{"cell_type":"markdown","source":"### Generates histogram, boxplot, and Q-Q plot for each numerical feature in the dataframe\n#### . Includes Skewness and Kurtosis statistics\n#### . Includes mean and median lines in the histogram for reference","metadata":{}},{"cell_type":"code","source":"ordered_colors = [\n    \"#339ae4\", \"#ff7f0e\", \"#2ca02c\", \"#d62728\", \"#9467bd\",\n    \"#c05945\", \"#354e91\", \"#c5d4b1\", \"#e2e228\", \"#22d5e9\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:29:07.083934Z","iopub.execute_input":"2025-06-04T04:29:07.084629Z","iopub.status.idle":"2025-06-04T04:29:07.088077Z","shell.execute_reply.started":"2025-06-04T04:29:07.084582Z","shell.execute_reply":"2025-06-04T04:29:07.087141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# def plot_distribution(data, ordered_colors):\n#     numerical_col = data.columns\n#     extended_color = (ordered_colors * (len(numerical_col) // len(ordered_colors)+1))[:len(numerical_col)]\n\n#     plt.style.use('seaborn-v0_8-whitegrid')\n#     sns.set_context(\"notebook\", font_scale=1.2)\n\n#     for i, col in enumerate(numerical_col):\n#         fig, axes = plt.subplots(1,3, figsize=(18,6), gridspec_kw={'width_ratios': [2, 1, 2]})\n#         fig.suptitle(f'Unvariate Analysis {col} ', weight='bold', fontsize=16, y=1.05)\n\n#         sns.histplot(data=data, kde=True, bins=30, color=extended_color[i], ax=axes[0])\n#         axes[0].set_title(f'{col} Histogram', fontsize=14)\n#         axes[0].set_xlabel(col, fontsize=12)\n#         axes[0].set_ylabel('Frequency', fontsize=12)\n\n#         mean_val = data[col].mean()\n#         median_val = data[col].median()\n#         axes[0].axvline(mean_val, color='purple', linestyle='--', label=f'Mean {mean_val:.2f}')\n#         axes[0].axvline(median_val, color='green', linestyle='-', label=f'Median {median_val:.2f}')\n\n\n#         sns.boxplot(y=data[col], ax=axes[1], color=extended_color[i], flierprops=dict(marker='o', markerfacecolor='red', markersize=8))\n#         axes[1].set_title(f'{col} Box Plot', fontsize=14)\n#         axes[1].set_ylabel(col, fontsize=12)\n\n\n#         stats.probplot(data[col], dist=\"norm\", plot=axes[2] , rvalue=True)\n#         axes[2].set_title(f'{col} Q-Q Plot', fontsize=14)\n#         axes[2].set_xlabel('Theoretical Quantiles', fontsize=12)\n#         axes[2].set_ylabel('Sample Quantiles', fontsize=12)\n\n\n#         skewness = stats.skew(data[col])\n#         kurtosis = stats.kurtosis(data[col])\n#         stats_text = f'{col} Skewness: {skewness:.2f}\\n{col} Kurtosis: {kurtosis:.2f}'\n#         fig.text(0.01, 0.95, stats_text, fontsize=12, bbox=dict(facecolor='white', alpha=0.8, edgecolor='black'))\n\n\n#         plt.tight_layout()\n#         plt.show()\n\n\n# # function calling......................................................\n# subset = train_ds.sample(n=5000, random_state=42)\n\n# numerical_cols = subset.select_dtypes(include=np.number).columns\n# plot_distribution(train_ds[numerical_cols], ordered_colors)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subset_train = train_ds.sample(n=50000, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:29:58.261589Z","iopub.execute_input":"2025-06-04T04:29:58.261910Z","iopub.status.idle":"2025-06-04T04:29:58.444938Z","shell.execute_reply.started":"2025-06-04T04:29:58.261889Z","shell.execute_reply":"2025-06-04T04:29:58.444366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv')\ntest_demo = pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv')\ntest.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:29:37.242578Z","iopub.execute_input":"2025-06-04T04:29:37.242883Z","iopub.status.idle":"2025-06-04T04:29:37.268495Z","shell.execute_reply.started":"2025-06-04T04:29:37.242863Z","shell.execute_reply":"2025-06-04T04:29:37.267966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds = test.merge(test_demo, on='subject', how='left')\ntest_ds.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:29:41.616981Z","iopub.execute_input":"2025-06-04T04:29:41.617721Z","iopub.status.idle":"2025-06-04T04:29:41.625980Z","shell.execute_reply.started":"2025-06-04T04:29:41.617691Z","shell.execute_reply":"2025-06-04T04:29:41.625286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subset_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:05.209418Z","iopub.execute_input":"2025-06-04T04:30:05.209726Z","iopub.status.idle":"2025-06-04T04:30:05.214365Z","shell.execute_reply.started":"2025-06-04T04:30:05.209703Z","shell.execute_reply":"2025-06-04T04:30:05.213698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subset_train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:09.377832Z","iopub.execute_input":"2025-06-04T04:30:09.378107Z","iopub.status.idle":"2025-06-04T04:30:09.457542Z","shell.execute_reply.started":"2025-06-04T04:30:09.378088Z","shell.execute_reply":"2025-06-04T04:30:09.456836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:13.628263Z","iopub.execute_input":"2025-06-04T04:30:13.628525Z","iopub.status.idle":"2025-06-04T04:30:13.635766Z","shell.execute_reply.started":"2025-06-04T04:30:13.628506Z","shell.execute_reply":"2025-06-04T04:30:13.635075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"subset_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:17.094456Z","iopub.execute_input":"2025-06-04T04:30:17.094955Z","iopub.status.idle":"2025-06-04T04:30:17.113734Z","shell.execute_reply.started":"2025-06-04T04:30:17.094936Z","shell.execute_reply":"2025-06-04T04:30:17.113004Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 5 Null value Handling and Preprocessing","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:19.969714Z","iopub.execute_input":"2025-06-04T04:30:19.969995Z","iopub.status.idle":"2025-06-04T04:30:20.166569Z","shell.execute_reply.started":"2025-06-04T04:30:19.969975Z","shell.execute_reply":"2025-06-04T04:30:20.166053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Null value removing.............................\nfor col in subset_train.columns:\n    if subset_train[col].dtype == 'object':\n        subset_train[col]= subset_train[col].fillna(subset_train[col].mode()[0])\n    else:\n        subset_train[col] = subset_train[col].fillna(subset_train[col].mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:23.156543Z","iopub.execute_input":"2025-06-04T04:30:23.157293Z","iopub.status.idle":"2025-06-04T04:30:23.560258Z","shell.execute_reply.started":"2025-06-04T04:30:23.157259Z","shell.execute_reply":"2025-06-04T04:30:23.559664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in test_ds.columns:\n    if test_ds[col].dtype == 'object':\n        test_ds[col]= test_ds[col].fillna(test_ds[col].mode()[0])\n    else:\n        test_ds[col] = test_ds[col].fillna(test_ds[col].mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:25.630082Z","iopub.execute_input":"2025-06-04T04:30:25.630339Z","iopub.status.idle":"2025-06-04T04:30:25.707006Z","shell.execute_reply.started":"2025-06-04T04:30:25.630321Z","shell.execute_reply":"2025-06-04T04:30:25.706432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Encode gesture labels\nle = LabelEncoder()\nsubset_train['gesture_encoded'] = le.fit_transform(subset_train['gesture'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:27.764785Z","iopub.execute_input":"2025-06-04T04:30:27.765338Z","iopub.status.idle":"2025-06-04T04:30:27.779665Z","shell.execute_reply.started":"2025-06-04T04:30:27.765313Z","shell.execute_reply":"2025-06-04T04:30:27.779129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Features and target\nfeatures = ['acc_x', 'acc_y', 'acc_z', 'rot_w', 'rot_x', 'rot_y', 'rot_z']\nX = subset_train[features]\ny = subset_train['gesture_encoded']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:32.861685Z","iopub.execute_input":"2025-06-04T04:30:32.862023Z","iopub.status.idle":"2025-06-04T04:30:32.867799Z","shell.execute_reply.started":"2025-06-04T04:30:32.862005Z","shell.execute_reply":"2025-06-04T04:30:32.867217Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h3 style='color:blue'> Outlier detection and removal using IQR standard deviation</h3>","metadata":{}},{"cell_type":"code","source":"# for col in X.columns:\n#     Q1 = X[col].quantile(0.25)\n#     Q3 = X[col].quantile(0.75)\n#     IQR = Q3 - Q1\n\n#     lower_limit = Q1 - 1.5*IQR\n#     upper_limit = Q3 + 1.5*IQR\n\n#     X[(X[col]<lower_limit)|(X[col]>upper_limit)]","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y , random_state=42, test_size=0.3)\nX_train.shape, X_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:39.204547Z","iopub.execute_input":"2025-06-04T04:30:39.204874Z","iopub.status.idle":"2025-06-04T04:30:39.220377Z","shell.execute_reply.started":"2025-06-04T04:30:39.204849Z","shell.execute_reply":"2025-06-04T04:30:39.219645Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 6 Model traning","metadata":{}},{"cell_type":"markdown","source":"## 6.1 RandomForestClassifier model","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:30:44.557831Z","iopub.execute_input":"2025-06-04T04:30:44.558491Z","iopub.status.idle":"2025-06-04T04:30:44.690671Z","shell.execute_reply.started":"2025-06-04T04:30:44.558463Z","shell.execute_reply":"2025-06-04T04:30:44.690107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"randomF_model = RandomForestClassifier(\n    n_estimators=500, \n    max_depth=10, \n    # min_samples_split=2, \n    # ccp_alpha=0.35, \n    random_state=42,\n    #n_jobs=1,\n    \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:32:50.625210Z","iopub.execute_input":"2025-06-04T04:32:50.625445Z","iopub.status.idle":"2025-06-04T04:32:50.629072Z","shell.execute_reply.started":"2025-06-04T04:32:50.625430Z","shell.execute_reply":"2025-06-04T04:32:50.628445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"randomF_model.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:32:54.615069Z","iopub.execute_input":"2025-06-04T04:32:54.615664Z","iopub.status.idle":"2025-06-04T04:33:24.307566Z","shell.execute_reply.started":"2025-06-04T04:32:54.615642Z","shell.execute_reply":"2025-06-04T04:33:24.306847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predictions\ny_pred = randomF_model.predict(X_val)\n\n\n# Compute valid F1 scores for multiclass\nmacro_f1 = f1_score(y_val, y_pred, average='macro')\nmicro_f1 = f1_score(y_val, y_pred, average='micro')\nfinal_score = (macro_f1 + micro_f1) / 2\n\n# Print results\nprint(f\"Micro F1 Score: {micro_f1:.4f}\")\nprint(f\"Macro F1 Score: {macro_f1:.4f}\")\nprint(f\"Final Score (Average): {final_score:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:33:27.522887Z","iopub.execute_input":"2025-06-04T04:33:27.523554Z","iopub.status.idle":"2025-06-04T04:33:28.807816Z","shell.execute_reply.started":"2025-06-04T04:33:27.523529Z","shell.execute_reply":"2025-06-04T04:33:28.806791Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 6.2 AdaBoostClassifier model","metadata":{}},{"cell_type":"code","source":"ada_model = AdaBoostClassifier(\n    n_estimators=500,\n    learning_rate=0.0001,\n    random_state=42,\n    #algorithm='SAMME'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:33:48.479746Z","iopub.execute_input":"2025-06-04T04:33:48.480016Z","iopub.status.idle":"2025-06-04T04:33:48.483804Z","shell.execute_reply.started":"2025-06-04T04:33:48.479998Z","shell.execute_reply":"2025-06-04T04:33:48.483049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ada_model.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:33:53.506128Z","iopub.execute_input":"2025-06-04T04:33:53.506832Z","iopub.status.idle":"2025-06-04T04:34:16.580067Z","shell.execute_reply.started":"2025-06-04T04:33:53.506805Z","shell.execute_reply":"2025-06-04T04:34:16.579222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predictions\ny_pred_2 = ada_model.predict(X_val)\n\n\n# Compute valid F1 scores for multiclass\nmacro_f1 = f1_score(y_val, y_pred_2, average='macro')\nmicro_f1 = f1_score(y_val, y_pred_2, average='micro')\nfinal_score = (macro_f1 + micro_f1) / 2\n\n# Print results\nprint(f\"Micro F1 Score: {micro_f1:.4f}\")\nprint(f\"Macro F1 Score: {macro_f1:.4f}\")\nprint(f\"Final Score (Average): {final_score:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:34:23.120522Z","iopub.execute_input":"2025-06-04T04:34:23.121188Z","iopub.status.idle":"2025-06-04T04:34:24.894423Z","shell.execute_reply.started":"2025-06-04T04:34:23.121162Z","shell.execute_reply":"2025-06-04T04:34:24.893517Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 6.3 XGBClassifier model","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBClassifier\n\nxgb_model = XGBClassifier(\n    #eta=0.01,\n    #gamma=10,\n    n_estimators = 1000,\n    n_jobs=-1\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:38:47.232098Z","iopub.execute_input":"2025-06-04T04:38:47.232642Z","iopub.status.idle":"2025-06-04T04:38:47.236416Z","shell.execute_reply.started":"2025-06-04T04:38:47.232593Z","shell.execute_reply":"2025-06-04T04:38:47.235515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_model.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:38:51.037680Z","iopub.execute_input":"2025-06-04T04:38:51.038434Z","iopub.status.idle":"2025-06-04T04:39:30.282189Z","shell.execute_reply.started":"2025-06-04T04:38:51.038402Z","shell.execute_reply":"2025-06-04T04:39:30.281532Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predictions\ny_pred_3 = xgb_model.predict(X_val)\n\n\n# Compute valid F1 scores for multiclass\nmacro_f1 = f1_score(y_val, y_pred_3, average='macro')\nmicro_f1 = f1_score(y_val, y_pred_3, average='micro')\nfinal_score = (macro_f1 + micro_f1) / 2\n\n# Print results\nprint(f\"Micro F1 Score: {micro_f1:.4f}\")\nprint(f\"Macro F1 Score: {macro_f1:.4f}\")\nprint(f\"Final Score (Average): {final_score:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:39:34.254707Z","iopub.execute_input":"2025-06-04T04:39:34.255309Z","iopub.status.idle":"2025-06-04T04:39:37.667794Z","shell.execute_reply.started":"2025-06-04T04:39:34.255282Z","shell.execute_reply":"2025-06-04T04:39:37.667070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = accuracy_score(y_val, y_pred_3)\naccuracy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:39:44.822774Z","iopub.execute_input":"2025-06-04T04:39:44.823049Z","iopub.status.idle":"2025-06-04T04:39:44.830016Z","shell.execute_reply.started":"2025-06-04T04:39:44.823030Z","shell.execute_reply":"2025-06-04T04:39:44.829342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !pip install catboost","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from catboost import CatBoostClassifier\n\ncatboost_model = CatBoostClassifier(\n    iterations=100,\n    max_depth=10,\n    learning_rate=0.0002,\n\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:40:03.786825Z","iopub.execute_input":"2025-06-04T04:40:03.787372Z","iopub.status.idle":"2025-06-04T04:40:03.989229Z","shell.execute_reply.started":"2025-06-04T04:40:03.787352Z","shell.execute_reply":"2025-06-04T04:40:03.988653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"catboost_model.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:40:06.784254Z","iopub.execute_input":"2025-06-04T04:40:06.785270Z","iopub.status.idle":"2025-06-04T04:41:06.096184Z","shell.execute_reply.started":"2025-06-04T04:40:06.785244Z","shell.execute_reply":"2025-06-04T04:41:06.095536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predictions\ny_pred_4 = catboost_model.predict(X_val)\n\n\n# Compute valid F1 scores for multiclass\nmacro_f1 = f1_score(y_val, y_pred_4, average='macro')\nmicro_f1 = f1_score(y_val, y_pred_4, average='micro')\nfinal_score = (macro_f1 + micro_f1) / 2\n\n# Print results\nprint(f\"Micro F1 Score: {micro_f1:.4f}\")\nprint(f\"Macro F1 Score: {macro_f1:.4f}\")\nprint(f\"Final Score (Average): {final_score:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:41:29.803158Z","iopub.execute_input":"2025-06-04T04:41:29.803733Z","iopub.status.idle":"2025-06-04T04:41:29.852757Z","shell.execute_reply.started":"2025-06-04T04:41:29.803708Z","shell.execute_reply":"2025-06-04T04:41:29.851900Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 7 model testing","metadata":{}},{"cell_type":"code","source":"test = test_ds[features]\ntest_ds['gesture_encoded'] = xgb_model.predict(test)\n\n# Decode labels\ntest_ds['gesture'] = le.inverse_transform(test_ds['gesture_encoded'])\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:42:18.459615Z","iopub.execute_input":"2025-06-04T04:42:18.459898Z","iopub.status.idle":"2025-06-04T04:42:18.504433Z","shell.execute_reply.started":"2025-06-04T04:42:18.459878Z","shell.execute_reply":"2025-06-04T04:42:18.503888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare submission\nsubmission = test_ds[['sequence_id', 'gesture']].drop_duplicates()\nsubmission.to_csv('submission.csv', index=False)\n\n# Display head of submission\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-04T04:42:22.010185Z","iopub.execute_input":"2025-06-04T04:42:22.010448Z","iopub.status.idle":"2025-06-04T04:42:22.026340Z","shell.execute_reply.started":"2025-06-04T04:42:22.010427Z","shell.execute_reply":"2025-06-04T04:42:22.025579Z"}},"outputs":[],"execution_count":null}]}