{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"colab":{"provenance":[],"toc_visible":true},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":115000,"databundleVersionId":13897013,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ***IMPORT***","metadata":{"id":"OHrUzA_2JPQg"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_error\n\nfrom google.colab import drive\n","metadata":{"id":"OlJc9Ka4rH4l","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:03.355277Z","iopub.execute_input":"2025-10-11T03:27:03.355587Z","iopub.status.idle":"2025-10-11T03:27:03.360584Z","shell.execute_reply.started":"2025-10-11T03:27:03.355567Z","shell.execute_reply":"2025-10-11T03:27:03.359567Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ***DATAFRAME CLEANUP***","metadata":{"id":"u8UjEdl9Je2I"}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/train.csv\")\ndf.head()","metadata":{"id":"xxh7Dzvf0Mgd","outputId":"9acef02a-e97f-47f5-e2cb-bdd91cc701bf","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:08.801429Z","iopub.execute_input":"2025-10-11T03:27:08.801715Z","iopub.status.idle":"2025-10-11T03:27:11.358118Z","shell.execute_reply.started":"2025-10-11T03:27:08.801697Z","shell.execute_reply":"2025-10-11T03:27:11.357212Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Age → Median\n\nAnnual Income → Median\n\nMarital Status → 'Single'\n\nNumber of Children → 0\n\nEducation Level → Mode\n\nOccupation → 'Unemployed'\n\nHealth Score → Median\n\nLocation → Mode\n\nPolicy Type → 'Basic'\n\nPrevious Claims → 0\n\nVehicle Age → Median\n\nCredit Score → Median\n\nInsurance Duration → Median\n\nCustomer Feedback → 'Unknown' (later encoded as 3)\n\nSmoking Status → 'No'\n\nExercise Frequency → 'Rarely'\n\nProperty Type → Mode","metadata":{"id":"0hakXfBj8hUs"}},{"cell_type":"code","source":"df['Age'].fillna(df['Age'].median(), inplace=True)\ndf['Annual Income'].fillna(df['Annual Income'].median(), inplace=True)\ndf['Marital Status'].fillna('Single', inplace=True)\ndf['Number of Children'].fillna(0, inplace=True)\ndf['Education Level'].fillna(df['Education Level'].mode()[0], inplace=True)\ndf['Occupation'].fillna('Unemployed', inplace=True)\ndf['Health Score'].fillna(df['Health Score'].median(), inplace=True)\ndf['Location'].fillna(df['Location'].mode()[0], inplace=True)\ndf['Policy Type'].fillna('Basic', inplace=True)\ndf['Previous Claims'].fillna(0, inplace=True)\ndf['Vehicle Age'].fillna(df['Vehicle Age'].median(), inplace=True)\ndf['Credit Score'].fillna(df['Credit Score'].median(), inplace=True)\ndf['Insurance Duration'].fillna(df['Insurance Duration'].median(), inplace=True)\ndf['Customer Feedback'].fillna('Unknown', inplace=True)\ndf['Smoking Status'].fillna('No', inplace=True)\ndf['Exercise Frequency'].fillna('Rarely', inplace=True)\ndf['Property Type'].fillna(df['Property Type'].mode()[0], inplace=True)","metadata":{"id":"YpOIH0dv1ice","outputId":"ea195888-f60e-4ac4-f4f0-41e7224b8da4","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:30.356421Z","iopub.execute_input":"2025-10-11T03:27:30.356716Z","iopub.status.idle":"2025-10-11T03:27:30.851225Z","shell.execute_reply.started":"2025-10-11T03:27:30.356697Z","shell.execute_reply":"2025-10-11T03:27:30.850297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"id":"p6gs-6pv9JOL","outputId":"2ba44a1b-9204-48d7-bd02-57bb02c9b584","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:36.980419Z","iopub.execute_input":"2025-10-11T03:27:36.980789Z","iopub.status.idle":"2025-10-11T03:27:37.308076Z","shell.execute_reply.started":"2025-10-11T03:27:36.980762Z","shell.execute_reply":"2025-10-11T03:27:37.307342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Marital Status'] = df['Marital Status'].map({'Single': 0, 'Married': 1, 'Divorced': 2})\ndf['Education Level'] = df['Education Level'].map({'High School': 0, \"Bachelor's\": 1, \"Master's\": 2, 'PhD': 3})\ndf['Occupation'] = df['Occupation'].map({'Unemployed': 0, 'Self-Employed': 1, 'Employed': 2})\ndf['Location'] = df['Location'].map({'Rural': 0, 'Suburban': 1, 'Urban': 2})\ndf['Policy Type'] = df['Policy Type'].map({'Basic': 0, 'Comprehensive': 1, 'Premium': 2})\ndf['Customer Feedback'] = df['Customer Feedback'].map({'Poor': 0, 'Average': 1, 'Good': 2, 'Unknown': 3})\ndf['Smoking Status'] = df['Smoking Status'].map({'No': 0, 'Yes': 1})\ndf['Exercise Frequency'] = df['Exercise Frequency'].map({'Rarely': 0, 'Monthly': 1, 'Weekly': 2, 'Daily' : 3})\ndf['Property Type'] = df['Property Type'].map({'House': 0, 'Condo': 1, 'Apartment': 2})","metadata":{"id":"teJMXoZx9S6T","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:44.808811Z","iopub.execute_input":"2025-10-11T03:27:44.809080Z","iopub.status.idle":"2025-10-11T03:27:45.187656Z","shell.execute_reply.started":"2025-10-11T03:27:44.809062Z","shell.execute_reply":"2025-10-11T03:27:45.186529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"id":"S7lB7FHgBNO2","outputId":"2e411787-7911-4058-c758-fe6aba1d50c8","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:47.937549Z","iopub.execute_input":"2025-10-11T03:27:47.938726Z","iopub.status.idle":"2025-10-11T03:27:47.957638Z","shell.execute_reply.started":"2025-10-11T03:27:47.938696Z","shell.execute_reply":"2025-10-11T03:27:47.956732Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ***TRAINING & TESTING***","metadata":{"id":"x2Kl6ZEmJpEe"}},{"cell_type":"code","source":"df['Income_per_Child'] = df['Annual Income'] / (df['Number of Children'] + 1)\ndf['Claims_per_Year'] = df['Previous Claims'] / (df['Insurance Duration'] + 1)\ndf['Credit_per_Income'] = df['Credit Score'] / (df['Annual Income'] + 1)\ndf['Health_to_Age'] = df['Health Score'] / (df['Age'] + 1)\ndf['Vehicle_Risk'] = df['Vehicle Age'] * (1 + df['Previous Claims'])\n\nx = df[[\"Income_per_Child\", \"Claims_per_Year\", \"Marital Status\", \"Credit_per_Income\", \"Education Level\", \"Occupation\", \"Health_to_Age\", \"Location\", \"Policy Type\", \"Vehicle_Risk\", \"Credit Score\", \"Insurance Duration\", \"Customer Feedback\", \"Smoking Status\", \"Exercise Frequency\", \"Property Type\"]]\nscaler = StandardScaler()\nx = scaler.fit_transform(x)\n\ny = df[[\"Premium Amount\"]]","metadata":{"id":"7bJ1jWwWB6Sy","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:51.519368Z","iopub.execute_input":"2025-10-11T03:27:51.519675Z","iopub.status.idle":"2025-10-11T03:27:52.050573Z","shell.execute_reply.started":"2025-10-11T03:27:51.519658Z","shell.execute_reply":"2025-10-11T03:27:52.049519Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.5, random_state=42)\n\nmodel = XGBRegressor(\n    n_estimators=1200,\n    learning_rate=0.01,\n    max_depth=8,\n    subsample=0.8,\n    random_state=42,\n    tree_method='hist'\n)\nmodel.fit(x_train, y_train.values.ravel())","metadata":{"id":"fpXOg9m3CkQn","outputId":"f75cab2e-628a-4db7-f463-7df74e6a9f4e","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:27:54.934852Z","iopub.execute_input":"2025-10-11T03:27:54.935103Z","iopub.status.idle":"2025-10-11T03:28:24.267594Z","shell.execute_reply.started":"2025-10-11T03:27:54.935086Z","shell.execute_reply":"2025-10-11T03:28:24.266131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(x_val)\nrmse = np.sqrt(mean_squared_error(y_val, y_pred))\nprint(\"Root Mean Squared Error:\", rmse)","metadata":{"id":"dX6CJvnMDJ7I","outputId":"b8bea57d-53a9-47e8-c191-65889ec4cc5f","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:28:34.745589Z","iopub.execute_input":"2025-10-11T03:28:34.746327Z","iopub.status.idle":"2025-10-11T03:28:43.683032Z","shell.execute_reply.started":"2025-10-11T03:28:34.746291Z","shell.execute_reply":"2025-10-11T03:28:43.682440Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Premium Amount'].describe()","metadata":{"id":"PE8gaVhRQ6Hh","outputId":"1ec4d0f1-94aa-4fef-9748-bf4e1adc1525","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:28:46.585537Z","iopub.execute_input":"2025-10-11T03:28:46.585819Z","iopub.status.idle":"2025-10-11T03:28:46.624045Z","shell.execute_reply.started":"2025-10-11T03:28:46.585800Z","shell.execute_reply":"2025-10-11T03:28:46.623215Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ***SUBMISSION***","metadata":{"id":"2VHrXFh_FWX5"}},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/test.csv\")\ntest.head()","metadata":{"id":"7GHZIL1AFgsJ","outputId":"7b83a2fb-462e-40fb-ee08-d2b5724f340a","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:28:49.574959Z","iopub.execute_input":"2025-10-11T03:28:49.575552Z","iopub.status.idle":"2025-10-11T03:28:51.049493Z","shell.execute_reply.started":"2025-10-11T03:28:49.575524Z","shell.execute_reply":"2025-10-11T03:28:51.048422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Age'] = test['Age'].fillna(test['Age'].median())\ntest['Annual Income'] = test['Annual Income'].fillna(test['Annual Income'].median())\ntest['Marital Status'] = test['Marital Status'].fillna('Single')\ntest['Number of Children'] = test['Number of Children'].fillna(0)\ntest['Education Level'] = test['Education Level'].fillna(test['Education Level'].mode()[0])\ntest['Occupation'] = test['Occupation'].fillna('Unemployed')\ntest['Health Score'] = test['Health Score'].fillna(test['Health Score'].median())\ntest['Location'] = test['Location'].fillna(test['Location'].mode()[0])\ntest['Policy Type'] = test['Policy Type'].fillna('Basic')\ntest['Previous Claims'] = test['Previous Claims'].fillna(0)\ntest['Vehicle Age'] = test['Vehicle Age'].fillna(test['Vehicle Age'].median())\ntest['Credit Score'] = test['Credit Score'].fillna(test['Credit Score'].median())\ntest['Insurance Duration'] = test['Insurance Duration'].fillna(test['Insurance Duration'].median())\ntest['Customer Feedback'] = test['Customer Feedback'].fillna('Unknown')\ntest['Smoking Status'] = test['Smoking Status'].fillna('No')\ntest['Exercise Frequency'] = test['Exercise Frequency'].fillna('Rarely')\ntest['Property Type'] = test['Property Type'].fillna(test['Property Type'].mode()[0])","metadata":{"id":"hnEKnnJRG2LG","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:28:54.378938Z","iopub.execute_input":"2025-10-11T03:28:54.379235Z","iopub.status.idle":"2025-10-11T03:28:54.772980Z","shell.execute_reply.started":"2025-10-11T03:28:54.379213Z","shell.execute_reply":"2025-10-11T03:28:54.772248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Marital Status'] = test['Marital Status'].map({'Single': 0, 'Married': 1, 'Divorced': 2})\ntest['Education Level'] = test['Education Level'].map({'High School': 0, \"Bachelor's\": 1, \"Master's\": 2, 'PhD': 3})\ntest['Occupation'] = test['Occupation'].map({'Unemployed': 0, 'Self-Employed': 1, 'Employed': 2})\ntest['Location'] = test['Location'].map({'Rural': 0, 'Suburban': 1, 'Urban': 2})\ntest['Policy Type'] = test['Policy Type'].map({'Basic': 0, 'Comprehensive': 1, 'Premium': 2})\ntest['Customer Feedback'] = test['Customer Feedback'].map({'Poor': 0, 'Average': 1, 'Good': 2, 'Unknown': 3})\ntest['Smoking Status'] = test['Smoking Status'].map({'No': 0, 'Yes': 1})\ntest['Exercise Frequency'] = test['Exercise Frequency'].map({'Rarely': 0, 'Monthly': 1, 'Weekly': 2, 'Daily' : 3})\ntest['Property Type'] = test['Property Type'].map({'House': 0, 'Condo': 1, 'Apartment': 2})\ntest.head()","metadata":{"id":"hYXwusYoG46a","outputId":"8333cd01-f923-4478-8d4f-d849542839c7","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:28:57.836831Z","iopub.execute_input":"2025-10-11T03:28:57.837092Z","iopub.status.idle":"2025-10-11T03:28:58.083019Z","shell.execute_reply.started":"2025-10-11T03:28:57.837075Z","shell.execute_reply":"2025-10-11T03:28:58.082239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Income_per_Child'] = test['Annual Income'] / (test['Number of Children'] + 1)\ntest['Claims_per_Year'] = test['Previous Claims'] / (test['Insurance Duration'] + 1)\ntest['Credit_per_Income'] = test['Credit Score'] / (test['Annual Income'] + 1)\ntest['Health_to_Age'] = test['Health Score'] / (test['Age'] + 1)\ntest['Vehicle_Risk'] = test['Vehicle Age'] * (1 + test['Previous Claims'])\n\nx_test = test[[\"Income_per_Child\", \"Claims_per_Year\", \"Marital Status\", \"Credit_per_Income\", \"Education Level\", \"Occupation\", \"Health_to_Age\", \"Location\", \"Policy Type\", \"Vehicle_Risk\", \"Credit Score\", \"Insurance Duration\", \"Customer Feedback\", \"Smoking Status\", \"Exercise Frequency\", \"Property Type\"]]\nscaler = StandardScaler()\nx_test = scaler.fit_transform(x_test)","metadata":{"id":"2dvdk1pzHg73","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:29:41.618569Z","iopub.execute_input":"2025-10-11T03:29:41.618900Z","iopub.status.idle":"2025-10-11T03:29:41.835741Z","shell.execute_reply.started":"2025-10-11T03:29:41.618880Z","shell.execute_reply":"2025-10-11T03:29:41.834357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_test = model.predict(x_test)","metadata":{"id":"ODfUvj8WH6kw","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:29:28.117586Z","iopub.execute_input":"2025-10-11T03:29:28.117896Z","iopub.status.idle":"2025-10-11T03:29:38.631816Z","shell.execute_reply.started":"2025-10-11T03:29:28.117875Z","shell.execute_reply":"2025-10-11T03:29:38.631204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\"id\": test[\"id\"], \"Premium Amount\": y_pred_test.flatten()})\nsubmission.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"id":"JlVl1u4qIAmh","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:30:31.057284Z","iopub.execute_input":"2025-10-11T03:30:31.057550Z","iopub.status.idle":"2025-10-11T03:30:31.624997Z","shell.execute_reply.started":"2025-10-11T03:30:31.057533Z","shell.execute_reply":"2025-10-11T03:30:31.624246Z"}},"outputs":[],"execution_count":null}]}