{"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":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Imports\nimport pandas as pd\nimport numpy as np\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import mean_squared_error\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:26:10.36468Z","iopub.execute_input":"2025-06-14T15:26:10.365072Z","iopub.status.idle":"2025-06-14T15:26:11.813624Z","shell.execute_reply.started":"2025-06-14T15:26:10.36504Z","shell.execute_reply":"2025-06-14T15:26:11.812684Z"}},"outputs":[],"execution_count":2},{"cell_type":"code","source":"#Data\ntrain = pd.read_csv('/kaggle/input/burnout-datathon-ieeecsmuj/train.csv')\ntest = pd.read_csv('/kaggle/input/burnout-datathon-ieeecsmuj/test.csv')\nsample_submission = pd.read_csv('/kaggle/input/burnout-datathon-ieeecsmuj/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:26:34.093831Z","iopub.execute_input":"2025-06-14T15:26:34.094153Z","iopub.status.idle":"2025-06-14T15:26:55.439208Z","shell.execute_reply.started":"2025-06-14T15:26:34.094128Z","shell.execute_reply":"2025-06-14T15:26:55.438316Z"}},"outputs":[],"execution_count":4},{"cell_type":"code","source":"#Dropping columns\ndrop_cols_common = [\n    'Unique ID', 'position', 'points', 'Penalty',\n    'rider', 'Rider_ID', 'team', 'bike',\n    'rider_name', 'team_name', 'bike_name', 'shortname',\n    'weather', 'track', 'air', 'ground', 'circuit_name'\n]\n\ntarget = train['Lap_Time_Seconds']\ntrain_clean = train.drop(columns=drop_cols_common + ['Lap_Time_Seconds'])\ntest_clean = test.drop(columns=drop_cols_common)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:27:04.756504Z","iopub.execute_input":"2025-06-14T15:27:04.756861Z","iopub.status.idle":"2025-06-14T15:27:04.95892Z","shell.execute_reply.started":"2025-06-14T15:27:04.756836Z","shell.execute_reply":"2025-06-14T15:27:04.95794Z"}},"outputs":[],"execution_count":5},{"cell_type":"code","source":"#Features\ncombined = pd.concat([train_clean, test_clean], axis=0)\ncategorical_cols = combined.select_dtypes(include='object').columns\n\nle = LabelEncoder()\nfor col in categorical_cols:\n    combined[col] = le.fit_transform(combined[col].astype(str))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:27:07.591239Z","iopub.execute_input":"2025-06-14T15:27:07.591583Z","iopub.status.idle":"2025-06-14T15:27:10.894474Z","shell.execute_reply.started":"2025-06-14T15:27:07.591556Z","shell.execute_reply":"2025-06-14T15:27:10.893565Z"}},"outputs":[],"execution_count":6},{"cell_type":"code","source":"#Splitting\nX_train = combined.iloc[:len(train)]\nX_test = combined.iloc[len(train):]\ny_train = target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:27:16.118664Z","iopub.execute_input":"2025-06-14T15:27:16.118988Z","iopub.status.idle":"2025-06-14T15:27:16.124529Z","shell.execute_reply.started":"2025-06-14T15:27:16.118964Z","shell.execute_reply":"2025-06-14T15:27:16.123611Z"}},"outputs":[],"execution_count":7},{"cell_type":"code","source":"#Validation\nX_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:27:26.820721Z","iopub.execute_input":"2025-06-14T15:27:26.821041Z","iopub.status.idle":"2025-06-14T15:27:27.894855Z","shell.execute_reply.started":"2025-06-14T15:27:26.821016Z","shell.execute_reply":"2025-06-14T15:27:27.893884Z"}},"outputs":[],"execution_count":8},{"cell_type":"code","source":"#Train Random Firest\nmodel = RandomForestRegressor(\n    n_estimators=300,\n    max_depth=25,\n    min_samples_split=4,\n    min_samples_leaf=2,\n    max_features='sqrt',\n    random_state=42,\n    n_jobs=-1\n)\nmodel.fit(X_tr, y_tr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:27:38.223895Z","iopub.execute_input":"2025-06-14T15:27:38.224506Z","iopub.status.idle":"2025-06-14T15:41:28.77285Z","shell.execute_reply.started":"2025-06-14T15:27:38.224477Z","shell.execute_reply":"2025-06-14T15:41:28.771933Z"}},"outputs":[{"execution_count":9,"output_type":"execute_result","data":{"text/plain":"RandomForestRegressor(max_depth=25, max_features='sqrt', min_samples_leaf=2,\n                      min_samples_split=4, n_estimators=300, n_jobs=-1,\n                      random_state=42)","text/html":"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>RandomForestRegressor(max_depth=25, max_features=&#x27;sqrt&#x27;, min_samples_leaf=2,\n                      min_samples_split=4, n_estimators=300, n_jobs=-1,\n                      random_state=42)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">RandomForestRegressor</label><div class=\"sk-toggleable__content\"><pre>RandomForestRegressor(max_depth=25, max_features=&#x27;sqrt&#x27;, min_samples_leaf=2,\n                      min_samples_split=4, n_estimators=300, n_jobs=-1,\n                      random_state=42)</pre></div></div></div></div></div>"},"metadata":{}}],"execution_count":9},{"cell_type":"code","source":" #Validate\nval_preds = model.predict(X_val)\nval_rmse = np.sqrt(mean_squared_error(y_val, val_preds))\nprint(\"🧠 Validation RMSE (Optimized):\", val_rmse)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:43:05.577282Z","iopub.execute_input":"2025-06-14T15:43:05.577981Z","iopub.status.idle":"2025-06-14T15:43:20.890436Z","shell.execute_reply.started":"2025-06-14T15:43:05.577946Z","shell.execute_reply":"2025-06-14T15:43:20.889379Z"}},"outputs":[{"name":"stdout","text":"🧠 Validation RMSE (Optimized): 4.779688031340994\n","output_type":"stream"}],"execution_count":10},{"cell_type":"code","source":"#Traun Full data\nmodel.fit(X_train, y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T15:43:24.915939Z","iopub.execute_input":"2025-06-14T15:43:24.916259Z","iopub.status.idle":"2025-06-14T16:01:13.938198Z","shell.execute_reply.started":"2025-06-14T15:43:24.916235Z","shell.execute_reply":"2025-06-14T16:01:13.937347Z"}},"outputs":[{"execution_count":11,"output_type":"execute_result","data":{"text/plain":"RandomForestRegressor(max_depth=25, max_features='sqrt', min_samples_leaf=2,\n                      min_samples_split=4, n_estimators=300, n_jobs=-1,\n                      random_state=42)","text/html":"<style>#sk-container-id-2 {color: black;background-color: white;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>RandomForestRegressor(max_depth=25, max_features=&#x27;sqrt&#x27;, min_samples_leaf=2,\n                      min_samples_split=4, n_estimators=300, n_jobs=-1,\n                      random_state=42)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">RandomForestRegressor</label><div class=\"sk-toggleable__content\"><pre>RandomForestRegressor(max_depth=25, max_features=&#x27;sqrt&#x27;, min_samples_leaf=2,\n                      min_samples_split=4, n_estimators=300, n_jobs=-1,\n                      random_state=42)</pre></div></div></div></div></div>"},"metadata":{}}],"execution_count":11},{"cell_type":"code","source":"#submission\ntest_preds = model.predict(X_test)\nsubmission = sample_submission.copy()\nsubmission['Lap_Time_Seconds'] = test_preds\nsubmission.to_csv('solution.csv', index=False)\nprint(\"✅ Submission file 'solution.csv' saved.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-14T16:04:35.678469Z","iopub.execute_input":"2025-06-14T16:04:35.678847Z","iopub.status.idle":"2025-06-14T16:05:00.659687Z","shell.execute_reply.started":"2025-06-14T16:04:35.67882Z","shell.execute_reply":"2025-06-14T16:05:00.658737Z"}},"outputs":[{"name":"stdout","text":"✅ Submission file 'solution.csv' saved.\n","output_type":"stream"}],"execution_count":12}]}