{"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":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":31012,"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-10T09:03:09.081096Z","iopub.execute_input":"2025-05-10T09:03:09.081391Z","iopub.status.idle":"2025-05-10T09:03:11.819074Z","shell.execute_reply.started":"2025-05-10T09:03:09.081363Z","shell.execute_reply":"2025-05-10T09:03:11.818183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error,mean_squared_log_error\n\nfrom sklearn.ensemble import RandomForestRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:06:51.370155Z","iopub.execute_input":"2025-05-10T09:06:51.370484Z","iopub.status.idle":"2025-05-10T09:06:51.841073Z","shell.execute_reply.started":"2025-05-10T09:06:51.370459Z","shell.execute_reply":"2025-05-10T09:06:51.840183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:14.916929Z","iopub.execute_input":"2025-05-10T09:03:14.917297Z","iopub.status.idle":"2025-05-10T09:03:26.274668Z","shell.execute_reply.started":"2025-05-10T09:03:14.917275Z","shell.execute_reply":"2025-05-10T09:03:26.273771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.275565Z","iopub.execute_input":"2025-05-10T09:03:26.276013Z","iopub.status.idle":"2025-05-10T09:03:26.327559Z","shell.execute_reply.started":"2025-05-10T09:03:26.275991Z","shell.execute_reply":"2025-05-10T09:03:26.326799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.328461Z","iopub.execute_input":"2025-05-10T09:03:26.328791Z","iopub.status.idle":"2025-05-10T09:03:26.349208Z","shell.execute_reply.started":"2025-05-10T09:03:26.328762Z","shell.execute_reply":"2025-05-10T09:03:26.348107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['source'] = 'train'\ntest['source'] = 'test'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.350181Z","iopub.execute_input":"2025-05-10T09:03:26.350487Z","iopub.status.idle":"2025-05-10T09:03:26.379919Z","shell.execute_reply.started":"2025-05-10T09:03:26.350459Z","shell.execute_reply":"2025-05-10T09:03:26.379043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined = pd.concat([train,test],ignore_index = True )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.380921Z","iopub.execute_input":"2025-05-10T09:03:26.381754Z","iopub.status.idle":"2025-05-10T09:03:26.826352Z","shell.execute_reply.started":"2025-05-10T09:03:26.381716Z","shell.execute_reply":"2025-05-10T09:03:26.825563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.827074Z","iopub.execute_input":"2025-05-10T09:03:26.827286Z","iopub.status.idle":"2025-05-10T09:03:26.835471Z","shell.execute_reply.started":"2025-05-10T09:03:26.827267Z","shell.execute_reply":"2025-05-10T09:03:26.834777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.838357Z","iopub.execute_input":"2025-05-10T09:03:26.838792Z","iopub.status.idle":"2025-05-10T09:03:26.872465Z","shell.execute_reply.started":"2025-05-10T09:03:26.838763Z","shell.execute_reply":"2025-05-10T09:03:26.871566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:26.873458Z","iopub.execute_input":"2025-05-10T09:03:26.874281Z","iopub.status.idle":"2025-05-10T09:03:28.084386Z","shell.execute_reply.started":"2025-05-10T09:03:26.874258Z","shell.execute_reply":"2025-05-10T09:03:28.083563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:28.085366Z","iopub.execute_input":"2025-05-10T09:03:28.085687Z","iopub.status.idle":"2025-05-10T09:03:29.347156Z","shell.execute_reply.started":"2025-05-10T09:03:28.08566Z","shell.execute_reply":"2025-05-10T09:03:29.346355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_col = combined.select_dtypes(include=['object']).columns\ncat_col","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:29.348291Z","iopub.execute_input":"2025-05-10T09:03:29.348637Z","iopub.status.idle":"2025-05-10T09:03:30.491968Z","shell.execute_reply.started":"2025-05-10T09:03:29.348608Z","shell.execute_reply":"2025-05-10T09:03:30.49107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_col = combined.select_dtypes(include=['int64','float64']).columns\nnum_col","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:30.49289Z","iopub.execute_input":"2025-05-10T09:03:30.493131Z","iopub.status.idle":"2025-05-10T09:03:30.668651Z","shell.execute_reply.started":"2025-05-10T09:03:30.493112Z","shell.execute_reply":"2025-05-10T09:03:30.667929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in cat_col:\n    combined[col] = combined[col].fillna(\"unknow\") ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:30.669484Z","iopub.execute_input":"2025-05-10T09:03:30.669766Z","iopub.status.idle":"2025-05-10T09:03:32.421976Z","shell.execute_reply.started":"2025-05-10T09:03:30.669747Z","shell.execute_reply":"2025-05-10T09:03:32.421209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in num_col:\n    combined[col] = combined[col].fillna(combined[col].median())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:32.423178Z","iopub.execute_input":"2025-05-10T09:03:32.423456Z","iopub.status.idle":"2025-05-10T09:03:32.832306Z","shell.execute_reply.started":"2025-05-10T09:03:32.423426Z","shell.execute_reply":"2025-05-10T09:03:32.831365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:32.833251Z","iopub.execute_input":"2025-05-10T09:03:32.833469Z","iopub.status.idle":"2025-05-10T09:03:34.12936Z","shell.execute_reply.started":"2025-05-10T09:03:32.833453Z","shell.execute_reply":"2025-05-10T09:03:34.128293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(combined['source'].unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:34.130186Z","iopub.execute_input":"2025-05-10T09:03:34.130441Z","iopub.status.idle":"2025-05-10T09:03:34.231235Z","shell.execute_reply.started":"2025-05-10T09:03:34.130422Z","shell.execute_reply":"2025-05-10T09:03:34.230367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in num_col:\n    sns.boxplot(x=combined[col])\n    plt.show()\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:34.232351Z","iopub.execute_input":"2025-05-10T09:03:34.232655Z","iopub.status.idle":"2025-05-10T09:03:37.701399Z","shell.execute_reply.started":"2025-05-10T09:03:34.232628Z","shell.execute_reply":"2025-05-10T09:03:37.700662Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy import stats\n\n# Define a function to remove outliers based on Z-score\ndef remove_outliers_z(combined, out_layer, threshold=3):\n    # Iterate over each column in out_layer\n    for column in out_layer:\n        # Calculate Z-scores\n        z_scores = stats.zscore(combined[column])\n        \n        # Filter the data based on Z-score threshold\n        combined = combined[(np.abs(z_scores) < threshold)]\n    \n    return combined\n\n# Example usage for a list of columns\nout_layer = ['Premium Amount', 'Previous Claims', 'Annual Income']  # Replace with your numeric column names\ncombined_cleaned = remove_outliers_z(combined, out_layer)\n\n# Now 'combined_cleaned' is your cleaned data, which you can use for further analysis\nprint(combined_cleaned)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:37.702275Z","iopub.execute_input":"2025-05-10T09:03:37.70261Z","iopub.status.idle":"2025-05-10T09:03:40.819985Z","shell.execute_reply.started":"2025-05-10T09:03:37.70259Z","shell.execute_reply":"2025-05-10T09:03:40.819041Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined = combined_cleaned","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:40.820823Z","iopub.execute_input":"2025-05-10T09:03:40.821077Z","iopub.status.idle":"2025-05-10T09:03:40.872213Z","shell.execute_reply.started":"2025-05-10T09:03:40.821058Z","shell.execute_reply":"2025-05-10T09:03:40.871328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in out_layer:\n    sns.boxplot(x=combined_cleaned[col])\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:40.873145Z","iopub.execute_input":"2025-05-10T09:03:40.873496Z","iopub.status.idle":"2025-05-10T09:03:42.73525Z","shell.execute_reply.started":"2025-05-10T09:03:40.873477Z","shell.execute_reply":"2025-05-10T09:03:42.73426Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scale = StandardScaler()\nnum_col_to_scale = [col for col in num_col if col not in ['Premium Amount', 'id']]\ncombined[num_col_to_scale] = scale.fit_transform(combined[num_col_to_scale])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:42.736086Z","iopub.execute_input":"2025-05-10T09:03:42.736324Z","iopub.status.idle":"2025-05-10T09:03:43.089713Z","shell.execute_reply.started":"2025-05-10T09:03:42.736305Z","shell.execute_reply":"2025-05-10T09:03:43.088789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:43.090673Z","iopub.execute_input":"2025-05-10T09:03:43.090968Z","iopub.status.idle":"2025-05-10T09:03:43.12125Z","shell.execute_reply.started":"2025-05-10T09:03:43.090937Z","shell.execute_reply":"2025-05-10T09:03:43.120041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize the encoder\nencoder = LabelEncoder()\n\n# List of columns to encode, excluding 'source'\ncolumns_to_encode = [col for col in combined.select_dtypes(include=['object']).columns if col != 'source']\n\n# Apply LabelEncoder to the selected columns\nfor col in columns_to_encode:\n    combined[col] = encoder.fit_transform(combined[col])\n\n# Check the encoded columns\nprint(combined.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:43.122492Z","iopub.execute_input":"2025-05-10T09:03:43.122904Z","iopub.status.idle":"2025-05-10T09:03:49.428073Z","shell.execute_reply.started":"2025-05-10T09:03:43.122872Z","shell.execute_reply":"2025-05-10T09:03:49.427014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_cleaned = combined[combined['source'] == 'train'].drop('source', axis=1)\ntest_cleaned = combined[combined['source'] == 'test'].drop('source', axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:49.428921Z","iopub.execute_input":"2025-05-10T09:03:49.429149Z","iopub.status.idle":"2025-05-10T09:03:50.126698Z","shell.execute_reply.started":"2025-05-10T09:03:49.429132Z","shell.execute_reply":"2025-05-10T09:03:50.125786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_cleaned","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:50.127653Z","iopub.execute_input":"2025-05-10T09:03:50.127926Z","iopub.status.idle":"2025-05-10T09:03:50.70897Z","shell.execute_reply.started":"2025-05-10T09:03:50.127908Z","shell.execute_reply":"2025-05-10T09:03:50.708178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_cleaned.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:50.712764Z","iopub.execute_input":"2025-05-10T09:03:50.713286Z","iopub.status.idle":"2025-05-10T09:03:50.734485Z","shell.execute_reply.started":"2025-05-10T09:03:50.713264Z","shell.execute_reply":"2025-05-10T09:03:50.733763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = train_cleaned.drop(\"Premium Amount\", axis=1)\ny = train_cleaned[\"Premium Amount\"]\nX_test_cleaned = test_cleaned.drop(columns=[\"id\"])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:50.735354Z","iopub.execute_input":"2025-05-10T09:03:50.735643Z","iopub.status.idle":"2025-05-10T09:03:50.909262Z","shell.execute_reply.started":"2025-05-10T09:03:50.735623Z","shell.execute_reply":"2025-05-10T09:03:50.908553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:50.910037Z","iopub.execute_input":"2025-05-10T09:03:50.910242Z","iopub.status.idle":"2025-05-10T09:03:50.915287Z","shell.execute_reply.started":"2025-05-10T09:03:50.910226Z","shell.execute_reply":"2025-05-10T09:03:50.914358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:03:50.916308Z","iopub.execute_input":"2025-05-10T09:03:50.916643Z","iopub.status.idle":"2025-05-10T09:03:51.473591Z","shell.execute_reply.started":"2025-05-10T09:03:50.916612Z","shell.execute_reply":"2025-05-10T09:03:51.472768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = RandomForestRegressor(\n    n_estimators=200,\n    max_depth=15,\n    random_state=42,\n    n_jobs=-1\n)\n\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:07:44.927135Z","iopub.execute_input":"2025-05-10T09:07:44.927437Z","iopub.status.idle":"2025-05-10T09:20:49.777239Z","shell.execute_reply.started":"2025-05-10T09:07:44.927416Z","shell.execute_reply":"2025-05-10T09:20:49.776371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)\nrmse = mean_squared_error(y_test, y_pred, squared=False)\nprint(f\"RMSE: {rmse:.2f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:20:56.553892Z","iopub.execute_input":"2025-05-10T09:20:56.554177Z","iopub.status.idle":"2025-05-10T09:20:59.187036Z","shell.execute_reply.started":"2025-05-10T09:20:56.554158Z","shell.execute_reply":"2025-05-10T09:20:59.18628Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def rmsle(y_true, y_pred):\n    y_pred = np.maximum(y_pred, 0)\n    return np.sqrt(mean_squared_log_error(y_true, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:21:02.01379Z","iopub.execute_input":"2025-05-10T09:21:02.014088Z","iopub.status.idle":"2025-05-10T09:21:02.019157Z","shell.execute_reply.started":"2025-05-10T09:21:02.014069Z","shell.execute_reply":"2025-05-10T09:21:02.018226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rmsle_score = rmsle(y_test, y_pred)\nprint(f\"RMSLE: {rmsle_score:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:23:05.571527Z","iopub.execute_input":"2025-05-10T09:23:05.571872Z","iopub.status.idle":"2025-05-10T09:23:05.583236Z","shell.execute_reply.started":"2025-05-10T09:23:05.571852Z","shell.execute_reply":"2025-05-10T09:23:05.581972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:23:08.727132Z","iopub.execute_input":"2025-05-10T09:23:08.727448Z","iopub.status.idle":"2025-05-10T09:23:08.748791Z","shell.execute_reply.started":"2025-05-10T09:23:08.727425Z","shell.execute_reply":"2025-05-10T09:23:08.747935Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': X_test['id'],  # Correct 'id' from the test set\n    'Premium Amount': y_pred  # Predicted 'Premium Amount'\n})\nsubmission = submission[['id', 'Premium Amount']]\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:23:16.757721Z","iopub.execute_input":"2025-05-10T09:23:16.758034Z","iopub.status.idle":"2025-05-10T09:23:17.283594Z","shell.execute_reply.started":"2025-05-10T09:23:16.758013Z","shell.execute_reply":"2025-05-10T09:23:17.282785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T09:23:23.10636Z","iopub.execute_input":"2025-05-10T09:23:23.106736Z","iopub.status.idle":"2025-05-10T09:23:23.120821Z","shell.execute_reply.started":"2025-05-10T09:23:23.106699Z","shell.execute_reply":"2025-05-10T09:23:23.119499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}