{"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":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current sessionos.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:06:54.450932Z","iopub.execute_input":"2025-01-18T11:06:54.451224Z","iopub.status.idle":"2025-01-18T11:06:54.458485Z","shell.execute_reply.started":"2025-01-18T11:06:54.4512Z","shell.execute_reply":"2025-01-18T11:06:54.45762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kaggle_dir='/kaggle/input'\ntrain_df=pd.read_csv(os.path.join(kaggle_dir, dirname, filenames[1]))\ntest_df=pd.read_csv(os.path.join(kaggle_dir, dirname, filenames[2]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:06:59.356764Z","iopub.execute_input":"2025-01-18T11:06:59.357131Z","iopub.status.idle":"2025-01-18T11:07:11.18352Z","shell.execute_reply.started":"2025-01-18T11:06:59.357104Z","shell.execute_reply":"2025-01-18T11:07:11.182301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:21.697651Z","iopub.execute_input":"2025-01-18T11:07:21.698074Z","iopub.status.idle":"2025-01-18T11:07:21.749364Z","shell.execute_reply.started":"2025-01-18T11:07:21.698016Z","shell.execute_reply":"2025-01-18T11:07:21.747887Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:23.04537Z","iopub.execute_input":"2025-01-18T11:07:23.04576Z","iopub.status.idle":"2025-01-18T11:07:23.0747Z","shell.execute_reply.started":"2025-01-18T11:07:23.045728Z","shell.execute_reply":"2025-01-18T11:07:23.073439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:31.50946Z","iopub.execute_input":"2025-01-18T11:07:31.509912Z","iopub.status.idle":"2025-01-18T11:07:32.147457Z","shell.execute_reply.started":"2025-01-18T11:07:31.509878Z","shell.execute_reply":"2025-01-18T11:07:32.146322Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"There are total 21 columns and 1200000 rows in the training dataset. \nThere are some columns with Null values also. ","metadata":{}},{"cell_type":"code","source":"test_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:34.291845Z","iopub.execute_input":"2025-01-18T11:07:34.292245Z","iopub.status.idle":"2025-01-18T11:07:34.715342Z","shell.execute_reply.started":"2025-01-18T11:07:34.292213Z","shell.execute_reply":"2025-01-18T11:07:34.714123Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Lets concat both the data and perform EDA","metadata":{}},{"cell_type":"code","source":"df=pd.concat([train_df, test_df], axis=0).reset_index()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:41.656296Z","iopub.execute_input":"2025-01-18T11:07:41.656663Z","iopub.status.idle":"2025-01-18T11:07:43.396384Z","shell.execute_reply.started":"2025-01-18T11:07:41.656631Z","shell.execute_reply":"2025-01-18T11:07:43.395166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:43.397867Z","iopub.execute_input":"2025-01-18T11:07:43.398205Z","iopub.status.idle":"2025-01-18T11:07:43.422146Z","shell.execute_reply.started":"2025-01-18T11:07:43.398176Z","shell.execute_reply":"2025-01-18T11:07:43.420633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.drop('index', inplace=True, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:07:43.424093Z","iopub.execute_input":"2025-01-18T11:07:43.424423Z","iopub.status.idle":"2025-01-18T11:07:43.725094Z","shell.execute_reply.started":"2025-01-18T11:07:43.424393Z","shell.execute_reply":"2025-01-18T11:07:43.723988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:08:08.988471Z","iopub.execute_input":"2025-01-18T11:08:08.988875Z","iopub.status.idle":"2025-01-18T11:08:10.184906Z","shell.execute_reply.started":"2025-01-18T11:08:08.988841Z","shell.execute_reply":"2025-01-18T11:08:10.183408Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Exploratory Data Analysis**","metadata":{}},{"cell_type":"markdown","source":"Lets understand the data in all the columns, its distribution and relationship with other columns ","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nimport seaborn as sns\n\n%matplotlib inline ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:08:34.397097Z","iopub.execute_input":"2025-01-18T11:08:34.397521Z","iopub.status.idle":"2025-01-18T11:08:35.336625Z","shell.execute_reply.started":"2025-01-18T11:08:34.397443Z","shell.execute_reply":"2025-01-18T11:08:35.335518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols = df.select_dtypes(np.number).columns.to_list()\ncat_cols=df.select_dtypes('object').columns.to_list()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:08:37.331561Z","iopub.execute_input":"2025-01-18T11:08:37.33212Z","iopub.status.idle":"2025-01-18T11:08:37.668924Z","shell.execute_reply.started":"2025-01-18T11:08:37.332079Z","shell.execute_reply":"2025-01-18T11:08:37.667693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(num_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:08:39.79191Z","iopub.execute_input":"2025-01-18T11:08:39.792318Z","iopub.status.idle":"2025-01-18T11:08:39.798359Z","shell.execute_reply.started":"2025-01-18T11:08:39.792281Z","shell.execute_reply":"2025-01-18T11:08:39.797176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:08:41.901799Z","iopub.execute_input":"2025-01-18T11:08:41.902201Z","iopub.status.idle":"2025-01-18T11:08:41.908804Z","shell.execute_reply.started":"2025-01-18T11:08:41.902172Z","shell.execute_reply":"2025-01-18T11:08:41.907646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(y=df['Age'],x=df['Gender'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:08:45.151534Z","iopub.execute_input":"2025-01-18T11:08:45.151921Z","iopub.status.idle":"2025-01-18T11:08:46.227711Z","shell.execute_reply.started":"2025-01-18T11:08:45.151893Z","shell.execute_reply":"2025-01-18T11:08:46.226583Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Seems like the Age has a uniform distribution","metadata":{}},{"cell_type":"code","source":"sns.boxplot(y=df['Annual Income'],x=df['Gender'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:40.710315Z","iopub.execute_input":"2025-01-18T11:10:40.710738Z","iopub.status.idle":"2025-01-18T11:10:42.197728Z","shell.execute_reply.started":"2025-01-18T11:10:40.710706Z","shell.execute_reply":"2025-01-18T11:10:42.196294Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"the median of Annual income is around 25K, however it has many outliers. ","metadata":{}},{"cell_type":"code","source":"df[num_cols[3]].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:45.572185Z","iopub.execute_input":"2025-01-18T11:10:45.572555Z","iopub.status.idle":"2025-01-18T11:10:45.616908Z","shell.execute_reply.started":"2025-01-18T11:10:45.572521Z","shell.execute_reply":"2025-01-18T11:10:45.615469Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The 'Number of Dependents' column had some null values, there can be two reasons for this either there is no dependents for that particular customer or the information is not available. ","metadata":{}},{"cell_type":"code","source":"sns.histplot(x=df[num_cols[4]])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:48.024503Z","iopub.execute_input":"2025-01-18T11:10:48.024916Z","iopub.status.idle":"2025-01-18T11:10:49.288727Z","shell.execute_reply.started":"2025-01-18T11:10:48.024882Z","shell.execute_reply":"2025-01-18T11:10:49.287415Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Health Score has a normal Distribution with a very little skewness, most of the data lies within the range of 20-30. \nThere is no such information about the interpretation of Health Score is available. For now, lets consider a low health score is the lack or requirement of improvement in the health and high health score is something about very heallty individuals. ","metadata":{}},{"cell_type":"code","source":"sns.boxplot(y=df[num_cols[4]],x=df['Gender'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:49.290186Z","iopub.execute_input":"2025-01-18T11:10:49.290519Z","iopub.status.idle":"2025-01-18T11:10:50.304176Z","shell.execute_reply.started":"2025-01-18T11:10:49.290488Z","shell.execute_reply":"2025-01-18T11:10:50.303191Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Even after distributing it across the Gender, there seems no difference in the distribution of Health Score from the overall data. ","metadata":{}},{"cell_type":"code","source":"df[num_cols[5]].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:50.487469Z","iopub.execute_input":"2025-01-18T11:10:50.48787Z","iopub.status.idle":"2025-01-18T11:10:50.528782Z","shell.execute_reply.started":"2025-01-18T11:10:50.487837Z","shell.execute_reply":"2025-01-18T11:10:50.527684Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Very few customers have more than 4 claims. ","metadata":{}},{"cell_type":"code","source":"sns.histplot(y=df[num_cols[6]])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:53.17559Z","iopub.execute_input":"2025-01-18T11:10:53.176008Z","iopub.status.idle":"2025-01-18T11:10:54.374663Z","shell.execute_reply.started":"2025-01-18T11:10:53.175973Z","shell.execute_reply":"2025-01-18T11:10:54.373442Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Vehicle Age is also uniformally distributed.","metadata":{}},{"cell_type":"code","source":"sns.histplot(x=df[num_cols[7]])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:56.531967Z","iopub.execute_input":"2025-01-18T11:10:56.532338Z","iopub.status.idle":"2025-01-18T11:10:57.673069Z","shell.execute_reply.started":"2025-01-18T11:10:56.532311Z","shell.execute_reply":"2025-01-18T11:10:57.671929Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Credit Score is somewhat uniformally distributed, but a few less customers have less than 500 of Credit Score. \nThis could be because of the reason that Credit Score plays a major role and Companies dont really prefer the Customers with a lesser Credit Score. \n\nhttps://www.mahindrafinance.com/blogs/credit-score/credit-score-impact-on-insurance-premiums","metadata":{}},{"cell_type":"code","source":"df[num_cols[8]].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:10:59.899285Z","iopub.execute_input":"2025-01-18T11:10:59.899633Z","iopub.status.idle":"2025-01-18T11:10:59.933489Z","shell.execute_reply.started":"2025-01-18T11:10:59.899605Z","shell.execute_reply":"2025-01-18T11:10:59.93234Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The Insurance Duration also has a uniform distribution. ","metadata":{}},{"cell_type":"code","source":"sns.histplot(x=df['Premium Amount'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:02.655417Z","iopub.execute_input":"2025-01-18T11:11:02.655827Z","iopub.status.idle":"2025-01-18T11:11:03.966297Z","shell.execute_reply.started":"2025-01-18T11:11:02.655785Z","shell.execute_reply":"2025-01-18T11:11:03.964969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:03.968007Z","iopub.execute_input":"2025-01-18T11:11:03.968467Z","iopub.status.idle":"2025-01-18T11:11:03.974849Z","shell.execute_reply.started":"2025-01-18T11:11:03.968427Z","shell.execute_reply":"2025-01-18T11:11:03.973716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(y=df['Premium Amount'], x=df['Education Level'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:04.517369Z","iopub.execute_input":"2025-01-18T11:11:04.517732Z","iopub.status.idle":"2025-01-18T11:11:05.771524Z","shell.execute_reply.started":"2025-01-18T11:11:04.517701Z","shell.execute_reply":"2025-01-18T11:11:05.770102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(y=df['Premium Amount'], x=df['Marital Status'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:06.748613Z","iopub.execute_input":"2025-01-18T11:11:06.749137Z","iopub.status.idle":"2025-01-18T11:11:08.065078Z","shell.execute_reply.started":"2025-01-18T11:11:06.749094Z","shell.execute_reply":"2025-01-18T11:11:08.063921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(y=df['Premium Amount'], x=df['Location'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:09.127764Z","iopub.execute_input":"2025-01-18T11:11:09.128143Z","iopub.status.idle":"2025-01-18T11:11:10.36337Z","shell.execute_reply.started":"2025-01-18T11:11:09.128115Z","shell.execute_reply":"2025-01-18T11:11:10.362096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=df['Policy Type'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:10.886341Z","iopub.execute_input":"2025-01-18T11:11:10.886719Z","iopub.status.idle":"2025-01-18T11:11:12.130858Z","shell.execute_reply.started":"2025-01-18T11:11:10.886679Z","shell.execute_reply":"2025-01-18T11:11:12.129643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Policy Start Date']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:12.645801Z","iopub.execute_input":"2025-01-18T11:11:12.646177Z","iopub.status.idle":"2025-01-18T11:11:12.654329Z","shell.execute_reply.started":"2025-01-18T11:11:12.646148Z","shell.execute_reply":"2025-01-18T11:11:12.652885Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"this is timestamp data, hence required to convert in data time format","metadata":{}},{"cell_type":"code","source":"df['Policy Start Date'] = pd.to_datetime(df['Policy Start Date'])\ntrain_df['Policy Start Date']=pd.to_datetime(train_df['Policy Start Date'])\ntest_df['Policy Start Date']=pd.to_datetime(test_df['Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:14.633156Z","iopub.execute_input":"2025-01-18T11:11:14.633486Z","iopub.status.idle":"2025-01-18T11:11:16.080377Z","shell.execute_reply.started":"2025-01-18T11:11:14.633462Z","shell.execute_reply":"2025-01-18T11:11:16.079295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max(df['Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:16.081905Z","iopub.execute_input":"2025-01-18T11:11:16.082236Z","iopub.status.idle":"2025-01-18T11:11:20.211029Z","shell.execute_reply.started":"2025-01-18T11:11:16.082209Z","shell.execute_reply":"2025-01-18T11:11:20.209439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"min(df['Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:20.213379Z","iopub.execute_input":"2025-01-18T11:11:20.213832Z","iopub.status.idle":"2025-01-18T11:11:24.278449Z","shell.execute_reply.started":"2025-01-18T11:11:20.213786Z","shell.execute_reply":"2025-01-18T11:11:24.277372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Year']=df['Policy Start Date'].dt.year\ndf['Month']=df['Policy Start Date'].dt.month \ndf['Day']=df['Policy Start Date'].dt.day\ntrain_df['Year']=train_df['Policy Start Date'].dt.year\ntrain_df['Month']=train_df['Policy Start Date'].dt.month \ntrain_df['Day']=train_df['Policy Start Date'].dt.day\ntest_df['Year']=test_df['Policy Start Date'].dt.year\ntest_df['Month']=test_df['Policy Start Date'].dt.month \ntest_df['Day']=df['Policy Start Date'].dt.day","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:24.279671Z","iopub.execute_input":"2025-01-18T11:11:24.280072Z","iopub.status.idle":"2025-01-18T11:11:24.928137Z","shell.execute_reply.started":"2025-01-18T11:11:24.280013Z","shell.execute_reply":"2025-01-18T11:11:24.92688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Year'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:24.929334Z","iopub.execute_input":"2025-01-18T11:11:24.929723Z","iopub.status.idle":"2025-01-18T11:11:24.949289Z","shell.execute_reply.started":"2025-01-18T11:11:24.929682Z","shell.execute_reply":"2025-01-18T11:11:24.948224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Month'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:24.950759Z","iopub.execute_input":"2025-01-18T11:11:24.951162Z","iopub.status.idle":"2025-01-18T11:11:24.982472Z","shell.execute_reply.started":"2025-01-18T11:11:24.951133Z","shell.execute_reply":"2025-01-18T11:11:24.981412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Occupation'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:24.983693Z","iopub.execute_input":"2025-01-18T11:11:24.984152Z","iopub.status.idle":"2025-01-18T11:11:25.126468Z","shell.execute_reply.started":"2025-01-18T11:11:24.984108Z","shell.execute_reply":"2025-01-18T11:11:25.125144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=df['Customer Feedback'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:25.129233Z","iopub.execute_input":"2025-01-18T11:11:25.129622Z","iopub.status.idle":"2025-01-18T11:11:26.406422Z","shell.execute_reply.started":"2025-01-18T11:11:25.129592Z","shell.execute_reply":"2025-01-18T11:11:26.40521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=df['Exercise Frequency'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:26.407646Z","iopub.execute_input":"2025-01-18T11:11:26.407944Z","iopub.status.idle":"2025-01-18T11:11:27.648394Z","shell.execute_reply.started":"2025-01-18T11:11:26.407918Z","shell.execute_reply":"2025-01-18T11:11:27.647111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=df['Smoking Status'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:27.649532Z","iopub.execute_input":"2025-01-18T11:11:27.649852Z","iopub.status.idle":"2025-01-18T11:11:28.846329Z","shell.execute_reply.started":"2025-01-18T11:11:27.649824Z","shell.execute_reply":"2025-01-18T11:11:28.844893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=df['Property Type'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:28.847673Z","iopub.execute_input":"2025-01-18T11:11:28.848119Z","iopub.status.idle":"2025-01-18T11:11:30.095606Z","shell.execute_reply.started":"2025-01-18T11:11:28.848075Z","shell.execute_reply":"2025-01-18T11:11:30.094502Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Seems like most of the data is uniformly distributed","metadata":{}},{"cell_type":"markdown","source":"sns.heatmap(train_df[num_cols].corr())","metadata":{}},{"cell_type":"code","source":"sns.heatmap(df[num_cols].corr())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:30.096636Z","iopub.execute_input":"2025-01-18T11:11:30.096909Z","iopub.status.idle":"2025-01-18T11:11:31.219359Z","shell.execute_reply.started":"2025-01-18T11:11:30.096887Z","shell.execute_reply":"2025-01-18T11:11:31.217835Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"feels like none of the column has a linear correlation with the Premium amount. ","metadata":{}},{"cell_type":"markdown","source":"# **Imputation**","metadata":{"execution":{"iopub.status.busy":"2025-01-11T13:16:46.240814Z","iopub.execute_input":"2025-01-11T13:16:46.241216Z","iopub.status.idle":"2025-01-11T13:16:46.255235Z","shell.execute_reply.started":"2025-01-11T13:16:46.241183Z","shell.execute_reply":"2025-01-11T13:16:46.253701Z"}}},{"cell_type":"markdown","source":"Imputing Numerical Columns","metadata":{}},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:31.221678Z","iopub.execute_input":"2025-01-18T11:11:31.222189Z","iopub.status.idle":"2025-01-18T11:11:31.71193Z","shell.execute_reply.started":"2025-01-18T11:11:31.222151Z","shell.execute_reply":"2025-01-18T11:11:31.710622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean_imputer = SimpleImputer(strategy='mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:31.713187Z","iopub.execute_input":"2025-01-18T11:11:31.713481Z","iopub.status.idle":"2025-01-18T11:11:31.71835Z","shell.execute_reply.started":"2025-01-18T11:11:31.713456Z","shell.execute_reply":"2025-01-18T11:11:31.717063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[num_cols].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:31.719897Z","iopub.execute_input":"2025-01-18T11:11:31.720387Z","iopub.status.idle":"2025-01-18T11:11:31.796204Z","shell.execute_reply.started":"2025-01-18T11:11:31.72034Z","shell.execute_reply":"2025-01-18T11:11:31.795062Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"During EDA, it was found that only Annual Income has the non-uniform distribution and has outliers rest All columns were Uniform. \nSo, 'Annual Income' can be imputed with the Median values of the column and rest all columns can be imputed with Mean values. ","metadata":{}},{"cell_type":"code","source":"test_df[num_cols[:-1]].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:31.797328Z","iopub.execute_input":"2025-01-18T11:11:31.797644Z","iopub.status.idle":"2025-01-18T11:11:31.831237Z","shell.execute_reply.started":"2025-01-18T11:11:31.797615Z","shell.execute_reply":"2025-01-18T11:11:31.830006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean_imputer.fit(train_df[['Age', 'Number of Dependents', 'Health Score' ,'Previous Claims','Vehicle Age' ,'Credit Score' ,'Insurance Duration']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:31.902761Z","iopub.execute_input":"2025-01-18T11:11:31.903193Z","iopub.status.idle":"2025-01-18T11:11:32.036373Z","shell.execute_reply.started":"2025-01-18T11:11:31.903157Z","shell.execute_reply":"2025-01-18T11:11:32.034956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[['Age', 'Number of Dependents', 'Health Score' ,'Previous Claims','Vehicle Age' ,'Credit Score' ,'Insurance Duration']]=mean_imputer.transform(train_df[['Age', 'Number of Dependents', 'Health Score' ,'Previous Claims','Vehicle Age' ,'Credit Score' ,'Insurance Duration']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:32.246782Z","iopub.execute_input":"2025-01-18T11:11:32.247188Z","iopub.status.idle":"2025-01-18T11:11:32.386254Z","shell.execute_reply.started":"2025-01-18T11:11:32.247156Z","shell.execute_reply":"2025-01-18T11:11:32.385117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean_imputer.fit(test_df[['Age', 'Number of Dependents', 'Health Score' ,'Previous Claims','Vehicle Age' ,'Credit Score' ,'Insurance Duration']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:35.287221Z","iopub.execute_input":"2025-01-18T11:11:35.287592Z","iopub.status.idle":"2025-01-18T11:11:35.379218Z","shell.execute_reply.started":"2025-01-18T11:11:35.287561Z","shell.execute_reply":"2025-01-18T11:11:35.377949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[['Age', 'Number of Dependents', 'Health Score' ,'Previous Claims','Vehicle Age' ,'Credit Score' ,'Insurance Duration']]=mean_imputer.transform(test_df[['Age', 'Number of Dependents', 'Health Score' ,'Previous Claims','Vehicle Age' ,'Credit Score' ,'Insurance Duration']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:35.615721Z","iopub.execute_input":"2025-01-18T11:11:35.61619Z","iopub.status.idle":"2025-01-18T11:11:35.710667Z","shell.execute_reply.started":"2025-01-18T11:11:35.616155Z","shell.execute_reply":"2025-01-18T11:11:35.709363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_imputer= SimpleImputer(strategy='median')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:36.915814Z","iopub.execute_input":"2025-01-18T11:11:36.916253Z","iopub.status.idle":"2025-01-18T11:11:36.921081Z","shell.execute_reply.started":"2025-01-18T11:11:36.916215Z","shell.execute_reply":"2025-01-18T11:11:36.919733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_imputer.fit(pd.DataFrame(train_df['Annual Income']))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:37.287937Z","iopub.execute_input":"2025-01-18T11:11:37.28834Z","iopub.status.idle":"2025-01-18T11:11:37.516885Z","shell.execute_reply.started":"2025-01-18T11:11:37.28831Z","shell.execute_reply":"2025-01-18T11:11:37.515706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['Annual Income'] = median_imputer.transform(pd.DataFrame(train_df['Annual Income']))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:39.354199Z","iopub.execute_input":"2025-01-18T11:11:39.354572Z","iopub.status.idle":"2025-01-18T11:11:39.377556Z","shell.execute_reply.started":"2025-01-18T11:11:39.354542Z","shell.execute_reply":"2025-01-18T11:11:39.37618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_imputer.fit(pd.DataFrame(test_df['Annual Income']))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:39.789732Z","iopub.execute_input":"2025-01-18T11:11:39.79019Z","iopub.status.idle":"2025-01-18T11:11:39.957889Z","shell.execute_reply.started":"2025-01-18T11:11:39.790157Z","shell.execute_reply":"2025-01-18T11:11:39.95661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['Annual Income'] = median_imputer.transform(pd.DataFrame(test_df['Annual Income']))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:41.470137Z","iopub.execute_input":"2025-01-18T11:11:41.470483Z","iopub.status.idle":"2025-01-18T11:11:41.487869Z","shell.execute_reply.started":"2025-01-18T11:11:41.470456Z","shell.execute_reply":"2025-01-18T11:11:41.486751Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Imputing Categorical Columns","metadata":{}},{"cell_type":"code","source":"train_df[cat_cols].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:43.483827Z","iopub.execute_input":"2025-01-18T11:11:43.484224Z","iopub.status.idle":"2025-01-18T11:11:44.178241Z","shell.execute_reply.started":"2025-01-18T11:11:43.48419Z","shell.execute_reply":"2025-01-18T11:11:44.176986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[cat_cols].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:45.519599Z","iopub.execute_input":"2025-01-18T11:11:45.51996Z","iopub.status.idle":"2025-01-18T11:11:45.970077Z","shell.execute_reply.started":"2025-01-18T11:11:45.519931Z","shell.execute_reply":"2025-01-18T11:11:45.969023Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"We have 3 categorical columns having missing values: \n1. Marital Status\n2. Occupation\n3. Customer Feedback","metadata":{}},{"cell_type":"code","source":"train_df['Customer Feedback'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:48.731908Z","iopub.execute_input":"2025-01-18T11:11:48.732279Z","iopub.status.idle":"2025-01-18T11:11:48.823508Z","shell.execute_reply.started":"2025-01-18T11:11:48.732252Z","shell.execute_reply":"2025-01-18T11:11:48.822201Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Though, there is very less difference in the counts of Feedback, we can still impute the values with 'Average'","metadata":{}},{"cell_type":"code","source":"train_df['Customer Feedback']= pd.DataFrame(train_df['Customer Feedback']).fillna('Average')\ntest_df['Customer Feedback']= pd.DataFrame(test_df['Customer Feedback']).fillna('Average')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:53.925275Z","iopub.execute_input":"2025-01-18T11:11:53.925629Z","iopub.status.idle":"2025-01-18T11:11:54.077218Z","shell.execute_reply.started":"2025-01-18T11:11:53.925602Z","shell.execute_reply":"2025-01-18T11:11:54.076102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['Marital Status']= pd.DataFrame(train_df['Marital Status']).fillna(train_df['Marital Status'].mode())\ntest_df['Marital Status']= pd.DataFrame(test_df['Marital Status']).fillna(test_df['Marital Status'].mode())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:55.598665Z","iopub.execute_input":"2025-01-18T11:11:55.599071Z","iopub.status.idle":"2025-01-18T11:11:55.783849Z","shell.execute_reply.started":"2025-01-18T11:11:55.599019Z","shell.execute_reply":"2025-01-18T11:11:55.782422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['Occupation']= pd.DataFrame(train_df['Occupation']).fillna(train_df['Occupation'].mode())\ntest_df['Occupation']= pd.DataFrame(test_df['Occupation']).fillna(test_df['Occupation'].mode())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:11:55.903842Z","iopub.execute_input":"2025-01-18T11:11:55.904288Z","iopub.status.idle":"2025-01-18T11:11:56.071119Z","shell.execute_reply.started":"2025-01-18T11:11:55.904253Z","shell.execute_reply":"2025-01-18T11:11:56.07001Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Scale Numeric Values ","metadata":{}},{"cell_type":"code","source":"train_df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:22:13.353077Z","iopub.execute_input":"2025-01-18T11:22:13.353569Z","iopub.status.idle":"2025-01-18T11:22:13.362149Z","shell.execute_reply.started":"2025-01-18T11:22:13.353514Z","shell.execute_reply":"2025-01-18T11:22:13.360529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.select_dtypes(include = ['int64','float64']).columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:27:34.362906Z","iopub.execute_input":"2025-01-18T11:27:34.36336Z","iopub.status.idle":"2025-01-18T11:27:34.47546Z","shell.execute_reply.started":"2025-01-18T11:27:34.363327Z","shell.execute_reply":"2025-01-18T11:27:34.474099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train_df['Year']  = train_df['Year'].astype('int')\ntrain_df['Month']  = train_df['Month'].astype('int')\ntrain_df['Day']  = train_df['Day'].astype('int')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:28:38.318789Z","iopub.execute_input":"2025-01-18T11:28:38.319196Z","iopub.status.idle":"2025-01-18T11:28:38.33138Z","shell.execute_reply.started":"2025-01-18T11:28:38.319163Z","shell.execute_reply":"2025-01-18T11:28:38.330427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols = train_df.select_dtypes(include = ['int64','float64']).columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:28:52.39626Z","iopub.execute_input":"2025-01-18T11:28:52.396664Z","iopub.status.idle":"2025-01-18T11:28:52.544278Z","shell.execute_reply.started":"2025-01-18T11:28:52.396632Z","shell.execute_reply":"2025-01-18T11:28:52.543017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols.remove('Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:30:38.083817Z","iopub.execute_input":"2025-01-18T11:30:38.08432Z","iopub.status.idle":"2025-01-18T11:30:38.089192Z","shell.execute_reply.started":"2025-01-18T11:30:38.084287Z","shell.execute_reply":"2025-01-18T11:30:38.087769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols.remove('id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:21.532997Z","iopub.execute_input":"2025-01-18T13:08:21.533519Z","iopub.status.idle":"2025-01-18T13:08:21.539006Z","shell.execute_reply.started":"2025-01-18T13:08:21.53348Z","shell.execute_reply":"2025-01-18T13:08:21.537543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:24.308206Z","iopub.execute_input":"2025-01-18T13:08:24.308543Z","iopub.status.idle":"2025-01-18T13:08:24.315119Z","shell.execute_reply.started":"2025-01-18T13:08:24.308516Z","shell.execute_reply":"2025-01-18T13:08:24.313796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols.remove('Policy Start Date')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:31:00.929431Z","iopub.execute_input":"2025-01-18T11:31:00.929779Z","iopub.status.idle":"2025-01-18T11:31:00.934437Z","shell.execute_reply.started":"2025-01-18T11:31:00.929752Z","shell.execute_reply":"2025-01-18T11:31:00.933184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:31:05.509412Z","iopub.execute_input":"2025-01-18T11:31:05.509834Z","iopub.status.idle":"2025-01-18T11:31:05.516629Z","shell.execute_reply.started":"2025-01-18T11:31:05.509802Z","shell.execute_reply":"2025-01-18T11:31:05.5152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:14:15.128533Z","iopub.execute_input":"2025-01-18T11:14:15.129156Z","iopub.status.idle":"2025-01-18T11:14:15.13584Z","shell.execute_reply.started":"2025-01-18T11:14:15.129104Z","shell.execute_reply":"2025-01-18T11:14:15.134663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Create the scaler\nScaler = MinMaxScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:31:14.242915Z","iopub.execute_input":"2025-01-18T11:31:14.243309Z","iopub.status.idle":"2025-01-18T11:31:14.248033Z","shell.execute_reply.started":"2025-01-18T11:31:14.243281Z","shell.execute_reply":"2025-01-18T11:31:14.246689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Scaler.fit(train_df[num_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:29.788894Z","iopub.execute_input":"2025-01-18T13:08:29.789284Z","iopub.status.idle":"2025-01-18T13:08:30.038067Z","shell.execute_reply.started":"2025-01-18T13:08:29.789256Z","shell.execute_reply":"2025-01-18T13:08:30.036783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[num_cols] = Scaler.transform(train_df[num_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:32.985109Z","iopub.execute_input":"2025-01-18T13:08:32.985456Z","iopub.status.idle":"2025-01-18T13:08:33.130891Z","shell.execute_reply.started":"2025-01-18T13:08:32.985431Z","shell.execute_reply":"2025-01-18T13:08:33.129785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[num_cols].describe().loc[['min','max']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:35.183917Z","iopub.execute_input":"2025-01-18T13:08:35.184349Z","iopub.status.idle":"2025-01-18T13:08:35.879493Z","shell.execute_reply.started":"2025-01-18T13:08:35.184313Z","shell.execute_reply":"2025-01-18T13:08:35.87818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[num_cols] = Scaler.transform(test_df[num_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:37.307674Z","iopub.execute_input":"2025-01-18T13:08:37.30807Z","iopub.status.idle":"2025-01-18T13:08:37.509524Z","shell.execute_reply.started":"2025-01-18T13:08:37.308021Z","shell.execute_reply":"2025-01-18T13:08:37.508294Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Encoding Categorical Values ","metadata":{}},{"cell_type":"code","source":"train_df[cat_cols].nunique().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:32:04.601991Z","iopub.execute_input":"2025-01-18T11:32:04.602391Z","iopub.status.idle":"2025-01-18T11:32:05.330339Z","shell.execute_reply.started":"2025-01-18T11:32:04.602363Z","shell.execute_reply":"2025-01-18T11:32:05.329116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:33:12.784818Z","iopub.execute_input":"2025-01-18T11:33:12.785215Z","iopub.status.idle":"2025-01-18T11:33:12.79011Z","shell.execute_reply.started":"2025-01-18T11:33:12.785184Z","shell.execute_reply":"2025-01-18T11:33:12.788527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoder = OneHotEncoder(sparse_output=False, handle_unknown='ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:33:45.611246Z","iopub.execute_input":"2025-01-18T11:33:45.611633Z","iopub.status.idle":"2025-01-18T11:33:45.616287Z","shell.execute_reply.started":"2025-01-18T11:33:45.6116Z","shell.execute_reply":"2025-01-18T11:33:45.615098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoder.fit(train_df[cat_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:34:18.306978Z","iopub.execute_input":"2025-01-18T11:34:18.307389Z","iopub.status.idle":"2025-01-18T11:34:18.849349Z","shell.execute_reply.started":"2025-01-18T11:34:18.307356Z","shell.execute_reply":"2025-01-18T11:34:18.848162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoded_cols = list(encoder.get_feature_names_out(cat_cols))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:34:43.990718Z","iopub.execute_input":"2025-01-18T11:34:43.991128Z","iopub.status.idle":"2025-01-18T11:34:43.99663Z","shell.execute_reply.started":"2025-01-18T11:34:43.991094Z","shell.execute_reply":"2025-01-18T11:34:43.995162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[encoded_cols]=encoder.transform(train_df[cat_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:35:17.297826Z","iopub.execute_input":"2025-01-18T11:35:17.298241Z","iopub.status.idle":"2025-01-18T11:35:21.352022Z","shell.execute_reply.started":"2025-01-18T11:35:17.298208Z","shell.execute_reply":"2025-01-18T11:35:21.350894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[encoded_cols]=encoder.transform(test_df[cat_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:36:05.203708Z","iopub.execute_input":"2025-01-18T11:36:05.204143Z","iopub.status.idle":"2025-01-18T11:36:07.901538Z","shell.execute_reply.started":"2025-01-18T11:36:05.204108Z","shell.execute_reply":"2025-01-18T11:36:07.900272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:35:35.191705Z","iopub.execute_input":"2025-01-18T11:35:35.192142Z","iopub.status.idle":"2025-01-18T11:35:36.496864Z","shell.execute_reply.started":"2025-01-18T11:35:35.192106Z","shell.execute_reply":"2025-01-18T11:35:36.494928Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Training & Validation","metadata":{}},{"cell_type":"markdown","source":"#### Linear Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T11:36:44.760122Z","iopub.execute_input":"2025-01-18T11:36:44.760713Z","iopub.status.idle":"2025-01-18T11:36:44.765647Z","shell.execute_reply.started":"2025-01-18T11:36:44.760658Z","shell.execute_reply":"2025-01-18T11:36:44.764366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, val_train, X_targets, val_targets = train_test_split(train_df[num_cols + encoded_cols],\n                                                                        train_df['Premium Amount'],\n                                                                        test_size=0.2,\n                                                                        random_state=28)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:46.361276Z","iopub.execute_input":"2025-01-18T13:08:46.361643Z","iopub.status.idle":"2025-01-18T13:08:48.074154Z","shell.execute_reply.started":"2025-01-18T13:08:46.361615Z","shell.execute_reply":"2025-01-18T13:08:48.072884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_targets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:52.738638Z","iopub.execute_input":"2025-01-18T13:08:52.739032Z","iopub.status.idle":"2025-01-18T13:08:52.748088Z","shell.execute_reply.started":"2025-01-18T13:08:52.738998Z","shell.execute_reply":"2025-01-18T13:08:52.746764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:57.4211Z","iopub.execute_input":"2025-01-18T13:08:57.421563Z","iopub.status.idle":"2025-01-18T13:08:57.427442Z","shell.execute_reply.started":"2025-01-18T13:08:57.421522Z","shell.execute_reply":"2025-01-18T13:08:57.426202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_model = LinearRegression ()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:08:59.477808Z","iopub.execute_input":"2025-01-18T13:08:59.478217Z","iopub.status.idle":"2025-01-18T13:08:59.482995Z","shell.execute_reply.started":"2025-01-18T13:08:59.478184Z","shell.execute_reply":"2025-01-18T13:08:59.481522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_model.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:02.023344Z","iopub.execute_input":"2025-01-18T13:09:02.023742Z","iopub.status.idle":"2025-01-18T13:09:05.184246Z","shell.execute_reply.started":"2025-01-18T13:09:02.023709Z","shell.execute_reply":"2025-01-18T13:09:05.183097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_predicts = LR_model.predict(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:06.420461Z","iopub.execute_input":"2025-01-18T13:09:06.420875Z","iopub.status.idle":"2025-01-18T13:09:06.549695Z","shell.execute_reply.started":"2025-01-18T13:09:06.420841Z","shell.execute_reply":"2025-01-18T13:09:06.548132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_predicts = LR_model.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:08.118513Z","iopub.execute_input":"2025-01-18T13:09:08.118912Z","iopub.status.idle":"2025-01-18T13:09:08.159536Z","shell.execute_reply.started":"2025-01-18T13:09:08.118882Z","shell.execute_reply":"2025-01-18T13:09:08.157831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:10.076628Z","iopub.execute_input":"2025-01-18T13:09:10.077034Z","iopub.status.idle":"2025-01-18T13:09:10.082309Z","shell.execute_reply.started":"2025-01-18T13:09:10.077004Z","shell.execute_reply":"2025-01-18T13:09:10.080562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_rmse = np.sqrt(mean_squared_error(X_targets, X_predicts))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:11.590341Z","iopub.execute_input":"2025-01-18T13:09:11.590772Z","iopub.status.idle":"2025-01-18T13:09:11.601397Z","shell.execute_reply.started":"2025-01-18T13:09:11.590737Z","shell.execute_reply":"2025-01-18T13:09:11.600182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_rmse = np.sqrt(mean_squared_error(val_targets, val_predicts))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:13.156475Z","iopub.execute_input":"2025-01-18T13:09:13.156884Z","iopub.status.idle":"2025-01-18T13:09:13.164135Z","shell.execute_reply.started":"2025-01-18T13:09:13.156856Z","shell.execute_reply":"2025-01-18T13:09:13.162998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_rmse)\nprint(val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:18.058239Z","iopub.execute_input":"2025-01-18T13:09:18.058594Z","iopub.status.idle":"2025-01-18T13:09:18.064712Z","shell.execute_reply.started":"2025-01-18T13:09:18.058567Z","shell.execute_reply":"2025-01-18T13:09:18.06344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_weight = LR_model.coef_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:21.503275Z","iopub.execute_input":"2025-01-18T13:09:21.503671Z","iopub.status.idle":"2025-01-18T13:09:21.508533Z","shell.execute_reply.started":"2025-01-18T13:09:21.503639Z","shell.execute_reply":"2025-01-18T13:09:21.507149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_model_Weights = pd.DataFrame({'Columns': X_train.columns, 'Weights': LR_weight})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:23.911136Z","iopub.execute_input":"2025-01-18T13:09:23.911518Z","iopub.status.idle":"2025-01-18T13:09:23.917412Z","shell.execute_reply.started":"2025-01-18T13:09:23.911483Z","shell.execute_reply":"2025-01-18T13:09:23.915895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_model_Weights","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:26.081857Z","iopub.execute_input":"2025-01-18T13:09:26.082265Z","iopub.status.idle":"2025-01-18T13:09:26.095132Z","shell.execute_reply.started":"2025-01-18T13:09:26.082232Z","shell.execute_reply":"2025-01-18T13:09:26.093787Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:31.333485Z","iopub.execute_input":"2025-01-18T13:09:31.333833Z","iopub.status.idle":"2025-01-18T13:09:31.338294Z","shell.execute_reply.started":"2025-01-18T13:09:31.333806Z","shell.execute_reply":"2025-01-18T13:09:31.336971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_model = DecisionTreeRegressor(random_state = 28)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:33.758634Z","iopub.execute_input":"2025-01-18T13:09:33.758994Z","iopub.status.idle":"2025-01-18T13:09:33.763772Z","shell.execute_reply.started":"2025-01-18T13:09:33.758965Z","shell.execute_reply":"2025-01-18T13:09:33.762346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_model.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:09:37.027636Z","iopub.execute_input":"2025-01-18T13:09:37.028072Z","iopub.status.idle":"2025-01-18T13:10:12.219937Z","shell.execute_reply.started":"2025-01-18T13:09:37.028015Z","shell.execute_reply":"2025-01-18T13:10:12.218732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_X_predicts = DT_model.predict(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:10:49.713625Z","iopub.execute_input":"2025-01-18T13:10:49.714122Z","iopub.status.idle":"2025-01-18T13:10:50.930337Z","shell.execute_reply.started":"2025-01-18T13:10:49.714068Z","shell.execute_reply":"2025-01-18T13:10:50.929195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_val_predicts = DT_model.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:10:52.05668Z","iopub.execute_input":"2025-01-18T13:10:52.057084Z","iopub.status.idle":"2025-01-18T13:10:52.349283Z","shell.execute_reply.started":"2025-01-18T13:10:52.057037Z","shell.execute_reply":"2025-01-18T13:10:52.348129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_X_rmse = np.sqrt(mean_squared_error(X_targets, DT_X_predicts))\nDT_val_rmse = np.sqrt(mean_squared_error(val_targets, DT_val_predicts))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:10:56.039605Z","iopub.execute_input":"2025-01-18T13:10:56.040008Z","iopub.status.idle":"2025-01-18T13:10:56.052868Z","shell.execute_reply.started":"2025-01-18T13:10:56.039975Z","shell.execute_reply":"2025-01-18T13:10:56.051574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(DT_X_rmse)\nprint(DT_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:10:56.893245Z","iopub.execute_input":"2025-01-18T13:10:56.893604Z","iopub.status.idle":"2025-01-18T13:10:56.899586Z","shell.execute_reply.started":"2025-01-18T13:10:56.893576Z","shell.execute_reply":"2025-01-18T13:10:56.898187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_model.tree_.max_depth","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:10:59.475689Z","iopub.execute_input":"2025-01-18T13:10:59.476212Z","iopub.status.idle":"2025-01-18T13:10:59.483989Z","shell.execute_reply.started":"2025-01-18T13:10:59.476163Z","shell.execute_reply":"2025-01-18T13:10:59.482747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.tree import plot_tree","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:02.536798Z","iopub.execute_input":"2025-01-18T13:11:02.5372Z","iopub.status.idle":"2025-01-18T13:11:02.541811Z","shell.execute_reply.started":"2025-01-18T13:11:02.537166Z","shell.execute_reply":"2025-01-18T13:11:02.540649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (80,20))\nplot_tree(DT_model, feature_names = X_train.columns, max_depth=2, filled= True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:05.044683Z","iopub.execute_input":"2025-01-18T13:11:05.045073Z","iopub.status.idle":"2025-01-18T13:11:06.53121Z","shell.execute_reply.started":"2025-01-18T13:11:05.045024Z","shell.execute_reply":"2025-01-18T13:11:06.52987Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Reducing max_depth from 68 to 10 ","metadata":{}},{"cell_type":"code","source":"DT_model = DecisionTreeRegressor(random_state = 28, max_depth = 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:09.315239Z","iopub.execute_input":"2025-01-18T13:11:09.315658Z","iopub.status.idle":"2025-01-18T13:11:09.320542Z","shell.execute_reply.started":"2025-01-18T13:11:09.315624Z","shell.execute_reply":"2025-01-18T13:11:09.319139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_model.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:23.777341Z","iopub.execute_input":"2025-01-18T13:11:23.777755Z","iopub.status.idle":"2025-01-18T13:11:36.660979Z","shell.execute_reply.started":"2025-01-18T13:11:23.777715Z","shell.execute_reply":"2025-01-18T13:11:36.65965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_X_predicts = DT_model.predict(X_train)\nDT_val_predicts = DT_model.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:36.662678Z","iopub.execute_input":"2025-01-18T13:11:36.663107Z","iopub.status.idle":"2025-01-18T13:11:36.952082Z","shell.execute_reply.started":"2025-01-18T13:11:36.663069Z","shell.execute_reply":"2025-01-18T13:11:36.950827Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_X_rmse = np.sqrt(mean_squared_error(X_targets, DT_X_predicts))\nDT_val_rmse = np.sqrt(mean_squared_error(val_targets, DT_val_predicts))\nprint(DT_X_rmse, DT_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:39.601393Z","iopub.execute_input":"2025-01-18T13:11:39.601752Z","iopub.status.idle":"2025-01-18T13:11:39.616657Z","shell.execute_reply.started":"2025-01-18T13:11:39.601725Z","shell.execute_reply":"2025-01-18T13:11:39.615076Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"by reducing the max_depth, there is significant decrease in the error ","metadata":{}},{"cell_type":"code","source":"DT_model = DecisionTreeRegressor(random_state = 28, max_depth = 8, max_features = 'sqrt')\nDT_model1= DecisionTreeRegressor(random_state = 28, max_depth = 10, max_features = 0.6)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:44.358035Z","iopub.execute_input":"2025-01-18T13:11:44.358475Z","iopub.status.idle":"2025-01-18T13:11:44.363591Z","shell.execute_reply.started":"2025-01-18T13:11:44.358445Z","shell.execute_reply":"2025-01-18T13:11:44.362184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_model.fit(X_train, X_targets)\nDT_model1.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:48.518295Z","iopub.execute_input":"2025-01-18T13:11:48.518654Z","iopub.status.idle":"2025-01-18T13:11:58.130622Z","shell.execute_reply.started":"2025-01-18T13:11:48.518625Z","shell.execute_reply":"2025-01-18T13:11:58.12925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_X_predicts = DT_model.predict(X_train)\nDT_val_predicts = DT_model.predict(val_train)\nDT1_X_predicts = DT_model1.predict(X_train)\nDT1_val_predicts = DT_model1.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:58.132099Z","iopub.execute_input":"2025-01-18T13:11:58.132411Z","iopub.status.idle":"2025-01-18T13:11:58.661769Z","shell.execute_reply.started":"2025-01-18T13:11:58.132371Z","shell.execute_reply":"2025-01-18T13:11:58.660636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DT_X_rmse = np.sqrt(mean_squared_error(X_targets, DT_X_predicts))\nDT_val_rmse = np.sqrt(mean_squared_error(val_targets, DT_val_predicts))\nDT1_X_rmse = np.sqrt(mean_squared_error(X_targets, DT1_X_predicts))\nDT2_val_rmse = np.sqrt(mean_squared_error(val_targets, DT1_val_predicts))\nprint(DT_X_rmse, DT_val_rmse)\nprint(DT_X_rmse, DT_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:11:58.663565Z","iopub.execute_input":"2025-01-18T13:11:58.663977Z","iopub.status.idle":"2025-01-18T13:11:58.686599Z","shell.execute_reply.started":"2025-01-18T13:11:58.663935Z","shell.execute_reply":"2025-01-18T13:11:58.685314Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"other factors did not gave major change in the error","metadata":{}},{"cell_type":"markdown","source":"#### Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:12:04.047853Z","iopub.execute_input":"2025-01-18T13:12:04.048258Z","iopub.status.idle":"2025-01-18T13:12:04.052991Z","shell.execute_reply.started":"2025-01-18T13:12:04.048225Z","shell.execute_reply":"2025-01-18T13:12:04.051671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF=RandomForestRegressor(random_state=28, n_jobs=-1, n_estimators = 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:12:05.567347Z","iopub.execute_input":"2025-01-18T13:12:05.567747Z","iopub.status.idle":"2025-01-18T13:12:05.572937Z","shell.execute_reply.started":"2025-01-18T13:12:05.567713Z","shell.execute_reply":"2025-01-18T13:12:05.571499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:12:06.115564Z","iopub.execute_input":"2025-01-18T13:12:06.115947Z","iopub.status.idle":"2025-01-18T13:13:43.074071Z","shell.execute_reply.started":"2025-01-18T13:12:06.115918Z","shell.execute_reply":"2025-01-18T13:13:43.0724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF_X_predicts = RF.predict(X_train)\nRF_val_predicts = RF.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:13:43.07552Z","iopub.execute_input":"2025-01-18T13:13:43.075846Z","iopub.status.idle":"2025-01-18T13:13:48.757514Z","shell.execute_reply.started":"2025-01-18T13:13:43.075818Z","shell.execute_reply":"2025-01-18T13:13:48.75667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF_X_rmse = np.sqrt(mean_squared_error(X_targets, RF_X_predicts))\nRF_val_rmse = np.sqrt(mean_squared_error(val_targets, RF_val_predicts))\nprint(RF_X_rmse, RF_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:13:48.75953Z","iopub.execute_input":"2025-01-18T13:13:48.759934Z","iopub.status.idle":"2025-01-18T13:13:48.775163Z","shell.execute_reply.started":"2025-01-18T13:13:48.759906Z","shell.execute_reply":"2025-01-18T13:13:48.773844Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF1=RandomForestRegressor(random_state=28, n_jobs=-1, n_estimators = 10, max_depth = 7)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:13:48.77698Z","iopub.execute_input":"2025-01-18T13:13:48.777386Z","iopub.status.idle":"2025-01-18T13:13:48.791736Z","shell.execute_reply.started":"2025-01-18T13:13:48.777343Z","shell.execute_reply":"2025-01-18T13:13:48.790287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF1.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:13:48.793248Z","iopub.execute_input":"2025-01-18T13:13:48.793763Z","iopub.status.idle":"2025-01-18T13:14:16.282322Z","shell.execute_reply.started":"2025-01-18T13:13:48.793717Z","shell.execute_reply":"2025-01-18T13:14:16.281172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF1_X_predicts = RF1.predict(X_train)\nRF1_val_predicts = RF1.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:16.283469Z","iopub.execute_input":"2025-01-18T13:14:16.28378Z","iopub.status.idle":"2025-01-18T13:14:16.76695Z","shell.execute_reply.started":"2025-01-18T13:14:16.283753Z","shell.execute_reply":"2025-01-18T13:14:16.765955Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF1_X_rmse = np.sqrt(mean_squared_error(X_targets, RF1_X_predicts))\nRF1_val_rmse = np.sqrt(mean_squared_error(val_targets, RF1_val_predicts))\nprint(RF1_X_rmse, RF1_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:16.768008Z","iopub.execute_input":"2025-01-18T13:14:16.768366Z","iopub.status.idle":"2025-01-18T13:14:16.783217Z","shell.execute_reply.started":"2025-01-18T13:14:16.768338Z","shell.execute_reply":"2025-01-18T13:14:16.781892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF2=RandomForestRegressor(random_state=28, n_jobs=-1, n_estimators = 7, max_depth = 7)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:16.78688Z","iopub.execute_input":"2025-01-18T13:14:16.787251Z","iopub.status.idle":"2025-01-18T13:14:16.796946Z","shell.execute_reply.started":"2025-01-18T13:14:16.787222Z","shell.execute_reply":"2025-01-18T13:14:16.795791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF2.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:16.798584Z","iopub.execute_input":"2025-01-18T13:14:16.798907Z","iopub.status.idle":"2025-01-18T13:14:37.056792Z","shell.execute_reply.started":"2025-01-18T13:14:16.798877Z","shell.execute_reply":"2025-01-18T13:14:37.055228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF2_X_predicts = RF2.predict(X_train)\nRF2_val_predicts = RF2.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:37.058498Z","iopub.execute_input":"2025-01-18T13:14:37.058976Z","iopub.status.idle":"2025-01-18T13:14:37.464987Z","shell.execute_reply.started":"2025-01-18T13:14:37.058934Z","shell.execute_reply":"2025-01-18T13:14:37.463763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF2_X_rmse = np.sqrt(mean_squared_error(X_targets, RF2_X_predicts))\nRF2_val_rmse = np.sqrt(mean_squared_error(val_targets, RF2_val_predicts))\nprint(RF2_X_rmse, RF2_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:37.466222Z","iopub.execute_input":"2025-01-18T13:14:37.466523Z","iopub.status.idle":"2025-01-18T13:14:37.480486Z","shell.execute_reply.started":"2025-01-18T13:14:37.466497Z","shell.execute_reply":"2025-01-18T13:14:37.479201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF3 = RandomForestRegressor(random_state=28, n_jobs=-1, n_estimators = 7, max_depth = 7, max_samples = 0.7)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:37.481534Z","iopub.execute_input":"2025-01-18T13:14:37.482091Z","iopub.status.idle":"2025-01-18T13:14:37.491725Z","shell.execute_reply.started":"2025-01-18T13:14:37.482014Z","shell.execute_reply":"2025-01-18T13:14:37.490362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF3.fit(X_train, X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:37.492815Z","iopub.execute_input":"2025-01-18T13:14:37.493217Z","iopub.status.idle":"2025-01-18T13:14:53.32445Z","shell.execute_reply.started":"2025-01-18T13:14:37.493186Z","shell.execute_reply":"2025-01-18T13:14:53.323317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF3_X_predicts = RF3.predict(X_train)\nRF3_val_predicts = RF3.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:53.325432Z","iopub.execute_input":"2025-01-18T13:14:53.325757Z","iopub.status.idle":"2025-01-18T13:14:53.743444Z","shell.execute_reply.started":"2025-01-18T13:14:53.32573Z","shell.execute_reply":"2025-01-18T13:14:53.741934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"RF3_X_rmse = np.sqrt(mean_squared_error(X_targets, RF3_X_predicts))\nRF3_val_rmse = np.sqrt(mean_squared_error(val_targets, RF3_val_predicts))\nprint(RF2_X_rmse, RF2_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:53.744837Z","iopub.execute_input":"2025-01-18T13:14:53.745288Z","iopub.status.idle":"2025-01-18T13:14:53.760661Z","shell.execute_reply.started":"2025-01-18T13:14:53.745248Z","shell.execute_reply":"2025-01-18T13:14:53.759351Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"the least error we have found is from DT which is of 843.3","metadata":{}},{"cell_type":"markdown","source":"#### XGBoost","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:53.761826Z","iopub.execute_input":"2025-01-18T13:14:53.762229Z","iopub.status.idle":"2025-01-18T13:14:53.774126Z","shell.execute_reply.started":"2025-01-18T13:14:53.762196Z","shell.execute_reply":"2025-01-18T13:14:53.77281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Xgb_model = XGBRegressor(n_jobs=-1, random_state = 28, n_estimators = 10, max_depth = 8)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:53.775232Z","iopub.execute_input":"2025-01-18T13:14:53.775658Z","iopub.status.idle":"2025-01-18T13:14:53.794942Z","shell.execute_reply.started":"2025-01-18T13:14:53.775626Z","shell.execute_reply":"2025-01-18T13:14:53.793567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Xgb_model.fit(X_train,X_targets)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:53.796119Z","iopub.execute_input":"2025-01-18T13:14:53.796558Z","iopub.status.idle":"2025-01-18T13:14:56.755718Z","shell.execute_reply.started":"2025-01-18T13:14:53.796516Z","shell.execute_reply":"2025-01-18T13:14:56.754759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_x_predicts=Xgb_model.predict(X_train)\nxbg_val_predicts=Xgb_model.predict(val_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:56.759492Z","iopub.execute_input":"2025-01-18T13:14:56.760689Z","iopub.status.idle":"2025-01-18T13:14:57.22428Z","shell.execute_reply.started":"2025-01-18T13:14:56.760595Z","shell.execute_reply":"2025-01-18T13:14:57.223386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_x_rmse = np.sqrt(mean_squared_error(X_targets, xgb_x_predicts))\nxgb_val_rmse = np.sqrt(mean_squared_error(val_targets, xbg_val_predicts))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:57.225004Z","iopub.execute_input":"2025-01-18T13:14:57.225316Z","iopub.status.idle":"2025-01-18T13:14:57.239433Z","shell.execute_reply.started":"2025-01-18T13:14:57.22529Z","shell.execute_reply":"2025-01-18T13:14:57.238202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(xgb_x_rmse, xgb_val_rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:14:57.240688Z","iopub.execute_input":"2025-01-18T13:14:57.241452Z","iopub.status.idle":"2025-01-18T13:14:57.25367Z","shell.execute_reply.started":"2025-01-18T13:14:57.2414Z","shell.execute_reply":"2025-01-18T13:14:57.252193Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"this is the lowest error we have found so far. \nthese are the best iteration for XGBoost model","metadata":{}},{"cell_type":"markdown","source":"### Predicting the test result values using XGBoost model","metadata":{}},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:15:57.107156Z","iopub.execute_input":"2025-01-18T13:15:57.107562Z","iopub.status.idle":"2025-01-18T13:15:57.143809Z","shell.execute_reply.started":"2025-01-18T13:15:57.107529Z","shell.execute_reply":"2025-01-18T13:15:57.142144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predict = Xgb_model.predict(test_df[num_cols + encoded_cols])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:16:59.434825Z","iopub.execute_input":"2025-01-18T13:16:59.435254Z","iopub.status.idle":"2025-01-18T13:16:59.945863Z","shell.execute_reply.started":"2025-01-18T13:16:59.43522Z","shell.execute_reply":"2025-01-18T13:16:59.938663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:17:07.508938Z","iopub.execute_input":"2025-01-18T13:17:07.509396Z","iopub.status.idle":"2025-01-18T13:17:07.517009Z","shell.execute_reply.started":"2025-01-18T13:17:07.509349Z","shell.execute_reply":"2025-01-18T13:17:07.515551Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Saving the model","metadata":{}},{"cell_type":"code","source":"import joblib","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:19:43.024301Z","iopub.execute_input":"2025-01-18T13:19:43.024784Z","iopub.status.idle":"2025-01-18T13:19:43.029924Z","shell.execute_reply.started":"2025-01-18T13:19:43.024749Z","shell.execute_reply":"2025-01-18T13:19:43.028473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Insurance_premium_prediction = {\n    'model': Xgb_model,\n    'imputer': mean_imputer,\n    'scaler': Scaler,\n    'encoder': encoder,\n    'input_cols': num_cols+encoded_cols,\n    'target_col': 'Premium Amount',\n    'numeric_cols': num_cols,\n    'categorical_cols': cat_cols,\n    'encoded_cols': encoded_cols\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:22:34.750786Z","iopub.execute_input":"2025-01-18T13:22:34.751215Z","iopub.status.idle":"2025-01-18T13:22:34.756441Z","shell.execute_reply.started":"2025-01-18T13:22:34.751182Z","shell.execute_reply":"2025-01-18T13:22:34.755175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"joblib.dump(Insurance_premium_prediction, 'Insurance_premium_prediction.joblib')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T13:23:02.119238Z","iopub.execute_input":"2025-01-18T13:23:02.119637Z","iopub.status.idle":"2025-01-18T13:23:02.133768Z","shell.execute_reply.started":"2025-01-18T13:23:02.119599Z","shell.execute_reply":"2025-01-18T13:23:02.132772Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}