{"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":104857,"databundleVersionId":12651513,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.metrics import mean_squared_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:57:38.297962Z","iopub.execute_input":"2025-06-14T15:57:38.298248Z","iopub.status.idle":"2025-06-14T15:57:38.888423Z","shell.execute_reply.started":"2025-06-14T15:57:38.298223Z","shell.execute_reply":"2025-06-14T15:57:38.887702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv(\"/kaggle/input/burnout-datathon-ieeecsmuj/train.csv\")\ntest_df=pd.read_csv(\"/kaggle/input/burnout-datathon-ieeecsmuj/test.csv\")\nval_df=pd.read_csv(\"/kaggle/input/burnout-datathon-ieeecsmuj/val.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:37:27.506429Z","iopub.execute_input":"2025-06-14T15:37:27.506734Z","iopub.status.idle":"2025-06-14T15:37:50.201694Z","shell.execute_reply.started":"2025-06-14T15:37:27.506708Z","shell.execute_reply":"2025-06-14T15:37:50.200898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_df.shape,test_df.shape,val_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:39:15.006860Z","iopub.execute_input":"2025-06-14T15:39:15.007142Z","iopub.status.idle":"2025-06-14T15:39:15.012728Z","shell.execute_reply.started":"2025-06-14T15:39:15.007122Z","shell.execute_reply":"2025-06-14T15:39:15.011596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.concat([train_df, val_df], ignore_index=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:37:50.202750Z","iopub.execute_input":"2025-06-14T15:37:50.202995Z","iopub.status.idle":"2025-06-14T15:37:50.698939Z","shell.execute_reply.started":"2025-06-14T15:37:50.202972Z","shell.execute_reply":"2025-06-14T15:37:50.698080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:38:51.571882Z","iopub.execute_input":"2025-06-14T15:38:51.572154Z","iopub.status.idle":"2025-06-14T15:38:51.593847Z","shell.execute_reply.started":"2025-06-14T15:38:51.572133Z","shell.execute_reply":"2025-06-14T15:38:51.593033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df .isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:37:50.958000Z","iopub.execute_input":"2025-06-14T15:37:50.958346Z","iopub.status.idle":"2025-06-14T15:37:52.470490Z","shell.execute_reply.started":"2025-06-14T15:37:50.958267Z","shell.execute_reply":"2025-06-14T15:37:52.469667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=train_df.fillna(\"Unknown\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:38:19.403961Z","iopub.execute_input":"2025-06-14T15:38:19.404788Z","iopub.status.idle":"2025-06-14T15:38:21.541676Z","shell.execute_reply.started":"2025-06-14T15:38:19.404755Z","shell.execute_reply":"2025-06-14T15:38:21.540729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:38:31.411155Z","iopub.execute_input":"2025-06-14T15:38:31.411480Z","iopub.status.idle":"2025-06-14T15:38:34.445618Z","shell.execute_reply.started":"2025-06-14T15:38:31.411453Z","shell.execute_reply":"2025-06-14T15:38:34.444801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:39:29.209126Z","iopub.execute_input":"2025-06-14T15:39:29.209430Z","iopub.status.idle":"2025-06-14T15:39:29.216894Z","shell.execute_reply.started":"2025-06-14T15:39:29.209407Z","shell.execute_reply":"2025-06-14T15:39:29.216075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:41:03.844769Z","iopub.execute_input":"2025-06-14T15:41:03.845455Z","iopub.status.idle":"2025-06-14T15:41:06.029625Z","shell.execute_reply.started":"2025-06-14T15:41:03.845422Z","shell.execute_reply":"2025-06-14T15:41:06.028640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select numeric columns\nnumeric_cols = train_df.select_dtypes(include=['number']).columns\n\n# Compute correlation with target\ncorrelations = train_df[numeric_cols].corr()['Lap_Time_Seconds'].drop('Lap_Time_Seconds').sort_values(ascending=False)\n\n# Display correlations\nprint(\"📊 Correlation of numeric features with Lap_Time_Seconds:\\n\")\nprint(correlations)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:53:42.372382Z","iopub.execute_input":"2025-06-14T15:53:42.372703Z","iopub.status.idle":"2025-06-14T15:53:49.648687Z","shell.execute_reply.started":"2025-06-14T15:53:42.372679Z","shell.execute_reply":"2025-06-14T15:53:49.647926Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Key Mapping Approach","metadata":{}},{"cell_type":"code","source":"# Convert key columns to string\nkey_cols = ['rider','circuit_name', 'year_x',\n     'Corners_per_Lap', 'Pit_Stop_Duration_Seconds']\n\nfor col in key_cols:\n    train_df[col] = train_df[col].astype(str)\n    test_df[col] = test_df[col].astype(str)\n\n# Create composite key\ntrain_df['key'] = train_df[key_cols].agg('_'.join, axis=1)\ntest_df['key'] = test_df[key_cols].agg('_'.join, axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:55:12.520214Z","iopub.execute_input":"2025-06-14T15:55:12.521006Z","iopub.status.idle":"2025-06-14T15:55:29.485493Z","shell.execute_reply.started":"2025-06-14T15:55:12.520978Z","shell.execute_reply":"2025-06-14T15:55:29.484575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Step 1: Compute average Lap_Time_Seconds for each key from train_df\nkey_to_avg_lap_time = train_df.groupby('key')['Lap_Time_Seconds'].mean()\n\n# Step 2: Map this to test_df based on the key\ntest_df['calc_lap_time'] = test_df['key'].map(key_to_avg_lap_time)\n\n# Step 3: Count NaN values\nnum_nan = test_df['calc_lap_time'].isna().sum()\n\nprint(f\"❌ Number of NaN values in test_df['calc_lap_time']: {num_nan}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:57:10.059680Z","iopub.execute_input":"2025-06-14T15:57:10.060508Z","iopub.status.idle":"2025-06-14T15:57:10.939573Z","shell.execute_reply.started":"2025-06-14T15:57:10.060422Z","shell.execute_reply":"2025-06-14T15:57:10.938737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['calc_lap_time'] = train_df['key'].map(key_to_avg_lap_time)\n\n# Step 3: Compute RMSE between predicted and actual\nrmse_key_mean = mean_squared_error(train_df['Lap_Time_Seconds'], train_df['calc_lap_time'], squared=False)\n\n# Step 4: Print result\nprint(f\"📏 RMSE using group mean by key in train_df: {rmse_key_mean:.4f} seconds\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:57:42.554008Z","iopub.execute_input":"2025-06-14T15:57:42.554899Z","iopub.status.idle":"2025-06-14T15:57:43.091774Z","shell.execute_reply.started":"2025-06-14T15:57:42.554870Z","shell.execute_reply":"2025-06-14T15:57:43.090962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Step 1: Compute mean Lap_Time_Seconds for each key\nkey_to_avg_lap_time = train_df.groupby('key')['Lap_Time_Seconds'].mean()\n\n# Step 2: Map to test_df\ntest_df['Lap_Time_Seconds'] = test_df['key'].map(key_to_avg_lap_time)\n\n# Step 3: Handle missing keys (optional: fill with global mean or median)\nglobal_mean = train_df['Lap_Time_Seconds'].mean()\ntest_df['Lap_Time_Seconds'] = test_df['Lap_Time_Seconds'].fillna(global_mean)\n\n# Step 4: Create submission DataFrame\nsubmission = test_df[['Unique ID', 'Lap_Time_Seconds']]\n\n# Step 5: Save to CSV\nsubmission.to_csv('submission.csv', index=False)\n\nprint(\"✅ Submission file 'submission.csv' created.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:57:52.631666Z","iopub.execute_input":"2025-06-14T15:57:52.631993Z","iopub.status.idle":"2025-06-14T15:57:54.296671Z","shell.execute_reply.started":"2025-06-14T15:57:52.631968Z","shell.execute_reply":"2025-06-14T15:57:54.295864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}