{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30823,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:01.01184Z","iopub.execute_input":"2024-12-29T19:14:01.012237Z","iopub.status.idle":"2024-12-29T19:14:01.020572Z","shell.execute_reply.started":"2024-12-29T19:14:01.0122Z","shell.execute_reply":"2024-12-29T19:14:01.01958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')\nimport seaborn as sns\nimport scipy.stats as stats\nfrom scipy.stats import shapiro\nimport optuna\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.cluster import KMeans\nfrom lightgbm import LGBMRegressor\nfrom sklearn.linear_model import TweedieRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.ensemble import HistGradientBoostingRegressor\nfrom sklearn.ensemble import AdaBoostRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.model_selection import GridSearchCV\nimport shap\n\nimport warnings\nwarnings.simplefilter(action='ignore', category=UserWarning)\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:01.021972Z","iopub.execute_input":"2024-12-29T19:14:01.022307Z","iopub.status.idle":"2024-12-29T19:14:11.642401Z","shell.execute_reply.started":"2024-12-29T19:14:01.02226Z","shell.execute_reply":"2024-12-29T19:14:11.641737Z"}},"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')\nsubmission = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:11.644117Z","iopub.execute_input":"2024-12-29T19:14:11.64467Z","iopub.status.idle":"2024-12-29T19:14:20.177641Z","shell.execute_reply.started":"2024-12-29T19:14:11.644647Z","shell.execute_reply":"2024-12-29T19:14:20.17694Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Have a peek of the dataset","metadata":{}},{"cell_type":"code","source":"print(train.info())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:20.178946Z","iopub.execute_input":"2024-12-29T19:14:20.179255Z","iopub.status.idle":"2024-12-29T19:14:20.736764Z","shell.execute_reply.started":"2024-12-29T19:14:20.179222Z","shell.execute_reply":"2024-12-29T19:14:20.735456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test.info())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:20.737887Z","iopub.execute_input":"2024-12-29T19:14:20.738213Z","iopub.status.idle":"2024-12-29T19:14:21.141559Z","shell.execute_reply.started":"2024-12-29T19:14:20.738163Z","shell.execute_reply":"2024-12-29T19:14:21.140619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.142479Z","iopub.execute_input":"2024-12-29T19:14:21.1428Z","iopub.status.idle":"2024-12-29T19:14:21.147232Z","shell.execute_reply.started":"2024-12-29T19:14:21.142767Z","shell.execute_reply":"2024-12-29T19:14:21.146456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.148213Z","iopub.execute_input":"2024-12-29T19:14:21.148475Z","iopub.status.idle":"2024-12-29T19:14:21.160209Z","shell.execute_reply.started":"2024-12-29T19:14:21.148456Z","shell.execute_reply":"2024-12-29T19:14:21.159591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.160989Z","iopub.execute_input":"2024-12-29T19:14:21.161188Z","iopub.status.idle":"2024-12-29T19:14:21.173991Z","shell.execute_reply.started":"2024-12-29T19:14:21.161155Z","shell.execute_reply":"2024-12-29T19:14:21.173383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.176905Z","iopub.execute_input":"2024-12-29T19:14:21.177096Z","iopub.status.idle":"2024-12-29T19:14:21.187774Z","shell.execute_reply.started":"2024-12-29T19:14:21.177079Z","shell.execute_reply":"2024-12-29T19:14:21.187136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.189401Z","iopub.execute_input":"2024-12-29T19:14:21.189592Z","iopub.status.idle":"2024-12-29T19:14:21.225046Z","shell.execute_reply.started":"2024-12-29T19:14:21.189569Z","shell.execute_reply":"2024-12-29T19:14:21.224424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.225851Z","iopub.execute_input":"2024-12-29T19:14:21.226071Z","iopub.status.idle":"2024-12-29T19:14:21.242275Z","shell.execute_reply.started":"2024-12-29T19:14:21.226051Z","shell.execute_reply":"2024-12-29T19:14:21.241483Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Summary Statistics","metadata":{}},{"cell_type":"code","source":"# summarize categorical variable distributions\nobj_cols_tr=[var for var in train.columns if train[var].dtype in ['object']]\ntrain[obj_cols_tr].describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:21.243028Z","iopub.execute_input":"2024-12-29T19:14:21.243327Z","iopub.status.idle":"2024-12-29T19:14:22.878889Z","shell.execute_reply.started":"2024-12-29T19:14:21.243297Z","shell.execute_reply":"2024-12-29T19:14:22.877964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# summarize categorical variable distributions\nobj_cols_te=[var for var in test.columns if test[var].dtype in ['object']]\ntest[obj_cols_te].describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:22.879788Z","iopub.execute_input":"2024-12-29T19:14:22.880087Z","iopub.status.idle":"2024-12-29T19:14:23.984001Z","shell.execute_reply.started":"2024-12-29T19:14:22.880055Z","shell.execute_reply":"2024-12-29T19:14:23.983229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# summarize numerical variable distributions\nnum_cols_tr=[var for var in train.columns if train[var].dtype in ('float','int')]\ntrain[num_cols_tr].describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:23.984843Z","iopub.execute_input":"2024-12-29T19:14:23.985083Z","iopub.status.idle":"2024-12-29T19:14:24.582705Z","shell.execute_reply.started":"2024-12-29T19:14:23.985063Z","shell.execute_reply":"2024-12-29T19:14:24.581844Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# summarize numerical variable distributions\nnum_cols_te=[var for var in test.columns if test[var].dtype in ('float','int')]\ntest[num_cols_te].describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:24.583582Z","iopub.execute_input":"2024-12-29T19:14:24.5839Z","iopub.status.idle":"2024-12-29T19:14:24.937016Z","shell.execute_reply.started":"2024-12-29T19:14:24.583871Z","shell.execute_reply":"2024-12-29T19:14:24.93613Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Cleaning","metadata":{}},{"cell_type":"code","source":"train.drop_duplicates(inplace=True)\nprint(train.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:24.937959Z","iopub.execute_input":"2024-12-29T19:14:24.938306Z","iopub.status.idle":"2024-12-29T19:14:26.395589Z","shell.execute_reply.started":"2024-12-29T19:14:24.938274Z","shell.execute_reply":"2024-12-29T19:14:26.394678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.drop_duplicates(inplace=True)\nprint(test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:26.396585Z","iopub.execute_input":"2024-12-29T19:14:26.396888Z","iopub.status.idle":"2024-12-29T19:14:27.367617Z","shell.execute_reply.started":"2024-12-29T19:14:26.396864Z","shell.execute_reply":"2024-12-29T19:14:27.366795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_tr=train.isnull().sum()/train.shape[0]*100\nmissing_tr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:27.368415Z","iopub.execute_input":"2024-12-29T19:14:27.368697Z","iopub.status.idle":"2024-12-29T19:14:27.895442Z","shell.execute_reply.started":"2024-12-29T19:14:27.368673Z","shell.execute_reply":"2024-12-29T19:14:27.894689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_te=test.isnull().sum()/test.shape[0]*100\nmissing_te","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:27.896117Z","iopub.execute_input":"2024-12-29T19:14:27.89633Z","iopub.status.idle":"2024-12-29T19:14:28.254306Z","shell.execute_reply.started":"2024-12-29T19:14:27.896312Z","shell.execute_reply":"2024-12-29T19:14:28.253527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input missing values with median or mode depending of features class\ntrain['Marital Status'].fillna(train['Marital Status'].mode()[0], inplace=True)\ntrain['Marital Status'] = train['Marital Status'].astype(object)\ntrain['Customer Feedback'].fillna(train['Customer Feedback'].mode()[0], inplace=True)\ntrain['Customer Feedback'] = train['Customer Feedback'].astype(object)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:28.255094Z","iopub.execute_input":"2024-12-29T19:14:28.25537Z","iopub.status.idle":"2024-12-29T19:14:28.539947Z","shell.execute_reply.started":"2024-12-29T19:14:28.255348Z","shell.execute_reply":"2024-12-29T19:14:28.539224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input missing values with median or mode depending of features class\ntest['Marital Status'].fillna(test['Marital Status'].mode()[0], inplace=True)\ntest['Marital Status'] = test['Marital Status'].astype(object)\ntest['Customer Feedback'].fillna(test['Customer Feedback'].mode()[0], inplace=True)\ntest['Customer Feedback'] = test['Customer Feedback'].astype(object)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:28.540631Z","iopub.execute_input":"2024-12-29T19:14:28.540833Z","iopub.status.idle":"2024-12-29T19:14:28.72796Z","shell.execute_reply.started":"2024-12-29T19:14:28.540815Z","shell.execute_reply":"2024-12-29T19:14:28.727259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input missing values with median or mode depending of features class\ntrain['Age'].fillna(train['Age'].median(), inplace=True)\ntrain['Annual Income'].fillna(train['Annual Income'].median(), inplace=True)\ntrain['Number of Dependents'].fillna(train['Number of Dependents'].median(), inplace=True)\ntrain['Health Score'].fillna(train['Health Score'].median(), inplace=True)\ntrain['Credit Score'].fillna(train['Credit Score'].median(), inplace=True)\ntrain['Insurance Duration'].fillna(train['Insurance Duration'].median(), inplace=True)\ntrain['Vehicle Age'].fillna(train['Vehicle Age'].median(), inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:28.728754Z","iopub.execute_input":"2024-12-29T19:14:28.728976Z","iopub.status.idle":"2024-12-29T19:14:28.92259Z","shell.execute_reply.started":"2024-12-29T19:14:28.728956Z","shell.execute_reply":"2024-12-29T19:14:28.921904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input missing values with median or mode depending of features class\ntest['Age'].fillna(test['Age'].median(), inplace=True)\ntest['Annual Income'].fillna(test['Annual Income'].median(), inplace=True)\ntest['Number of Dependents'].fillna(test['Number of Dependents'].median(), inplace=True)\ntest['Health Score'].fillna(test['Health Score'].median(), inplace=True)\ntest['Credit Score'].fillna(test['Credit Score'].median(), inplace=True)\ntest['Insurance Duration'].fillna(test['Insurance Duration'].median(), inplace=True)\ntest['Vehicle Age'].fillna(test['Vehicle Age'].median(), inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:28.923447Z","iopub.execute_input":"2024-12-29T19:14:28.923712Z","iopub.status.idle":"2024-12-29T19:14:29.044584Z","shell.execute_reply.started":"2024-12-29T19:14:28.923691Z","shell.execute_reply":"2024-12-29T19:14:29.043666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input missing values with mode or median when is missing and add a boolean feature \ntrain['Occupation_Missing'] = train['Occupation'].isnull().astype(int)\ntrain['Previous_Claims_Missing'] = train['Previous Claims'].isnull().astype(int)\ntrain['Occupation'].fillna(train['Occupation'].mode()[0], inplace=True)\ntrain['Occupation']=train['Occupation'].astype('object')\ntrain['Previous Claims'].fillna(train['Previous Claims'].median(), inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.045471Z","iopub.execute_input":"2024-12-29T19:14:29.045795Z","iopub.status.idle":"2024-12-29T19:14:29.26371Z","shell.execute_reply.started":"2024-12-29T19:14:29.045762Z","shell.execute_reply":"2024-12-29T19:14:29.262974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input missing values with mode or median when is missing and add a boolean feature \ntest['Occupation_Missing'] = test['Occupation'].isnull().astype(int)\ntest['Previous_Claims_Missing'] = test['Previous Claims'].isnull().astype(int)\ntest['Occupation'].fillna(test['Occupation'].mode()[0], inplace=True)\ntest['Occupation']=test['Occupation'].astype('object')\ntest['Previous Claims'].fillna(test['Previous Claims'].median(), inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.264467Z","iopub.execute_input":"2024-12-29T19:14:29.264709Z","iopub.status.idle":"2024-12-29T19:14:29.414248Z","shell.execute_reply.started":"2024-12-29T19:14:29.264689Z","shell.execute_reply":"2024-12-29T19:14:29.413284Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Functions","metadata":{}},{"cell_type":"code","source":"def plot_target(data, var):\n    plt.rcParams['figure.figsize']=(15,5)\n    plt.suptitle('Premium Amount')\n    plt.subplot(1,3,1)\n    x=data[var]\n    plt.hist(x, color='green',edgecolor='black')\n    plt.title('{} histogram'.format(var))\n    plt.yticks(rotation=45, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n\n    plt.subplot(1,3,2)\n    x=data[var]\n    sns.boxplot(x, color='orange')\n    plt.title('{} boxplot'.format(var))\n    plt.yticks(rotation=45, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n\n    plt.subplot(1,3,3)\n    res=stats.probplot(data[var],plot=plt)\n    plt.title('{} Q-Q plot'.format(var))\n    plt.yticks(rotation=45, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n\n    plt.show()\n    \n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.415227Z","iopub.execute_input":"2024-12-29T19:14:29.415543Z","iopub.status.idle":"2024-12-29T19:14:29.421119Z","shell.execute_reply.started":"2024-12-29T19:14:29.415519Z","shell.execute_reply":"2024-12-29T19:14:29.420503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define function to plot the distribution of the numerical variables\ndef plot_num(data, var):\n    plt.rcParams['figure.figsize']=(15,5)\n    plt.subplot(1,3,1)\n    x=data[var]\n    plt.hist(x,color='green',edgecolor='black')\n    plt.title('{} histogram'.format(var))\n    plt.yticks(rotation=0, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n\n\n    plt.subplot(1,3,2)\n    x=data[var]\n    sns.boxplot(x, color=\"orange\")\n    plt.title('{} boxplot'.format(var))\n    plt.yticks(rotation=0, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n\n\n    plt.subplot(1,3,3)\n    res = stats.probplot(data[var], plot=plt)\n    plt.title('{} Q-Q plot'.format(var))\n    plt.yticks(rotation=0, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.42689Z","iopub.execute_input":"2024-12-29T19:14:29.427116Z","iopub.status.idle":"2024-12-29T19:14:29.443914Z","shell.execute_reply.started":"2024-12-29T19:14:29.427096Z","shell.execute_reply":"2024-12-29T19:14:29.443262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define function to plot scatterplot between the target and numerical variables\ndef plot_scatterplot(data, var):\n    plt.rcParams['figure.figsize']=(15,5)\n    sns.scatterplot(data=data, x=var, y='Premium Amount')\n    plt.suptitle('Premium Amount pr {}'.format(var), fontsize=10)\n    plt.xlabel('{}'.format(var), fontsize=15)\n    plt.ylabel('Premium Amount', fontsize=15)\n    plt.yticks(rotation=45, fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.446282Z","iopub.execute_input":"2024-12-29T19:14:29.446498Z","iopub.status.idle":"2024-12-29T19:14:29.460568Z","shell.execute_reply.started":"2024-12-29T19:14:29.446479Z","shell.execute_reply":"2024-12-29T19:14:29.459696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define function to plot scatterplot between the target and numerical variables\ndef plot_scatterplot(data,var):\n    plt.rcParams['figure.figsize']=(10,5)\n    sns.scatterplot(data=data, x=var, y='Premium Amount')\n    plt.suptitle('Premium Amount Distribution per {}'.format(var),fontsize=10)\n    plt.xlabel('{}'.format(var), fontsize=15)\n    plt.ylabel('premium Amount', fontsize=15)\n    plt.yticks(rotation=0,fontsize=15)\n    plt.xticks(rotation=45, fontsize=15)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.461323Z","iopub.execute_input":"2024-12-29T19:14:29.461598Z","iopub.status.idle":"2024-12-29T19:14:29.474464Z","shell.execute_reply.started":"2024-12-29T19:14:29.461578Z","shell.execute_reply":"2024-12-29T19:14:29.473639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define function to plot the distribution of the categorical variables\ndef plot_cat(data, var):\n    plt.rcParams['figure.figsize']=(15,5)\n    sns.countplot(x=data[var], data=data).set_title(\"Barplot {} Variable Distribution\".format(var))\n    plt.yticks(rotation=0, fontsize=5)\n    plt.xticks(rotation=90, fontsize=10)\n    plt.show()\n     ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.475353Z","iopub.execute_input":"2024-12-29T19:14:29.475597Z","iopub.status.idle":"2024-12-29T19:14:29.491413Z","shell.execute_reply.started":"2024-12-29T19:14:29.475577Z","shell.execute_reply":"2024-12-29T19:14:29.490703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define function to plot boxplot between the target and categorical variables\ndef plot_boxplot(data, var):\n    plt.rcParams['figure.figsize']=(20,10)\n    sns.boxplot(x=data[var], y='Premium Amount', linewidth=2, palette=\"Set1\", data=data)\n    plt.suptitle('Premium Amount Distribution per {}'.format(var),fontsize=10)\n    plt.xlabel('{}'.format(var), fontsize=15)\n    plt.ylabel('Premium Amount', fontsize=15)\n    plt.yticks(rotation=0,fontsize=15)\n    plt.xticks(rotation=90, fontsize=15)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.492117Z","iopub.execute_input":"2024-12-29T19:14:29.492355Z","iopub.status.idle":"2024-12-29T19:14:29.503879Z","shell.execute_reply.started":"2024-12-29T19:14:29.492337Z","shell.execute_reply":"2024-12-29T19:14:29.502993Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Target Analysis","metadata":{}},{"cell_type":"code","source":"# split train and target variable\nX_tr=train.copy()\ny=X_tr['Premium Amount']\nX_tr.drop(['Premium Amount', 'id'], axis=1, inplace=True)\ntest.drop(['id'], axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:29.504865Z","iopub.execute_input":"2024-12-29T19:14:29.50513Z","iopub.status.idle":"2024-12-29T19:14:30.412359Z","shell.execute_reply.started":"2024-12-29T19:14:29.505109Z","shell.execute_reply":"2024-12-29T19:14:30.411632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Univariate analysis looking at Mean, Variance, Standard Deviation, Skewness and Kurtosis\nprint(y.name,\n      '\\nMean :', np.mean(y),\n      '\\nVariance :', np.var(y),\n      '\\nStandard Deviation :', np.var(y)**0.5,\n      '\\nSkewness :', stats.skew(y),\n      '\\nKurtosis :', stats.kurtosis(y))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:30.413133Z","iopub.execute_input":"2024-12-29T19:14:30.413426Z","iopub.status.idle":"2024-12-29T19:14:30.463026Z","shell.execute_reply.started":"2024-12-29T19:14:30.413389Z","shell.execute_reply":"2024-12-29T19:14:30.462294Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_target(train, var='Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:30.463816Z","iopub.execute_input":"2024-12-29T19:14:30.46412Z","iopub.status.idle":"2024-12-29T19:14:33.488053Z","shell.execute_reply.started":"2024-12-29T19:14:30.464094Z","shell.execute_reply":"2024-12-29T19:14:33.487189Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Numerical Variable Analysis","metadata":{}},{"cell_type":"code","source":"# Select numerical features\nnumerical_cols=[var for var in X_tr.columns if X_tr[var].dtype in ['int64','float64']]\n# Subset with numerical columns\nnum_tr=X_tr[numerical_cols]\nnum_te=test[numerical_cols]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:33.489219Z","iopub.execute_input":"2024-12-29T19:14:33.489558Z","iopub.status.idle":"2024-12-29T19:14:33.54621Z","shell.execute_reply.started":"2024-12-29T19:14:33.489525Z","shell.execute_reply":"2024-12-29T19:14:33.545472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot distributions of numerical features\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:33.546989Z","iopub.execute_input":"2024-12-29T19:14:33.547319Z","iopub.status.idle":"2024-12-29T19:14:33.550895Z","shell.execute_reply.started":"2024-12-29T19:14:33.547292Z","shell.execute_reply":"2024-12-29T19:14:33.550022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:33.551702Z","iopub.execute_input":"2024-12-29T19:14:33.551905Z","iopub.status.idle":"2024-12-29T19:14:36.318793Z","shell.execute_reply.started":"2024-12-29T19:14:33.551887Z","shell.execute_reply":"2024-12-29T19:14:36.317927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:36.31977Z","iopub.execute_input":"2024-12-29T19:14:36.320106Z","iopub.status.idle":"2024-12-29T19:14:38.428427Z","shell.execute_reply.started":"2024-12-29T19:14:36.320073Z","shell.execute_reply":"2024-12-29T19:14:38.427508Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Annual Income')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:38.429127Z","iopub.execute_input":"2024-12-29T19:14:38.429374Z","iopub.status.idle":"2024-12-29T19:14:41.45336Z","shell.execute_reply.started":"2024-12-29T19:14:38.429354Z","shell.execute_reply":"2024-12-29T19:14:41.452487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Annual Income')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:41.454108Z","iopub.execute_input":"2024-12-29T19:14:41.454349Z","iopub.status.idle":"2024-12-29T19:14:43.594078Z","shell.execute_reply.started":"2024-12-29T19:14:41.454329Z","shell.execute_reply":"2024-12-29T19:14:43.593283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Number of Dependents')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:43.594935Z","iopub.execute_input":"2024-12-29T19:14:43.595251Z","iopub.status.idle":"2024-12-29T19:14:46.414943Z","shell.execute_reply.started":"2024-12-29T19:14:43.595224Z","shell.execute_reply":"2024-12-29T19:14:46.413979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Number of Dependents')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:46.41558Z","iopub.execute_input":"2024-12-29T19:14:46.415787Z","iopub.status.idle":"2024-12-29T19:14:49.21565Z","shell.execute_reply.started":"2024-12-29T19:14:46.415768Z","shell.execute_reply":"2024-12-29T19:14:49.214705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Health Score')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:49.216536Z","iopub.execute_input":"2024-12-29T19:14:49.216876Z","iopub.status.idle":"2024-12-29T19:14:51.999104Z","shell.execute_reply.started":"2024-12-29T19:14:49.216841Z","shell.execute_reply":"2024-12-29T19:14:51.998259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Health Score')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:51.999953Z","iopub.execute_input":"2024-12-29T19:14:52.000269Z","iopub.status.idle":"2024-12-29T19:14:53.990966Z","shell.execute_reply.started":"2024-12-29T19:14:52.000245Z","shell.execute_reply":"2024-12-29T19:14:53.990035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Previous Claims')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:53.991941Z","iopub.execute_input":"2024-12-29T19:14:53.992259Z","iopub.status.idle":"2024-12-29T19:14:57.028612Z","shell.execute_reply.started":"2024-12-29T19:14:53.992234Z","shell.execute_reply":"2024-12-29T19:14:57.027692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Previous Claims')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:57.029583Z","iopub.execute_input":"2024-12-29T19:14:57.029913Z","iopub.status.idle":"2024-12-29T19:14:59.019321Z","shell.execute_reply.started":"2024-12-29T19:14:57.02988Z","shell.execute_reply":"2024-12-29T19:14:59.01844Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Vehicle Age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:14:59.020276Z","iopub.execute_input":"2024-12-29T19:14:59.020582Z","iopub.status.idle":"2024-12-29T19:15:01.886589Z","shell.execute_reply.started":"2024-12-29T19:14:59.020551Z","shell.execute_reply":"2024-12-29T19:15:01.885724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Vehicle Age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:01.887521Z","iopub.execute_input":"2024-12-29T19:15:01.887823Z","iopub.status.idle":"2024-12-29T19:15:03.923044Z","shell.execute_reply.started":"2024-12-29T19:15:01.8878Z","shell.execute_reply":"2024-12-29T19:15:03.922196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Credit Score')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:03.923925Z","iopub.execute_input":"2024-12-29T19:15:03.924227Z","iopub.status.idle":"2024-12-29T19:15:06.715166Z","shell.execute_reply.started":"2024-12-29T19:15:03.924194Z","shell.execute_reply":"2024-12-29T19:15:06.714259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Credit Score')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:06.715918Z","iopub.execute_input":"2024-12-29T19:15:06.716153Z","iopub.status.idle":"2024-12-29T19:15:08.709484Z","shell.execute_reply.started":"2024-12-29T19:15:06.716122Z","shell.execute_reply":"2024-12-29T19:15:08.708601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_tr, var='Insurance Duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:08.710432Z","iopub.execute_input":"2024-12-29T19:15:08.710765Z","iopub.status.idle":"2024-12-29T19:15:11.582795Z","shell.execute_reply.started":"2024-12-29T19:15:08.710732Z","shell.execute_reply":"2024-12-29T19:15:11.581932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_num(num_te, var='Insurance Duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:11.583742Z","iopub.execute_input":"2024-12-29T19:15:11.584073Z","iopub.status.idle":"2024-12-29T19:15:13.626778Z","shell.execute_reply.started":"2024-12-29T19:15:11.58404Z","shell.execute_reply":"2024-12-29T19:15:13.625939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Combine target variable with numerical features for scatter plots\nnum2= pd.concat([y,num_tr], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:13.627727Z","iopub.execute_input":"2024-12-29T19:15:13.628039Z","iopub.status.idle":"2024-12-29T19:15:13.668828Z","shell.execute_reply.started":"2024-12-29T19:15:13.628007Z","shell.execute_reply":"2024-12-29T19:15:13.668202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot scatterplots between target and numerical features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:13.669534Z","iopub.execute_input":"2024-12-29T19:15:13.669772Z","iopub.status.idle":"2024-12-29T19:15:13.673383Z","shell.execute_reply.started":"2024-12-29T19:15:13.669752Z","shell.execute_reply":"2024-12-29T19:15:13.672484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:13.674075Z","iopub.execute_input":"2024-12-29T19:15:13.674274Z","iopub.status.idle":"2024-12-29T19:15:15.993436Z","shell.execute_reply.started":"2024-12-29T19:15:13.674258Z","shell.execute_reply":"2024-12-29T19:15:15.992551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Annual Income')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:15.994341Z","iopub.execute_input":"2024-12-29T19:15:15.994672Z","iopub.status.idle":"2024-12-29T19:15:18.254224Z","shell.execute_reply.started":"2024-12-29T19:15:15.99463Z","shell.execute_reply":"2024-12-29T19:15:18.253391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Number of Dependents')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:18.255082Z","iopub.execute_input":"2024-12-29T19:15:18.25534Z","iopub.status.idle":"2024-12-29T19:15:20.509535Z","shell.execute_reply.started":"2024-12-29T19:15:18.255317Z","shell.execute_reply":"2024-12-29T19:15:20.508661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Health Score')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:20.510424Z","iopub.execute_input":"2024-12-29T19:15:20.51064Z","iopub.status.idle":"2024-12-29T19:15:22.852386Z","shell.execute_reply.started":"2024-12-29T19:15:20.510621Z","shell.execute_reply":"2024-12-29T19:15:22.851384Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Previous Claims')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:22.853209Z","iopub.execute_input":"2024-12-29T19:15:22.853443Z","iopub.status.idle":"2024-12-29T19:15:25.096773Z","shell.execute_reply.started":"2024-12-29T19:15:22.853421Z","shell.execute_reply":"2024-12-29T19:15:25.095917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Vehicle Age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:25.097602Z","iopub.execute_input":"2024-12-29T19:15:25.097868Z","iopub.status.idle":"2024-12-29T19:15:27.388529Z","shell.execute_reply.started":"2024-12-29T19:15:25.097832Z","shell.execute_reply":"2024-12-29T19:15:27.387714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Credit Score')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:27.389548Z","iopub.execute_input":"2024-12-29T19:15:27.389876Z","iopub.status.idle":"2024-12-29T19:15:29.67745Z","shell.execute_reply.started":"2024-12-29T19:15:27.389843Z","shell.execute_reply":"2024-12-29T19:15:29.67643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_scatterplot(num2, var='Insurance Duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:29.678315Z","iopub.execute_input":"2024-12-29T19:15:29.678608Z","iopub.status.idle":"2024-12-29T19:15:31.947492Z","shell.execute_reply.started":"2024-12-29T19:15:29.678582Z","shell.execute_reply":"2024-12-29T19:15:31.946634Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Categorical Variable Analysis","metadata":{}},{"cell_type":"code","source":"\n# let's have a look at how many labels for categorical features\nfor col in X_tr.columns:\n    if X_tr[col].dtype ==\"object\":\n        print(col, ': ', len(X_tr[col].unique()), ' labels')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:31.948467Z","iopub.execute_input":"2024-12-29T19:15:31.948794Z","iopub.status.idle":"2024-12-29T19:15:32.674478Z","shell.execute_reply.started":"2024-12-29T19:15:31.948759Z","shell.execute_reply":"2024-12-29T19:15:32.673618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select categorical columns with relatively low cardinality (convenient but arbitrary)\ncategorical_cols = [cname for cname in X_tr.columns if\n                    X_tr[cname].nunique() <= 15 and\n                    X_tr[cname].dtype == \"object\"]\ncat_tr=X_tr[categorical_cols]\ncat_te=test[categorical_cols]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:32.675384Z","iopub.execute_input":"2024-12-29T19:15:32.675744Z","iopub.status.idle":"2024-12-29T19:15:33.661482Z","shell.execute_reply.started":"2024-12-29T19:15:32.675693Z","shell.execute_reply":"2024-12-29T19:15:33.66079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot distributions of categorical features\n     ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:33.662305Z","iopub.execute_input":"2024-12-29T19:15:33.662518Z","iopub.status.idle":"2024-12-29T19:15:33.665701Z","shell.execute_reply.started":"2024-12-29T19:15:33.6625Z","shell.execute_reply":"2024-12-29T19:15:33.664831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Gender')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:33.666426Z","iopub.execute_input":"2024-12-29T19:15:33.666654Z","iopub.status.idle":"2024-12-29T19:15:34.689416Z","shell.execute_reply.started":"2024-12-29T19:15:33.666634Z","shell.execute_reply":"2024-12-29T19:15:34.688522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Gender')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:34.690296Z","iopub.execute_input":"2024-12-29T19:15:34.690626Z","iopub.status.idle":"2024-12-29T19:15:35.373975Z","shell.execute_reply.started":"2024-12-29T19:15:34.690592Z","shell.execute_reply":"2024-12-29T19:15:35.372732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Marital Status')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:35.374962Z","iopub.execute_input":"2024-12-29T19:15:35.375392Z","iopub.status.idle":"2024-12-29T19:15:36.222592Z","shell.execute_reply.started":"2024-12-29T19:15:35.375351Z","shell.execute_reply":"2024-12-29T19:15:36.221793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Marital Status')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:36.223503Z","iopub.execute_input":"2024-12-29T19:15:36.223868Z","iopub.status.idle":"2024-12-29T19:15:37.051477Z","shell.execute_reply.started":"2024-12-29T19:15:36.22383Z","shell.execute_reply":"2024-12-29T19:15:37.050544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Education Level')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:37.052483Z","iopub.execute_input":"2024-12-29T19:15:37.052808Z","iopub.status.idle":"2024-12-29T19:15:37.912023Z","shell.execute_reply.started":"2024-12-29T19:15:37.052777Z","shell.execute_reply":"2024-12-29T19:15:37.911052Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Education Level')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:37.913041Z","iopub.execute_input":"2024-12-29T19:15:37.913406Z","iopub.status.idle":"2024-12-29T19:15:38.55378Z","shell.execute_reply.started":"2024-12-29T19:15:37.91337Z","shell.execute_reply":"2024-12-29T19:15:38.552854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Occupation')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:38.554842Z","iopub.execute_input":"2024-12-29T19:15:38.555196Z","iopub.status.idle":"2024-12-29T19:15:39.6919Z","shell.execute_reply.started":"2024-12-29T19:15:38.555143Z","shell.execute_reply":"2024-12-29T19:15:39.690947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Occupation')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:39.69282Z","iopub.execute_input":"2024-12-29T19:15:39.693133Z","iopub.status.idle":"2024-12-29T19:15:40.573131Z","shell.execute_reply.started":"2024-12-29T19:15:39.693108Z","shell.execute_reply":"2024-12-29T19:15:40.572279Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Location')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:40.574027Z","iopub.execute_input":"2024-12-29T19:15:40.574337Z","iopub.status.idle":"2024-12-29T19:15:41.72147Z","shell.execute_reply.started":"2024-12-29T19:15:40.57431Z","shell.execute_reply":"2024-12-29T19:15:41.72056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Location')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:41.722357Z","iopub.execute_input":"2024-12-29T19:15:41.722638Z","iopub.status.idle":"2024-12-29T19:15:42.524303Z","shell.execute_reply.started":"2024-12-29T19:15:41.722603Z","shell.execute_reply":"2024-12-29T19:15:42.523472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Policy Type')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:42.525245Z","iopub.execute_input":"2024-12-29T19:15:42.525587Z","iopub.status.idle":"2024-12-29T19:15:43.561727Z","shell.execute_reply.started":"2024-12-29T19:15:42.525553Z","shell.execute_reply":"2024-12-29T19:15:43.560833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Policy Type')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:43.562694Z","iopub.execute_input":"2024-12-29T19:15:43.562981Z","iopub.status.idle":"2024-12-29T19:15:44.319924Z","shell.execute_reply.started":"2024-12-29T19:15:43.562958Z","shell.execute_reply":"2024-12-29T19:15:44.319108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Customer Feedback')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:44.320765Z","iopub.execute_input":"2024-12-29T19:15:44.321107Z","iopub.status.idle":"2024-12-29T19:15:45.206274Z","shell.execute_reply.started":"2024-12-29T19:15:44.321073Z","shell.execute_reply":"2024-12-29T19:15:45.205381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Customer Feedback')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:45.207153Z","iopub.execute_input":"2024-12-29T19:15:45.207481Z","iopub.status.idle":"2024-12-29T19:15:45.89237Z","shell.execute_reply.started":"2024-12-29T19:15:45.207449Z","shell.execute_reply":"2024-12-29T19:15:45.891461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Smoking Status')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:45.893257Z","iopub.execute_input":"2024-12-29T19:15:45.893511Z","iopub.status.idle":"2024-12-29T19:15:47.058162Z","shell.execute_reply.started":"2024-12-29T19:15:45.893489Z","shell.execute_reply":"2024-12-29T19:15:47.057453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Smoking Status')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:47.066976Z","iopub.execute_input":"2024-12-29T19:15:47.06724Z","iopub.status.idle":"2024-12-29T19:15:47.859547Z","shell.execute_reply.started":"2024-12-29T19:15:47.067217Z","shell.execute_reply":"2024-12-29T19:15:47.858652Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Exercise Frequency')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:47.86259Z","iopub.execute_input":"2024-12-29T19:15:47.862849Z","iopub.status.idle":"2024-12-29T19:15:48.749871Z","shell.execute_reply.started":"2024-12-29T19:15:47.862821Z","shell.execute_reply":"2024-12-29T19:15:48.749097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Exercise Frequency')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:48.750719Z","iopub.execute_input":"2024-12-29T19:15:48.751027Z","iopub.status.idle":"2024-12-29T19:15:49.425678Z","shell.execute_reply.started":"2024-12-29T19:15:48.750996Z","shell.execute_reply":"2024-12-29T19:15:49.424815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_tr, var = 'Property Type')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:49.42648Z","iopub.execute_input":"2024-12-29T19:15:49.426823Z","iopub.status.idle":"2024-12-29T19:15:50.267376Z","shell.execute_reply.started":"2024-12-29T19:15:49.426779Z","shell.execute_reply":"2024-12-29T19:15:50.266625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_cat(cat_te, var = 'Property Type')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:50.268152Z","iopub.execute_input":"2024-12-29T19:15:50.26848Z","iopub.status.idle":"2024-12-29T19:15:50.893647Z","shell.execute_reply.started":"2024-12-29T19:15:50.268448Z","shell.execute_reply":"2024-12-29T19:15:50.892775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Combine target variable with categorical features for box plots\ncat2=pd.concat([y,cat_tr], axis=1)\n     ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:50.894551Z","iopub.execute_input":"2024-12-29T19:15:50.894873Z","iopub.status.idle":"2024-12-29T19:15:50.953017Z","shell.execute_reply.started":"2024-12-29T19:15:50.894845Z","shell.execute_reply":"2024-12-29T19:15:50.952275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Plot boxplots between target and categorical features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:50.953815Z","iopub.execute_input":"2024-12-29T19:15:50.954218Z","iopub.status.idle":"2024-12-29T19:15:50.959169Z","shell.execute_reply.started":"2024-12-29T19:15:50.954155Z","shell.execute_reply":"2024-12-29T19:15:50.957924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Gender')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:50.960351Z","iopub.execute_input":"2024-12-29T19:15:50.960687Z","iopub.status.idle":"2024-12-29T19:15:51.853549Z","shell.execute_reply.started":"2024-12-29T19:15:50.960649Z","shell.execute_reply":"2024-12-29T19:15:51.852655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Marital Status')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:51.854387Z","iopub.execute_input":"2024-12-29T19:15:51.854614Z","iopub.status.idle":"2024-12-29T19:15:52.64201Z","shell.execute_reply.started":"2024-12-29T19:15:51.854594Z","shell.execute_reply":"2024-12-29T19:15:52.641107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Education Level')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:52.642827Z","iopub.execute_input":"2024-12-29T19:15:52.643099Z","iopub.status.idle":"2024-12-29T19:15:53.521668Z","shell.execute_reply.started":"2024-12-29T19:15:52.643077Z","shell.execute_reply":"2024-12-29T19:15:53.520903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Occupation')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:53.522401Z","iopub.execute_input":"2024-12-29T19:15:53.522661Z","iopub.status.idle":"2024-12-29T19:15:54.419489Z","shell.execute_reply.started":"2024-12-29T19:15:53.522638Z","shell.execute_reply":"2024-12-29T19:15:54.418637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Location')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:54.420375Z","iopub.execute_input":"2024-12-29T19:15:54.420707Z","iopub.status.idle":"2024-12-29T19:15:55.29791Z","shell.execute_reply.started":"2024-12-29T19:15:54.420674Z","shell.execute_reply":"2024-12-29T19:15:55.297102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Policy Type')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:55.298694Z","iopub.execute_input":"2024-12-29T19:15:55.298988Z","iopub.status.idle":"2024-12-29T19:15:56.151359Z","shell.execute_reply.started":"2024-12-29T19:15:55.298957Z","shell.execute_reply":"2024-12-29T19:15:56.15051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Customer Feedback')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:56.152399Z","iopub.execute_input":"2024-12-29T19:15:56.152725Z","iopub.status.idle":"2024-12-29T19:15:56.924218Z","shell.execute_reply.started":"2024-12-29T19:15:56.152693Z","shell.execute_reply":"2024-12-29T19:15:56.923319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Smoking Status')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:56.92512Z","iopub.execute_input":"2024-12-29T19:15:56.925448Z","iopub.status.idle":"2024-12-29T19:15:57.811738Z","shell.execute_reply.started":"2024-12-29T19:15:56.925414Z","shell.execute_reply":"2024-12-29T19:15:57.810899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Exercise Frequency')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:57.8125Z","iopub.execute_input":"2024-12-29T19:15:57.812802Z","iopub.status.idle":"2024-12-29T19:15:58.643988Z","shell.execute_reply.started":"2024-12-29T19:15:57.812778Z","shell.execute_reply":"2024-12-29T19:15:58.643077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_boxplot(cat2, var='Property Type')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:58.644946Z","iopub.execute_input":"2024-12-29T19:15:58.645304Z","iopub.status.idle":"2024-12-29T19:15:59.554696Z","shell.execute_reply.started":"2024-12-29T19:15:58.645268Z","shell.execute_reply":"2024-12-29T19:15:59.553862Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Outliers Management","metadata":{}},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Age'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:59.555466Z","iopub.execute_input":"2024-12-29T19:15:59.555762Z","iopub.status.idle":"2024-12-29T19:15:59.633297Z","shell.execute_reply.started":"2024-12-29T19:15:59.555738Z","shell.execute_reply":"2024-12-29T19:15:59.632542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Annual Income'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:59.634043Z","iopub.execute_input":"2024-12-29T19:15:59.634285Z","iopub.status.idle":"2024-12-29T19:15:59.712167Z","shell.execute_reply.started":"2024-12-29T19:15:59.634264Z","shell.execute_reply":"2024-12-29T19:15:59.711486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Number of Dependents'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:59.712958Z","iopub.execute_input":"2024-12-29T19:15:59.713187Z","iopub.status.idle":"2024-12-29T19:15:59.787878Z","shell.execute_reply.started":"2024-12-29T19:15:59.713154Z","shell.execute_reply":"2024-12-29T19:15:59.787129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Health Score'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:59.788694Z","iopub.execute_input":"2024-12-29T19:15:59.788991Z","iopub.status.idle":"2024-12-29T19:15:59.864911Z","shell.execute_reply.started":"2024-12-29T19:15:59.78896Z","shell.execute_reply":"2024-12-29T19:15:59.864241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Previous Claims'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:59.865711Z","iopub.execute_input":"2024-12-29T19:15:59.866021Z","iopub.status.idle":"2024-12-29T19:15:59.941503Z","shell.execute_reply.started":"2024-12-29T19:15:59.865983Z","shell.execute_reply":"2024-12-29T19:15:59.940577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Vehicle Age'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:15:59.942408Z","iopub.execute_input":"2024-12-29T19:15:59.942659Z","iopub.status.idle":"2024-12-29T19:16:00.020877Z","shell.execute_reply.started":"2024-12-29T19:15:59.942637Z","shell.execute_reply":"2024-12-29T19:16:00.019898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Credit Score'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:00.02185Z","iopub.execute_input":"2024-12-29T19:16:00.022102Z","iopub.status.idle":"2024-12-29T19:16:00.094951Z","shell.execute_reply.started":"2024-12-29T19:16:00.022081Z","shell.execute_reply":"2024-12-29T19:16:00.093915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cap residual outliers on numerical feature\ni = 'Insurance Duration'\nq75, q25 = np.percentile(num_tr[i].dropna(), [75 ,25])\niqr = q75 - q25\nmin_val = q25 - (iqr*1.5)\nmax_val = q75 + (iqr*1.5)\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_tr = num_tr.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_tr.loc[num_out_tr[i] < min_val, i] = min_val\nnum_out_tr.loc[num_out_tr[i] > max_val, i] = max_val\n\n# Create a copy of the DataFrame to avoid SettingWithCopyWarning\nnum_out_te = num_te.copy()\n\n# Use .loc to set the values within the IQR range\nnum_out_te.loc[num_out_te[i] < min_val, i] = min_val\nnum_out_te.loc[num_out_te[i] > max_val, i] = max_val\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:00.095974Z","iopub.execute_input":"2024-12-29T19:16:00.09633Z","iopub.status.idle":"2024-12-29T19:16:00.173385Z","shell.execute_reply.started":"2024-12-29T19:16:00.096295Z","shell.execute_reply":"2024-12-29T19:16:00.172694Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Encoding Categorical Variables","metadata":{}},{"cell_type":"code","source":"encoder = OrdinalEncoder()\ncat_encoded_tr=cat_tr.copy()\ncat_encoded_tr.loc[:,'Gender_encoded'] = encoder.fit_transform(cat_encoded_tr[['Gender']])\ncat_encoded_tr.loc[:,'Marital_Status_encoded'] = encoder.fit_transform(cat_encoded_tr[['Marital Status']])\ncat_encoded_tr.loc[:,'Education_Level_encoded'] = encoder.fit_transform(cat_encoded_tr[['Education Level']])\ncat_encoded_tr.loc[:,'Occupation_encoded'] = encoder.fit_transform(cat_encoded_tr[['Occupation']])\ncat_encoded_tr.loc[:,'Location_encoded'] = encoder.fit_transform(cat_encoded_tr[['Location']])\ncat_encoded_tr.loc[:,'Policy_Type_encoded'] = encoder.fit_transform(cat_encoded_tr[['Policy Type']])\ncat_encoded_tr.loc[:,'Customer_Feedback_encoded'] = encoder.fit_transform(cat_encoded_tr[['Customer Feedback']])\ncat_encoded_tr.loc[:,'Smoking_Status_encoded'] = encoder.fit_transform(cat_encoded_tr[['Smoking Status']])\ncat_encoded_tr.loc[:,'Exercise_Frequency_encoded'] = encoder.fit_transform(cat_encoded_tr[['Exercise Frequency']])\ncat_encoded_tr.loc[:,'Property_Type_encoded'] = encoder.fit_transform(cat_encoded_tr[['Property Type']])\ncat_encoded_tr.drop(['Gender','Marital Status','Education Level','Occupation','Location','Policy Type',\n                 'Customer Feedback','Smoking Status','Exercise Frequency','Property Type'], axis=1, inplace=True)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:00.174201Z","iopub.execute_input":"2024-12-29T19:16:00.174524Z","iopub.status.idle":"2024-12-29T19:16:02.496313Z","shell.execute_reply.started":"2024-12-29T19:16:00.174492Z","shell.execute_reply":"2024-12-29T19:16:02.49559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_encoded_te=cat_te.copy()\ncat_encoded_te.loc[:,'Gender_encoded'] = encoder.fit_transform(cat_encoded_te[['Gender']])\ncat_encoded_te.loc[:,'Marital_Status_encoded'] = encoder.fit_transform(cat_encoded_te[['Marital Status']])\ncat_encoded_te.loc[:,'Education_Level_encoded'] = encoder.fit_transform(cat_encoded_te[['Education Level']])\ncat_encoded_te.loc[:,'Occupation_encoded'] = encoder.fit_transform(cat_encoded_te[['Occupation']])\ncat_encoded_te.loc[:,'Location_encoded'] = encoder.fit_transform(cat_encoded_te[['Location']])\ncat_encoded_te.loc[:,'Policy_Type_encoded'] = encoder.fit_transform(cat_encoded_te[['Policy Type']])\ncat_encoded_te.loc[:,'Customer_Feedback_encoded'] = encoder.fit_transform(cat_encoded_te[['Customer Feedback']])\ncat_encoded_te.loc[:,'Smoking_Status_encoded'] = encoder.fit_transform(cat_encoded_te[['Smoking Status']])\ncat_encoded_te.loc[:,'Exercise_Frequency_encoded'] = encoder.fit_transform(cat_encoded_te[['Exercise Frequency']])\ncat_encoded_te.loc[:,'Property_Type_encoded'] = encoder.fit_transform(cat_encoded_te[['Property Type']])\n\ncat_encoded_te.drop(['Gender','Marital Status','Education Level','Occupation','Location','Policy Type',\n                 'Customer Feedback','Smoking Status','Exercise Frequency','Property Type'], axis=1, inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:02.497056Z","iopub.execute_input":"2024-12-29T19:16:02.497302Z","iopub.status.idle":"2024-12-29T19:16:04.073787Z","shell.execute_reply.started":"2024-12-29T19:16:02.49728Z","shell.execute_reply":"2024-12-29T19:16:04.073123Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Combine all processed categorical and numerical features\nX_all_tr=pd.concat([cat_encoded_tr, num_out_tr,train[['Policy Start Date']]], axis=1)\nX_all_te=pd.concat([cat_encoded_te, num_out_te,test[['Policy Start Date']]], axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:04.074487Z","iopub.execute_input":"2024-12-29T19:16:04.0747Z","iopub.status.idle":"2024-12-29T19:16:04.262941Z","shell.execute_reply.started":"2024-12-29T19:16:04.074681Z","shell.execute_reply":"2024-12-29T19:16:04.262236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert 'Policy Start Date' to datetime format and add Policy and date\nX_all_tr['Policy Start Date'] = pd.to_datetime(X_all_tr['Policy Start Date'])\n# Extract year, month and day\nX_all_tr['Year_Start'] = X_all_tr['Policy Start Date'].dt.year\nX_all_tr['Month_Start'] = X_all_tr['Policy Start Date'].dt.month\nX_all_tr['Day_Start'] = X_all_tr['Policy Start Date'].dt.day\n# Add duration to policy start date\n# Convert 'Insurance Duration' to timedelta\nX_all_tr['Duration Timedelta'] = pd.to_timedelta(X_all_tr['Insurance Duration'], unit='D')\nX_all_tr['Policy End Date'] = X_all_tr['Policy Start Date']+X_all_tr['Duration Timedelta']\nX_all_tr['Year_End'] = X_all_tr['Policy Start Date'].dt.year\nX_all_tr['Month_End'] = X_all_tr['Policy Start Date'].dt.month\nX_all_tr['Day_End'] = X_all_tr['Policy Start Date'].dt.day\n# drop policy start date and end date\nX_all_tr.drop(['Policy Start Date','Policy End Date','Duration Timedelta'], axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:04.263654Z","iopub.execute_input":"2024-12-29T19:16:04.263863Z","iopub.status.idle":"2024-12-29T19:16:04.976633Z","shell.execute_reply.started":"2024-12-29T19:16:04.263845Z","shell.execute_reply":"2024-12-29T19:16:04.975988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert 'Policy Start Date' to datetime format\nX_all_te['Policy Start Date'] = pd.to_datetime(X_all_te['Policy Start Date'])\n# Extract year, month and day\nX_all_te['Year_Start'] = X_all_te['Policy Start Date'].dt.year\nX_all_te['Month_Start'] = X_all_te['Policy Start Date'].dt.month\nX_all_te['Day_Start'] = X_all_te['Policy Start Date'].dt.day\n# Add duration to policy start date\n# Convert 'Insurance Duration' to timedelta\nX_all_te['Duration Timedelta'] = pd.to_timedelta(X_all_te['Insurance Duration'], unit='D')\nX_all_te['Policy End Date'] = X_all_te['Policy Start Date']+X_all_te['Duration Timedelta']\nX_all_te['Year_End'] = X_all_te['Policy Start Date'].dt.year\nX_all_te['Month_End'] = X_all_te['Policy Start Date'].dt.month\nX_all_te['Day_End'] = X_all_te['Policy Start Date'].dt.day\n# drop policy start date and end date\nX_all_te.drop(['Policy Start Date','Policy End Date','Duration Timedelta'], axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:04.97741Z","iopub.execute_input":"2024-12-29T19:16:04.977715Z","iopub.status.idle":"2024-12-29T19:16:05.452936Z","shell.execute_reply.started":"2024-12-29T19:16:04.977684Z","shell.execute_reply":"2024-12-29T19:16:05.452241Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Clustering with K-Means","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nscaled_tr = scaler.fit_transform(X_all_tr)\nscaled_te = scaler.fit_transform(X_all_te)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:05.453735Z","iopub.execute_input":"2024-12-29T19:16:05.453946Z","iopub.status.idle":"2024-12-29T19:16:06.354487Z","shell.execute_reply.started":"2024-12-29T19:16:05.453927Z","shell.execute_reply":"2024-12-29T19:16:06.353746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Cluster using different k values\n#inertias = []\n#K = range(1, 11)  # Example range, adjust as necessary\n#for k in K:\n#    kmeans = KMeans(n_clusters=k, random_state=0)\n#    kmeans.fit(scaled_tr)\n#    inertias.append(kmeans.inertia_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:06.355163Z","iopub.execute_input":"2024-12-29T19:16:06.355386Z","iopub.status.idle":"2024-12-29T19:16:06.358797Z","shell.execute_reply.started":"2024-12-29T19:16:06.355367Z","shell.execute_reply":"2024-12-29T19:16:06.357966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot elbow method\n#plt.plot(K, inertias, 'bx-')\n#plt.xlabel('Number of clusters (k)')\n#plt.ylabel('Inertia')\n#plt.title('Elbow Method For Optimal k')\n#plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:06.359472Z","iopub.execute_input":"2024-12-29T19:16:06.35975Z","iopub.status.idle":"2024-12-29T19:16:06.374626Z","shell.execute_reply.started":"2024-12-29T19:16:06.359721Z","shell.execute_reply":"2024-12-29T19:16:06.373969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Cluster using different k values\n#inertias = []\n#K = range(1, 11)  # Example range, adjust as necessary\n#for k in K:\n#    kmeans = KMeans(n_clusters=k, random_state=0)\n#    kmeans.fit(scaled_te)\n#    inertias.append(kmeans.inertia_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:06.375301Z","iopub.execute_input":"2024-12-29T19:16:06.375522Z","iopub.status.idle":"2024-12-29T19:16:06.388563Z","shell.execute_reply.started":"2024-12-29T19:16:06.375502Z","shell.execute_reply":"2024-12-29T19:16:06.387841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot elbow method\n#plt.plot(K, inertias, 'bx-')\n#plt.xlabel('Number of clusters (k)')\n#plt.ylabel('Inertia')\n#plt.title('Elbow Method For Optimal k')\n#plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:06.389361Z","iopub.execute_input":"2024-12-29T19:16:06.389646Z","iopub.status.idle":"2024-12-29T19:16:06.40221Z","shell.execute_reply.started":"2024-12-29T19:16:06.389616Z","shell.execute_reply":"2024-12-29T19:16:06.401557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"clusters_KM = (KMeans(n_clusters=5,\n                           random_state=0)\n            .fit(X_all_tr)\n            .labels_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:06.40281Z","iopub.execute_input":"2024-12-29T19:16:06.403029Z","iopub.status.idle":"2024-12-29T19:16:18.012737Z","shell.execute_reply.started":"2024-12-29T19:16:06.403011Z","shell.execute_reply":"2024-12-29T19:16:18.012009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add the cluster labels to the dataframe\nX_all_tr['Cluster_KM'] = clusters_KM\n\n# Display the first few rows with cluster labels\nX_all_tr.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:18.013565Z","iopub.execute_input":"2024-12-29T19:16:18.013785Z","iopub.status.idle":"2024-12-29T19:16:18.034494Z","shell.execute_reply.started":"2024-12-29T19:16:18.013766Z","shell.execute_reply":"2024-12-29T19:16:18.033842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"clusters_KM = (KMeans(n_clusters=5,\n                           random_state=0)\n            .fit(X_all_te)\n            .labels_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:18.03528Z","iopub.execute_input":"2024-12-29T19:16:18.035496Z","iopub.status.idle":"2024-12-29T19:16:25.10445Z","shell.execute_reply.started":"2024-12-29T19:16:18.035476Z","shell.execute_reply":"2024-12-29T19:16:25.103696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add the cluster labels to the dataframe\nX_all_te['Cluster_KM'] = clusters_KM\n\n# Display the first few rows with cluster labels\nX_all_te.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:25.105209Z","iopub.execute_input":"2024-12-29T19:16:25.105435Z","iopub.status.idle":"2024-12-29T19:16:25.126133Z","shell.execute_reply.started":"2024-12-29T19:16:25.105415Z","shell.execute_reply":"2024-12-29T19:16:25.125392Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Zero Variance Predictors","metadata":{}},{"cell_type":"code","source":"# Find features with variance equal zero or lower than 0.05\nto_drop = [col for col in X_all_tr.columns if np.var(X_all_tr[col]) ==0]\nto_drop","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:25.126926Z","iopub.execute_input":"2024-12-29T19:16:25.127189Z","iopub.status.idle":"2024-12-29T19:16:25.299511Z","shell.execute_reply.started":"2024-12-29T19:16:25.127154Z","shell.execute_reply":"2024-12-29T19:16:25.298428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Drop features with zero variance\nX_all_tr_v = X_all_tr.drop(X_all_tr[to_drop], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:25.300551Z","iopub.execute_input":"2024-12-29T19:16:25.30089Z","iopub.status.idle":"2024-12-29T19:16:25.357355Z","shell.execute_reply.started":"2024-12-29T19:16:25.300845Z","shell.execute_reply":"2024-12-29T19:16:25.356712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Find features with variance equal zero or lower than 0.05\nto_drop = [col for col in X_all_tr.columns if np.var(X_all_tr[col]) ==0]\nto_drop","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:25.358074Z","iopub.execute_input":"2024-12-29T19:16:25.358301Z","iopub.status.idle":"2024-12-29T19:16:25.518799Z","shell.execute_reply.started":"2024-12-29T19:16:25.358282Z","shell.execute_reply":"2024-12-29T19:16:25.518072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Drop features with zero variance\nX_all_te_v = X_all_te.drop(X_all_te[to_drop], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:25.519503Z","iopub.execute_input":"2024-12-29T19:16:25.519723Z","iopub.status.idle":"2024-12-29T19:16:25.559363Z","shell.execute_reply.started":"2024-12-29T19:16:25.519704Z","shell.execute_reply":"2024-12-29T19:16:25.558518Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Normality Test","metadata":{}},{"cell_type":"code","source":"# Perform Normality test\nstat, p = shapiro(X_all_tr_v)\nprint('Statistics=%.3f, p=%.3f' % (stat, p))\n# Interpret the normality test result\nalpha = 0.05\nif p > alpha:\n    print('Sample looks Gaussian (fail to reject H0)')\nelse:\n    print('Sample does not look Gaussian (reject H0)')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:25.560199Z","iopub.execute_input":"2024-12-29T19:16:25.560454Z","iopub.status.idle":"2024-12-29T19:16:27.065526Z","shell.execute_reply.started":"2024-12-29T19:16:25.560433Z","shell.execute_reply":"2024-12-29T19:16:27.064811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Perform Normality test\nstat, p = shapiro(X_all_te_v)\nprint('Statistics=%.3f, p=%.3f' % (stat, p))\n# Interpret the normality test result\nalpha = 0.05\nif p > alpha:\n    print('Sample looks Gaussian (fail to reject H0)')\nelse:\n    print('Sample does not look Gaussian (reject H0)')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:27.0663Z","iopub.execute_input":"2024-12-29T19:16:27.066516Z","iopub.status.idle":"2024-12-29T19:16:28.181528Z","shell.execute_reply.started":"2024-12-29T19:16:27.066498Z","shell.execute_reply":"2024-12-29T19:16:28.180707Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Correlated Predictors","metadata":{}},{"cell_type":"code","source":"# Plot Correlation heatmap\ncorr_matrix = X_all_tr_v.corr(method='spearman')\nsns.set(rc = {'figure.figsize': (30, 30)})\nplt.figure()\nsns.heatmap(corr_matrix, square = True, annot=True, fmt='.2f')\nplt.title('Correlation Heatmap on train set',size=25)\nplt.yticks(fontsize=\"15\")\nplt.xticks(fontsize=\"15\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:28.18244Z","iopub.execute_input":"2024-12-29T19:16:28.182704Z","iopub.status.idle":"2024-12-29T19:16:39.275726Z","shell.execute_reply.started":"2024-12-29T19:16:28.182682Z","shell.execute_reply":"2024-12-29T19:16:39.274485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Select correlated features and removed it\n# Select upper triangle of correlation matrix\nupper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(bool))\n# Find index of feature columns with correlation greater than 0.75\nto_drop = [column for column in upper.columns if any(upper[column].abs() > 0.75)]\nto_drop","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:39.276949Z","iopub.execute_input":"2024-12-29T19:16:39.277301Z","iopub.status.idle":"2024-12-29T19:16:39.287761Z","shell.execute_reply.started":"2024-12-29T19:16:39.277274Z","shell.execute_reply":"2024-12-29T19:16:39.28706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop highly correlated features\nX_all_tr_f = X_all_tr_v.drop(X_all_tr_v[to_drop], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:39.288782Z","iopub.execute_input":"2024-12-29T19:16:39.289081Z","iopub.status.idle":"2024-12-29T19:16:39.381388Z","shell.execute_reply.started":"2024-12-29T19:16:39.289051Z","shell.execute_reply":"2024-12-29T19:16:39.380667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot Correlation heatmap\ncorr_matrix = X_all_te_v.corr(method='spearman')\nsns.set(rc = {'figure.figsize': (30, 30)})\nplt.figure()\nsns.heatmap(corr_matrix, square = True, annot=True, fmt='.2f')\nplt.title('Correlation Heatmap on test set',size=25)\nplt.yticks(fontsize=\"15\")\nplt.xticks(fontsize=\"15\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:39.382188Z","iopub.execute_input":"2024-12-29T19:16:39.382479Z","iopub.status.idle":"2024-12-29T19:16:47.141692Z","shell.execute_reply.started":"2024-12-29T19:16:39.38245Z","shell.execute_reply":"2024-12-29T19:16:47.1408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select correlated features and removed it\n# Select upper triangle of correlation matrix\nupper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(bool))\n# Find index of feature columns with correlation greater than 0.75\nto_drop = [column for column in upper.columns if any(upper[column].abs() > 0.75)]\nto_drop","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.142755Z","iopub.execute_input":"2024-12-29T19:16:47.143083Z","iopub.status.idle":"2024-12-29T19:16:47.156713Z","shell.execute_reply.started":"2024-12-29T19:16:47.143053Z","shell.execute_reply":"2024-12-29T19:16:47.155862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop highly correlated features\nX_all_te_f = X_all_te_v.drop(X_all_te_v[to_drop], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.157596Z","iopub.execute_input":"2024-12-29T19:16:47.157888Z","iopub.status.idle":"2024-12-29T19:16:47.220645Z","shell.execute_reply.started":"2024-12-29T19:16:47.157852Z","shell.execute_reply":"2024-12-29T19:16:47.219704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Duration_tr=X_all_tr_f['Insurance Duration']\nX_all_tr_f_=X_all_tr_f.drop(['Insurance Duration','Previous Claims','Previous_Claims_Missing'], axis=1, inplace=False)\nDuration_te=X_all_te_f['Insurance Duration']\nX_all_te_f_=X_all_te_f.drop(['Insurance Duration','Previous Claims','Previous_Claims_Missing'], axis=1, inplace=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.221547Z","iopub.execute_input":"2024-12-29T19:16:47.221808Z","iopub.status.idle":"2024-12-29T19:16:47.327394Z","shell.execute_reply.started":"2024-12-29T19:16:47.221786Z","shell.execute_reply":"2024-12-29T19:16:47.326422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def RMSLE(y_true: list, y_pred: list) :\n    \"\"\"\n    The Root Mean Squared Log Error (RMSLE) metric using only NumPy\n    \n    :param y_true: The ground truth labels given in the dataset\n    :param y_pred: Our predictions\n    :return: The RMSLE score\n    \"\"\"\n    n = len(y_true)\n    RMSLE = np.sqrt(np.mean(np.square(np.log1p(y_pred) - np.log1p(y_true))))\n    return RMSLE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.328163Z","iopub.execute_input":"2024-12-29T19:16:47.328435Z","iopub.status.idle":"2024-12-29T19:16:47.332814Z","shell.execute_reply.started":"2024-12-29T19:16:47.328413Z","shell.execute_reply":"2024-12-29T19:16:47.331902Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# LightGBM Modelling","metadata":{}},{"cell_type":"markdown","source":"### Fine-tuning the model","metadata":{}},{"cell_type":"code","source":"# Fine-tuning\n\n#def objective(trial):\n#    params = {\n#        'objective':'Gamma',\n#        'random_state': 0,\n#        'n_estimators': trial.suggest_int('n_estimators', 50, 1000),\n#        'learning_rate': trial.suggest_loguniform('learning_rate', 0.01, 0.1),\n#        'max_depth': trial.suggest_int('max_depth', 3, 8),\n#        'num_leaves': trial.suggest_int('num_leaves', 20, 150),\n#        'min_child_samples': trial.suggest_int('min_child_samples', 5, 100),\n#        'n_jobs':-1,\n#        'verbosity': -1\n#    }\n\n#    lgbm = LGBMRegressor(**params)\n\n#    cv=KFold(n_splits=5, shuffle=True)\n\n#    rmsle_scores = []\n\n\n#    for train_index, val_index in cv.split(X_all_tr_f_):\n#        X_tr, X_val, Duration_t, Duration_v = X_all_tr_f_.iloc[train_index], X_all_tr_f_.iloc[val_index], Duration_tr.iloc[train_index], Duration_tr.iloc[val_index]\n#        y_tr, y_val = y.iloc[train_index], y.iloc[val_index]\n\n#        lgbm.fit(X_tr, np.log1p(y_tr),  sample_weight=Duration_t.values)\n#        pred_val = lgbm.predict(X_val)\n\n#        rmsle_score = RMSLE(np.log1p(y_val), pred_val)\n#        rmsle_scores.append(rmsle_score)\n\n#    return np.mean(rmsle_scores)\n\n#study = optuna.create_study(direction='minimize')\n#study.optimize(objective, n_trials=10)\n\n#best_params = study.best_params\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.333651Z","iopub.execute_input":"2024-12-29T19:16:47.333863Z","iopub.status.idle":"2024-12-29T19:16:47.346135Z","shell.execute_reply.started":"2024-12-29T19:16:47.333836Z","shell.execute_reply":"2024-12-29T19:16:47.345282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#best_params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.347083Z","iopub.execute_input":"2024-12-29T19:16:47.347389Z","iopub.status.idle":"2024-12-29T19:16:47.364039Z","shell.execute_reply.started":"2024-12-29T19:16:47.34736Z","shell.execute_reply":"2024-12-29T19:16:47.363257Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Fit & Prediction","metadata":{}},{"cell_type":"code","source":"# Initialize and configure the LGBMRegressor with optimized parameters\nlgbm_tuned = LGBMRegressor(\n        objective='Gamma',\n        n_estimators= 102,\n        learning_rate= 0.057830423022366614,\n        max_depth= 8,\n        min_child_samples=27,\n        num_leaves= 58,\n        n_jobs=-1,\n        verbosity=-1,\n        random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.364707Z","iopub.execute_input":"2024-12-29T19:16:47.364921Z","iopub.status.idle":"2024-12-29T19:16:47.378099Z","shell.execute_reply.started":"2024-12-29T19:16:47.364902Z","shell.execute_reply":"2024-12-29T19:16:47.377527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Perform the train-validation split\nX_train, X_val, y_train, y_val, Duration_t, Duration_v = train_test_split(X_all_tr_f_, y, Duration_tr, test_size=0.2, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.37908Z","iopub.execute_input":"2024-12-29T19:16:47.379375Z","iopub.status.idle":"2024-12-29T19:16:47.674036Z","shell.execute_reply.started":"2024-12-29T19:16:47.379347Z","shell.execute_reply":"2024-12-29T19:16:47.673082Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv=KFold(n_splits=5, shuffle=True)\nparam_grid = {}\nlgbm_model = GridSearchCV(lgbm_tuned,param_grid,cv=cv)\nlgbm_fitted=lgbm_model.fit(X_train, np.log1p(y_train),\n               sample_weight=Duration_t.values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:16:47.674954Z","iopub.execute_input":"2024-12-29T19:16:47.675245Z","iopub.status.idle":"2024-12-29T19:17:16.88705Z","shell.execute_reply.started":"2024-12-29T19:16:47.675211Z","shell.execute_reply":"2024-12-29T19:17:16.886377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_lgbm = []\npredictions_tr_lgbm = lgbm_fitted.predict(X_train)\npredictions_val_lgbm = lgbm_fitted.predict(X_val)\n\nrmsle_train = RMSLE(np.log1p(y_train), predictions_tr_lgbm)\nrmsle_val = RMSLE(np.log1p(y_val), predictions_val_lgbm)\n\nscore_dict = {\n        'rmsle_train': rmsle_train,\n        'rmsle_val': rmsle_val,\n    }\n\nscore_lgbm.append(score_dict)\nscore_lgbm = pd.DataFrame(score_lgbm, columns = ['rmsle_train','rmsle_val'])\nscore_lgbm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:16.887592Z","iopub.execute_input":"2024-12-29T19:17:16.887803Z","iopub.status.idle":"2024-12-29T19:17:19.693556Z","shell.execute_reply.started":"2024-12-29T19:17:16.887783Z","shell.execute_reply":"2024-12-29T19:17:19.692781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_te_lgbm = lgbm_fitted.predict(X_all_te_f_)\npred_te_lgbm = np.expm1(predictions_te_lgbm)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:19.694287Z","iopub.execute_input":"2024-12-29T19:17:19.694572Z","iopub.status.idle":"2024-12-29T19:17:21.582076Z","shell.execute_reply.started":"2024-12-29T19:17:19.694549Z","shell.execute_reply":"2024-12-29T19:17:21.581291Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# XGBoost Modelling","metadata":{}},{"cell_type":"markdown","source":"### Fine-tuning the model","metadata":{}},{"cell_type":"code","source":"# Fine-tuning\n\n#def objective(trial):\n#    params = {\n#        'objective': 'reg:gamma',\n#        'random_state': 0,\n#        'n_estimators': trial.suggest_int('n_estimators', 50, 1000),\n#        'learning_rate': trial.suggest_loguniform('learning_rate', 0.01, 0.1),\n#        'max_depth': trial.suggest_int('max_depth', 3, 8),\n#        'min_child_weight': trial.suggest_int('min_child_weight', 1, 100),  \n#        'subsample': trial.suggest_loguniform('subsample', 0.1, 1.0),  \n#        'colsample_bytree': trial.suggest_loguniform('colsample_bytree', 0.1, 1.0)  \n#    }\n    \n#    xgb = XGBRegressor(**params)\n\n#    cv=KFold(n_splits=5, shuffle=True)\n\n#    rmsle_scores = []\n\n\n#    for train_index, val_index in cv.split(X_all_tr_f_):\n#        X_tr, X_val, Duration_t, Duration_v = X_all_tr_f_.iloc[train_index], X_all_tr_f_.iloc[val_index], Duration_tr.iloc[train_index], Duration_tr.iloc[val_index]\n#        y_tr, y_val = y.iloc[train_index], y.iloc[val_index]\n\n#        xgb.fit(X_tr, np.log1p(y_tr),  sample_weight=Duration_t.values)\n#        pred_val = xgb.predict(X_val)\n\n#        rmsle_score = RMSLE(np.log1p(y_val), pred_val)\n#        rmsle_scores.append(rmsle_score)\n\n#    return np.mean(rmsle_scores)\n\n#study = optuna.create_study(direction='minimize')\n#study.optimize(objective, n_trials=10)\n\n#best_params = study.best_params\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:21.582693Z","iopub.execute_input":"2024-12-29T19:17:21.582947Z","iopub.status.idle":"2024-12-29T19:17:21.586213Z","shell.execute_reply.started":"2024-12-29T19:17:21.582924Z","shell.execute_reply":"2024-12-29T19:17:21.58545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#best_params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:21.587193Z","iopub.execute_input":"2024-12-29T19:17:21.587796Z","iopub.status.idle":"2024-12-29T19:17:21.603946Z","shell.execute_reply.started":"2024-12-29T19:17:21.587758Z","shell.execute_reply":"2024-12-29T19:17:21.603104Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Fit & Prediction","metadata":{}},{"cell_type":"code","source":"# Initialize and configure the XGBRegressor with optimized parameters\nxgb_tuned = XGBRegressor(\n    n_estimators=370,\n    learning_rate=0.037695880815377975,\n    max_depth=8,\n    min_child_weight=22,\n    subsample=0.9256249283300134,\n    colsample_bytree=0.6499546297593973,\n    objective='reg:gamma',\n    verbose=0,\n    random_state=0\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:21.604898Z","iopub.execute_input":"2024-12-29T19:17:21.605118Z","iopub.status.idle":"2024-12-29T19:17:21.617451Z","shell.execute_reply.started":"2024-12-29T19:17:21.605088Z","shell.execute_reply":"2024-12-29T19:17:21.616653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Perform the train-validation split\nX_train, X_val, y_train, y_val, Duration_t, Duration_v = train_test_split(X_all_tr_f_, y, Duration_tr, test_size=0.2, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:21.618139Z","iopub.execute_input":"2024-12-29T19:17:21.618419Z","iopub.status.idle":"2024-12-29T19:17:21.887551Z","shell.execute_reply.started":"2024-12-29T19:17:21.618393Z","shell.execute_reply":"2024-12-29T19:17:21.886781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv=KFold(n_splits=5, shuffle=True)\nparam_grid = {}\nxgb_model = GridSearchCV(xgb_tuned,param_grid,cv=cv)\nxgb_fitted=xgb_model.fit(X_train, np.log1p(y_train),\n               sample_weight=Duration_t.values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:17:21.888264Z","iopub.execute_input":"2024-12-29T19:17:21.888476Z","iopub.status.idle":"2024-12-29T19:19:35.948451Z","shell.execute_reply.started":"2024-12-29T19:17:21.888456Z","shell.execute_reply":"2024-12-29T19:19:35.947726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_xgb = []\npredictions_tr_xgb = xgb_fitted.predict(X_train)\npredictions_val_xgb = xgb_fitted.predict(X_val)\n\nrmsle_train = RMSLE(np.log1p(y_train), predictions_tr_xgb)\nrmsle_val = RMSLE(np.log1p(y_val), predictions_val_xgb)\n\nscore_dict = {\n        'rmsle_train': rmsle_train,\n        'rmsle_val': rmsle_val,\n    }\n\nscore_xgb.append(score_dict)\nscore_xgb = pd.DataFrame(score_xgb, columns = ['rmsle_train','rmsle_val'])\nscore_xgb","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:19:35.948991Z","iopub.execute_input":"2024-12-29T19:19:35.949235Z","iopub.status.idle":"2024-12-29T19:19:41.640695Z","shell.execute_reply.started":"2024-12-29T19:19:35.949212Z","shell.execute_reply":"2024-12-29T19:19:41.639962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_te_xgb = xgb_fitted.predict(X_all_te_f_)\npred_te_xgb = np.expm1(predictions_te_xgb)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:19:41.641625Z","iopub.execute_input":"2024-12-29T19:19:41.641958Z","iopub.status.idle":"2024-12-29T19:19:45.331678Z","shell.execute_reply.started":"2024-12-29T19:19:41.641922Z","shell.execute_reply":"2024-12-29T19:19:45.33093Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# HistGradientBoosting Modelling","metadata":{}},{"cell_type":"markdown","source":"### Fit & Prediction","metadata":{}},{"cell_type":"code","source":"# Initialize and configure the HistGradientBoostingRegressor with optimized parameters\n\nhgbm_tuned = HistGradientBoostingRegressor(\n    random_state=0,\n    verbose=0\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:26:18.841778Z","iopub.execute_input":"2024-12-29T19:26:18.842129Z","iopub.status.idle":"2024-12-29T19:26:18.84601Z","shell.execute_reply.started":"2024-12-29T19:26:18.842106Z","shell.execute_reply":"2024-12-29T19:26:18.845277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Perform the train-validation split\nX_train, X_val, y_train, y_val, Duration_t, Duration_v = train_test_split(X_all_tr_f_, y, Duration_tr, test_size=0.2, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:26:21.655739Z","iopub.execute_input":"2024-12-29T19:26:21.656031Z","iopub.status.idle":"2024-12-29T19:26:21.908721Z","shell.execute_reply.started":"2024-12-29T19:26:21.656008Z","shell.execute_reply":"2024-12-29T19:26:21.908043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv=KFold(n_splits=5, shuffle=True)\nparam_grid = {}\nhgbm_model = GridSearchCV(hgbm_tuned,param_grid,cv=cv)\nhgbm_fitted=hgbm_model.fit(X_train, np.log1p(y_train),\n               sample_weight=Duration_t.values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:26:23.2592Z","iopub.execute_input":"2024-12-29T19:26:23.259486Z","iopub.status.idle":"2024-12-29T19:26:51.228186Z","shell.execute_reply.started":"2024-12-29T19:26:23.259463Z","shell.execute_reply":"2024-12-29T19:26:51.227443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_hgbm = []\npredictions_tr_hgbm = hgbm_fitted.predict(X_train)\npredictions_val_hgbm = hgbm_fitted.predict(X_val)\n\nrmsle_train = RMSLE(np.log1p(y_train), predictions_tr_hgbm)\nrmsle_val = RMSLE(np.log1p(y_val), predictions_val_hgbm)\n\nscore_dict = {\n        'rmsle_train': rmsle_train,\n        'rmsle_val': rmsle_val,\n    }\n\nscore_hgbm.append(score_dict)\nscore_hgbm = pd.DataFrame(score_hgbm, columns = ['rmsle_train','rmsle_val'])\nscore_hgbm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:26:51.228986Z","iopub.execute_input":"2024-12-29T19:26:51.22925Z","iopub.status.idle":"2024-12-29T19:26:53.443957Z","shell.execute_reply.started":"2024-12-29T19:26:51.229227Z","shell.execute_reply":"2024-12-29T19:26:53.443322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_te_hgbm = hgbm_fitted.predict(X_all_te_f_)\npred_te_hgbm = np.expm1(predictions_te_hgbm)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:26:53.44508Z","iopub.execute_input":"2024-12-29T19:26:53.445562Z","iopub.status.idle":"2024-12-29T19:26:54.890708Z","shell.execute_reply.started":"2024-12-29T19:26:53.445536Z","shell.execute_reply":"2024-12-29T19:26:54.890014Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# AdaBoost Modelling","metadata":{}},{"cell_type":"markdown","source":"### Fit & Prediction","metadata":{}},{"cell_type":"code","source":"# Initialize and configure the AdaBoostRegressor with optimized parameters\n\nadaboost_tuned = AdaBoostRegressor(\n    random_state=0\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:28:39.292983Z","iopub.execute_input":"2024-12-29T19:28:39.293339Z","iopub.status.idle":"2024-12-29T19:28:39.297069Z","shell.execute_reply.started":"2024-12-29T19:28:39.293311Z","shell.execute_reply":"2024-12-29T19:28:39.296288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Perform the train-validation split\nX_train, X_val, y_train, y_val, Duration_t, Duration_v = train_test_split(X_all_tr_f_, y, Duration_tr, test_size=0.2, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:28:41.363999Z","iopub.execute_input":"2024-12-29T19:28:41.364393Z","iopub.status.idle":"2024-12-29T19:28:41.642544Z","shell.execute_reply.started":"2024-12-29T19:28:41.364362Z","shell.execute_reply":"2024-12-29T19:28:41.641561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv=KFold(n_splits=5, shuffle=True)\nparam_grid = {}\nadaboost_model = GridSearchCV(adaboost_tuned,param_grid,cv=cv)\nadaboost_fitted=adaboost_model.fit(X_train, np.log1p(y_train),\n               sample_weight=Duration_t.values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:28:43.765278Z","iopub.execute_input":"2024-12-29T19:28:43.765589Z","iopub.status.idle":"2024-12-29T19:30:39.34328Z","shell.execute_reply.started":"2024-12-29T19:28:43.765563Z","shell.execute_reply":"2024-12-29T19:30:39.342282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_adaboost = []\npredictions_tr_adaboost = adaboost_fitted.predict(X_train)\npredictions_val_adaboost = adaboost_fitted.predict(X_val)\n\nrmsle_train = RMSLE(np.log1p(y_train), predictions_tr_adaboost)\nrmsle_val = RMSLE(np.log1p(y_val), predictions_val_adaboost)\n\nscore_dict = {\n        'rmsle_train': rmsle_train,\n        'rmsle_val': rmsle_val,\n    }\n\nscore_adaboost.append(score_dict)\nscore_adaboost = pd.DataFrame(score_adaboost, columns = ['rmsle_train','rmsle_val'])\nscore_adaboost","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:33:38.083344Z","iopub.execute_input":"2024-12-29T19:33:38.083709Z","iopub.status.idle":"2024-12-29T19:33:38.739297Z","shell.execute_reply.started":"2024-12-29T19:33:38.083678Z","shell.execute_reply":"2024-12-29T19:33:38.738442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_te_adaboost = adaboost_fitted.predict(X_all_te_f_)\npred_te_adaboost = np.expm1(predictions_te_adaboost)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:30:40.012048Z","iopub.execute_input":"2024-12-29T19:30:40.012416Z","iopub.status.idle":"2024-12-29T19:30:40.440528Z","shell.execute_reply.started":"2024-12-29T19:30:40.012381Z","shell.execute_reply":"2024-12-29T19:30:40.439796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_1=pred_te_xgb*0.7+pred_te_adaboost*0.1+pred_te_hgbm*0.2\npred=pred_1*0.6+pred_te_lgbm*0.4\npred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:36:53.609937Z","iopub.execute_input":"2024-12-29T19:36:53.61028Z","iopub.status.idle":"2024-12-29T19:36:53.62249Z","shell.execute_reply.started":"2024-12-29T19:36:53.610252Z","shell.execute_reply":"2024-12-29T19:36:53.621762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission['Premium Amount'] = pred\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:37:03.935397Z","iopub.execute_input":"2024-12-29T19:37:03.935694Z","iopub.status.idle":"2024-12-29T19:37:05.284963Z","shell.execute_reply.started":"2024-12-29T19:37:03.935669Z","shell.execute_reply":"2024-12-29T19:37:05.284147Z"}},"outputs":[],"execution_count":null}]}