{"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":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:31:47.220597Z","iopub.execute_input":"2024-12-30T09:31:47.221057Z","iopub.status.idle":"2024-12-30T09:31:47.230503Z","shell.execute_reply.started":"2024-12-30T09:31:47.22102Z","shell.execute_reply":"2024-12-30T09:31:47.229109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Importing the necessary libraries\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\nfrom joblib import dump","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:31:47.232111Z","iopub.execute_input":"2024-12-30T09:31:47.23242Z","iopub.status.idle":"2024-12-30T09:31:47.248337Z","shell.execute_reply.started":"2024-12-30T09:31:47.232393Z","shell.execute_reply":"2024-12-30T09:31:47.247302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:43.818334Z","iopub.execute_input":"2024-12-30T09:32:43.81868Z","iopub.status.idle":"2024-12-30T09:32:48.235027Z","shell.execute_reply.started":"2024-12-30T09:32:43.818617Z","shell.execute_reply":"2024-12-30T09:32:48.234111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:52.808277Z","iopub.execute_input":"2024-12-30T09:32:52.808617Z","iopub.status.idle":"2024-12-30T09:32:52.812924Z","shell.execute_reply.started":"2024-12-30T09:32:52.80859Z","shell.execute_reply":"2024-12-30T09:32:52.811728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:53.500671Z","iopub.execute_input":"2024-12-30T09:32:53.501061Z","iopub.status.idle":"2024-12-30T09:32:53.526471Z","shell.execute_reply.started":"2024-12-30T09:32:53.501019Z","shell.execute_reply":"2024-12-30T09:32:53.524924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Checking if there is any duplicate in the dataset\ndata.loc[data.duplicated(subset=['id'])]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:54.093938Z","iopub.execute_input":"2024-12-30T09:32:54.094263Z","iopub.status.idle":"2024-12-30T09:32:54.126307Z","shell.execute_reply.started":"2024-12-30T09:32:54.094238Z","shell.execute_reply":"2024-12-30T09:32:54.125247Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:54.585361Z","iopub.execute_input":"2024-12-30T09:32:54.585724Z","iopub.status.idle":"2024-12-30T09:32:55.217705Z","shell.execute_reply.started":"2024-12-30T09:32:54.585685Z","shell.execute_reply":"2024-12-30T09:32:55.2167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_columns = ['Age','Annual Income','Number of Dependents','Health Score','Previous Claims','Vehicle Age',\n                    'Credit Score','Insurance Duration','Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:57.590615Z","iopub.execute_input":"2024-12-30T09:32:57.591011Z","iopub.status.idle":"2024-12-30T09:32:57.59549Z","shell.execute_reply.started":"2024-12-30T09:32:57.590979Z","shell.execute_reply":"2024-12-30T09:32:57.59436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Defining a function to remove the extreme outliers \ndef find_extreme_outliers(column):\n    Q1=column.quantile(0.25)\n    Q3=column.quantile(0.75)\n    IQR = Q3-Q1\n    extreme = data[(column<(Q1-3*IQR) )| (column>(Q3+3*IQR))]\n    return(extreme)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:58.119212Z","iopub.execute_input":"2024-12-30T09:32:58.119535Z","iopub.status.idle":"2024-12-30T09:32:58.124809Z","shell.execute_reply.started":"2024-12-30T09:32:58.119508Z","shell.execute_reply":"2024-12-30T09:32:58.123503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column in selected_columns:\n    extreme_outliers = find_extreme_outliers(data[column])\n    if len(extreme_outliers)>0:\n        print(\"Extreme outliers are found in\",column,\" and the count is \",len(extreme_outliers))\n        print(\"Extreme outliers are indexed at:\",extreme_outliers.index)\n        data.drop(extreme_outliers.index,axis=0,inplace=True)\n    else:\n        print(\"No extreme outliers found in\",column)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:58.543762Z","iopub.execute_input":"2024-12-30T09:32:58.544101Z","iopub.status.idle":"2024-12-30T09:32:59.490682Z","shell.execute_reply.started":"2024-12-30T09:32:58.544067Z","shell.execute_reply":"2024-12-30T09:32:59.489716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.isnull().mean()*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:32:59.669461Z","iopub.execute_input":"2024-12-30T09:32:59.669849Z","iopub.status.idle":"2024-12-30T09:33:00.293282Z","shell.execute_reply.started":"2024-12-30T09:32:59.669817Z","shell.execute_reply":"2024-12-30T09:33:00.292236Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Removing the null values by replacing it with mean or median or NA wherever necessary","metadata":{}},{"cell_type":"code","source":"median_age = data[\"Age\"].median()\ndata[\"Age\"].fillna(median_age,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Age\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:01.478918Z","iopub.execute_input":"2024-12-30T09:33:01.479255Z","iopub.status.idle":"2024-12-30T09:33:01.518477Z","shell.execute_reply.started":"2024-12-30T09:33:01.479227Z","shell.execute_reply":"2024-12-30T09:33:01.517411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_income = data[\"Annual Income\"].median()\ndata[\"Annual Income\"].fillna(median_income,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Annual Income\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:01.768381Z","iopub.execute_input":"2024-12-30T09:33:01.768746Z","iopub.status.idle":"2024-12-30T09:33:01.808222Z","shell.execute_reply.started":"2024-12-30T09:33:01.768709Z","shell.execute_reply":"2024-12-30T09:33:01.807134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_dependents = data[\"Number of Dependents\"].median()\ndata[\"Number of Dependents\"].fillna(median_dependents,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Number of Dependents\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:02.852035Z","iopub.execute_input":"2024-12-30T09:33:02.852406Z","iopub.status.idle":"2024-12-30T09:33:02.895577Z","shell.execute_reply.started":"2024-12-30T09:33:02.852376Z","shell.execute_reply":"2024-12-30T09:33:02.894388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_health_score = data[\"Health Score\"].median()\ndata[\"Health Score\"].fillna(median_health_score,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Health Score\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:03.110309Z","iopub.execute_input":"2024-12-30T09:33:03.110703Z","iopub.status.idle":"2024-12-30T09:33:03.14681Z","shell.execute_reply.started":"2024-12-30T09:33:03.110659Z","shell.execute_reply":"2024-12-30T09:33:03.145629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_credit_score = data[\"Credit Score\"].median()\ndata[\"Credit Score\"].fillna(median_credit_score,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Credit Score\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:03.309743Z","iopub.execute_input":"2024-12-30T09:33:03.310108Z","iopub.status.idle":"2024-12-30T09:33:03.353557Z","shell.execute_reply.started":"2024-12-30T09:33:03.310075Z","shell.execute_reply":"2024-12-30T09:33:03.352608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_vehicle_age = data[\"Vehicle Age\"].median()\ndata[\"Vehicle Age\"].fillna(median_vehicle_age,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Vehicle Age\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:03.525909Z","iopub.execute_input":"2024-12-30T09:33:03.526403Z","iopub.status.idle":"2024-12-30T09:33:03.566587Z","shell.execute_reply.started":"2024-12-30T09:33:03.52636Z","shell.execute_reply":"2024-12-30T09:33:03.565348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_insurance_duraction = data[\"Insurance Duration\"].median()\ndata[\"Insurance Duration\"].fillna(median_insurance_duraction,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Insurance Duration\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:04.781396Z","iopub.execute_input":"2024-12-30T09:33:04.78178Z","iopub.status.idle":"2024-12-30T09:33:04.818081Z","shell.execute_reply.started":"2024-12-30T09:33:04.781747Z","shell.execute_reply":"2024-12-30T09:33:04.817136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_previous_claims = data[\"Previous Claims\"].median()\ndata[\"Previous Claims\"].fillna(median_previous_claims,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Previous Claims\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:04.979018Z","iopub.execute_input":"2024-12-30T09:33:04.979385Z","iopub.status.idle":"2024-12-30T09:33:05.030362Z","shell.execute_reply.started":"2024-12-30T09:33:04.979353Z","shell.execute_reply":"2024-12-30T09:33:05.029244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"Marital Status\"].fillna(\"NA\",inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Marital Status\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:05.189262Z","iopub.execute_input":"2024-12-30T09:33:05.189588Z","iopub.status.idle":"2024-12-30T09:33:05.312812Z","shell.execute_reply.started":"2024-12-30T09:33:05.189561Z","shell.execute_reply":"2024-12-30T09:33:05.311739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"Occupation\"].fillna(\"NA\",inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Occupation\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:05.314028Z","iopub.execute_input":"2024-12-30T09:33:05.314356Z","iopub.status.idle":"2024-12-30T09:33:05.439957Z","shell.execute_reply.started":"2024-12-30T09:33:05.314319Z","shell.execute_reply":"2024-12-30T09:33:05.438577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"Customer Feedback\"].fillna(\"NA\",inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",data[\"Customer Feedback\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:06.437793Z","iopub.execute_input":"2024-12-30T09:33:06.438169Z","iopub.status.idle":"2024-12-30T09:33:06.560745Z","shell.execute_reply.started":"2024-12-30T09:33:06.438141Z","shell.execute_reply":"2024-12-30T09:33:06.559835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:06.628595Z","iopub.execute_input":"2024-12-30T09:33:06.62898Z","iopub.status.idle":"2024-12-30T09:33:07.255042Z","shell.execute_reply.started":"2024-12-30T09:33:06.628952Z","shell.execute_reply":"2024-12-30T09:33:07.254057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:07.256147Z","iopub.execute_input":"2024-12-30T09:33:07.256398Z","iopub.status.idle":"2024-12-30T09:33:07.2789Z","shell.execute_reply.started":"2024-12-30T09:33:07.256376Z","shell.execute_reply":"2024-12-30T09:33:07.277776Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Ecoding the dataset","metadata":{}},{"cell_type":"code","source":"data['Gender'] = data['Gender'].map({'Male': 1, 'Female': 0})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:09.810183Z","iopub.execute_input":"2024-12-30T09:33:09.81053Z","iopub.status.idle":"2024-12-30T09:33:09.889576Z","shell.execute_reply.started":"2024-12-30T09:33:09.810498Z","shell.execute_reply":"2024-12-30T09:33:09.888352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['Smoking Status'] = data['Smoking Status'].map({'Yes': 1, 'No': 0})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:10.029217Z","iopub.execute_input":"2024-12-30T09:33:10.029573Z","iopub.status.idle":"2024-12-30T09:33:10.104746Z","shell.execute_reply.started":"2024-12-30T09:33:10.029535Z","shell.execute_reply":"2024-12-30T09:33:10.1036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.get_dummies(data, columns=['Education Level'], drop_first=True)\ndata = pd.get_dummies(data, columns=['Occupation'], drop_first=True)\ndata = pd.get_dummies(data, columns=['Location'], drop_first=True)\ndata = pd.get_dummies(data, columns=['Policy Type'], drop_first=True)\ndata = pd.get_dummies(data, columns=['Customer Feedback'], drop_first=True)\ndata = pd.get_dummies(data, columns=['Exercise Frequency'], drop_first=True)\ndata = pd.get_dummies(data, columns=['Property Type'], drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:10.219208Z","iopub.execute_input":"2024-12-30T09:33:10.219528Z","iopub.status.idle":"2024-12-30T09:33:12.875018Z","shell.execute_reply.started":"2024-12-30T09:33:10.219502Z","shell.execute_reply":"2024-12-30T09:33:12.873992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.get_dummies(data, columns=['Marital Status'], drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:12.876117Z","iopub.execute_input":"2024-12-30T09:33:12.876382Z","iopub.status.idle":"2024-12-30T09:33:13.134467Z","shell.execute_reply.started":"2024-12-30T09:33:12.87636Z","shell.execute_reply":"2024-12-30T09:33:13.133444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:13.13582Z","iopub.execute_input":"2024-12-30T09:33:13.136112Z","iopub.status.idle":"2024-12-30T09:33:13.16419Z","shell.execute_reply.started":"2024-12-30T09:33:13.136084Z","shell.execute_reply":"2024-12-30T09:33:13.163103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" data = data.drop(columns=['Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:13.165323Z","iopub.execute_input":"2024-12-30T09:33:13.16561Z","iopub.status.idle":"2024-12-30T09:33:13.221328Z","shell.execute_reply.started":"2024-12-30T09:33:13.165584Z","shell.execute_reply":"2024-12-30T09:33:13.220285Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Building the Regression Model","metadata":{}},{"cell_type":"code","source":"data['Premium Amount'] = np.log1p(data['Premium Amount'])  # Log transform\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:47:39.709414Z","iopub.execute_input":"2024-12-30T09:47:39.709821Z","iopub.status.idle":"2024-12-30T09:47:39.740118Z","shell.execute_reply.started":"2024-12-30T09:47:39.709787Z","shell.execute_reply":"2024-12-30T09:47:39.73867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = data.drop(['Premium Amount'], axis=1)\ny = data['Premium Amount']\n\n\nsc = StandardScaler()\nX = sc.fit_transform(x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:47:39.918074Z","iopub.execute_input":"2024-12-30T09:47:39.918414Z","iopub.status.idle":"2024-12-30T09:47:42.134551Z","shell.execute_reply.started":"2024-12-30T09:47:39.918388Z","shell.execute_reply":"2024-12-30T09:47:42.13359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:47:42.135931Z","iopub.execute_input":"2024-12-30T09:47:42.13625Z","iopub.status.idle":"2024-12-30T09:47:42.753591Z","shell.execute_reply.started":"2024-12-30T09:47:42.136224Z","shell.execute_reply":"2024-12-30T09:47:42.752735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nmodel = LinearRegression()\nmodel.fit(X_train, y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:47:42.755186Z","iopub.execute_input":"2024-12-30T09:47:42.755589Z","iopub.status.idle":"2024-12-30T09:47:44.686594Z","shell.execute_reply.started":"2024-12-30T09:47:42.755549Z","shell.execute_reply":"2024-12-30T09:47:44.685502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\ny_pred = model.predict(X_test)\nprint(\"Mean Absolute Error:\", mean_absolute_error(y_test, y_pred))\nprint(\"Mean Squared Error:\", mean_squared_error(y_test, y_pred))\nprint(\"R-squared:\", r2_score(y_test, y_pred))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:47:44.688067Z","iopub.execute_input":"2024-12-30T09:47:44.688424Z","iopub.status.idle":"2024-12-30T09:47:44.714603Z","shell.execute_reply.started":"2024-12-30T09:47:44.688387Z","shell.execute_reply":"2024-12-30T09:47:44.713072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Save the trained model\ndump(model, \"trained_model.joblib\")\nprint(\"Model saved successfully.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:47:01.747953Z","iopub.execute_input":"2024-12-30T09:47:01.748285Z","iopub.status.idle":"2024-12-30T09:47:01.75432Z","shell.execute_reply.started":"2024-12-30T09:47:01.748257Z","shell.execute_reply":"2024-12-30T09:47:01.753015Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Exploring the test dataset","metadata":{}},{"cell_type":"code","source":"test_data=pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:22.577625Z","iopub.execute_input":"2024-12-30T09:33:22.578044Z","iopub.status.idle":"2024-12-30T09:33:25.353775Z","shell.execute_reply.started":"2024-12-30T09:33:22.578004Z","shell.execute_reply":"2024-12-30T09:33:25.352653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.isnull().mean()*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:25.355035Z","iopub.execute_input":"2024-12-30T09:33:25.35538Z","iopub.status.idle":"2024-12-30T09:33:25.769857Z","shell.execute_reply.started":"2024-12-30T09:33:25.355351Z","shell.execute_reply":"2024-12-30T09:33:25.768742Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:25.771464Z","iopub.execute_input":"2024-12-30T09:33:25.771862Z","iopub.status.idle":"2024-12-30T09:33:25.777732Z","shell.execute_reply.started":"2024-12-30T09:33:25.771821Z","shell.execute_reply":"2024-12-30T09:33:25.776534Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Removing the null values and replacing with mean or median or NA wherever necessary in test dataset","metadata":{}},{"cell_type":"code","source":"median_age = test_data[\"Age\"].median()\ntest_data[\"Age\"].fillna(median_age,inplace=True)\nprint(\"After treating null values in Age column, the missing values are : \",test_data[\"Age\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:26.974561Z","iopub.execute_input":"2024-12-30T09:33:26.974959Z","iopub.status.idle":"2024-12-30T09:33:27.00258Z","shell.execute_reply.started":"2024-12-30T09:33:26.974927Z","shell.execute_reply":"2024-12-30T09:33:27.001604Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_income = test_data[\"Annual Income\"].median()\ntest_data[\"Annual Income\"].fillna(median_income,inplace=True)\nprint(\"After treating null values in Annual Income column, the missing values are : \",test_data[\"Annual Income\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:27.186533Z","iopub.execute_input":"2024-12-30T09:33:27.186926Z","iopub.status.idle":"2024-12-30T09:33:27.21579Z","shell.execute_reply.started":"2024-12-30T09:33:27.186892Z","shell.execute_reply":"2024-12-30T09:33:27.214841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_dependents = test_data[\"Number of Dependents\"].median()\ntest_data[\"Number of Dependents\"].fillna(median_dependents,inplace=True)\nprint(\"After treating null values in Number of Dependents column, the missing values are : \",\n      test_data[\"Number of Dependents\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:27.385455Z","iopub.execute_input":"2024-12-30T09:33:27.385818Z","iopub.status.idle":"2024-12-30T09:33:27.415401Z","shell.execute_reply.started":"2024-12-30T09:33:27.385781Z","shell.execute_reply":"2024-12-30T09:33:27.41437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_health_score = test_data[\"Health Score\"].median()\ntest_data[\"Health Score\"].fillna(median_health_score,inplace=True)\nprint(\"After treating null values in Health Score column, the missing values are : \",test_data[\"Health Score\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:27.579848Z","iopub.execute_input":"2024-12-30T09:33:27.580198Z","iopub.status.idle":"2024-12-30T09:33:27.608721Z","shell.execute_reply.started":"2024-12-30T09:33:27.580171Z","shell.execute_reply":"2024-12-30T09:33:27.607761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_credit_score = test_data[\"Credit Score\"].median()\ntest_data[\"Credit Score\"].fillna(median_credit_score,inplace=True)\nprint(\"After treating null values in Credit Score column, the missing values are : \",test_data[\"Credit Score\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:27.736041Z","iopub.execute_input":"2024-12-30T09:33:27.736373Z","iopub.status.idle":"2024-12-30T09:33:27.768373Z","shell.execute_reply.started":"2024-12-30T09:33:27.736345Z","shell.execute_reply":"2024-12-30T09:33:27.767144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_vehicle_age = test_data[\"Vehicle Age\"].median()\ntest_data[\"Vehicle Age\"].fillna(median_vehicle_age,inplace=True)\nprint(\"After treating null values in Vehicle Age column, the missing values are : \",test_data[\"Vehicle Age\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:27.898397Z","iopub.execute_input":"2024-12-30T09:33:27.898787Z","iopub.status.idle":"2024-12-30T09:33:27.924199Z","shell.execute_reply.started":"2024-12-30T09:33:27.898753Z","shell.execute_reply":"2024-12-30T09:33:27.923191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_insurance_duraction = test_data[\"Insurance Duration\"].median()\ntest_data[\"Insurance Duration\"].fillna(median_insurance_duraction,inplace=True)\nprint(\"After treating null values in Insurance Duration column, the missing values are : \",test_data[\"Insurance Duration\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:28.067432Z","iopub.execute_input":"2024-12-30T09:33:28.067787Z","iopub.status.idle":"2024-12-30T09:33:28.095155Z","shell.execute_reply.started":"2024-12-30T09:33:28.067753Z","shell.execute_reply":"2024-12-30T09:33:28.094236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_previous_claims = test_data[\"Previous Claims\"].median()\ntest_data[\"Previous Claims\"].fillna(median_previous_claims,inplace=True)\nprint(\"After treating null values in Previous Claims column, the missing values are : \",test_data[\"Previous Claims\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:30.559471Z","iopub.execute_input":"2024-12-30T09:33:30.55981Z","iopub.status.idle":"2024-12-30T09:33:30.595567Z","shell.execute_reply.started":"2024-12-30T09:33:30.559782Z","shell.execute_reply":"2024-12-30T09:33:30.594543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data[\"Marital Status\"].fillna(\"NA\",inplace=True)\nprint(\"After treating null values in Marital Status column, the missing values are : \",test_data[\"Marital Status\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:30.747544Z","iopub.execute_input":"2024-12-30T09:33:30.748274Z","iopub.status.idle":"2024-12-30T09:33:30.834496Z","shell.execute_reply.started":"2024-12-30T09:33:30.748219Z","shell.execute_reply":"2024-12-30T09:33:30.833476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data[\"Occupation\"].fillna(\"NA\",inplace=True)\nprint(\"After treating null values in Occupation column, the missing values are : \",test_data[\"Occupation\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:30.968383Z","iopub.execute_input":"2024-12-30T09:33:30.968733Z","iopub.status.idle":"2024-12-30T09:33:31.056474Z","shell.execute_reply.started":"2024-12-30T09:33:30.968703Z","shell.execute_reply":"2024-12-30T09:33:31.055379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data[\"Customer Feedback\"].fillna(\"NA\",inplace=True)\nprint(\"After treating null values in Customer Feedback column, the missing values are : \",test_data[\"Customer Feedback\"].isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:31.146432Z","iopub.execute_input":"2024-12-30T09:33:31.146803Z","iopub.status.idle":"2024-12-30T09:33:31.233512Z","shell.execute_reply.started":"2024-12-30T09:33:31.146772Z","shell.execute_reply":"2024-12-30T09:33:31.232252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:31.339778Z","iopub.execute_input":"2024-12-30T09:33:31.340168Z","iopub.status.idle":"2024-12-30T09:33:31.758512Z","shell.execute_reply.started":"2024-12-30T09:33:31.340139Z","shell.execute_reply":"2024-12-30T09:33:31.757561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data['Gender'] = test_data['Gender'].map({'Male': 1, 'Female': 0})\ntest_data['Smoking Status'] = test_data['Smoking Status'].map({'Yes': 1, 'No': 0})\n\ntest_data = pd.get_dummies(test_data, columns=['Marital Status'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Education Level'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Occupation'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Location'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Policy Type'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Customer Feedback'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Exercise Frequency'], drop_first=True)\ntest_data = pd.get_dummies(test_data, columns=['Property Type'], drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:31.759815Z","iopub.execute_input":"2024-12-30T09:33:31.760186Z","iopub.status.idle":"2024-12-30T09:33:33.6524Z","shell.execute_reply.started":"2024-12-30T09:33:31.76015Z","shell.execute_reply":"2024-12-30T09:33:33.651047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:36.331941Z","iopub.execute_input":"2024-12-30T09:33:36.332279Z","iopub.status.idle":"2024-12-30T09:33:36.35899Z","shell.execute_reply.started":"2024-12-30T09:33:36.332252Z","shell.execute_reply":"2024-12-30T09:33:36.358019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = test_data.dropna(subset=['Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:43.443472Z","iopub.execute_input":"2024-12-30T09:33:43.443838Z","iopub.status.idle":"2024-12-30T09:33:43.558547Z","shell.execute_reply.started":"2024-12-30T09:33:43.443803Z","shell.execute_reply":"2024-12-30T09:33:43.556741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_features = ['id','Age','Gender','Annual Income','Number of Dependents','Health Score','Previous Claims',\n                  'Vehicle Age','Credit Score','Insurance Duration','Smoking Status','Education Level_High School',\n                  \"Education Level_Master's\",'Education Level_PhD',\t'Occupation_NA'\t,'Occupation_Self-Employed','Occupation_Unemployed',\n                  'Location_Suburban','Location_Urban','Policy Type_Comprehensive',\t'Policy Type_Premium','Customer Feedback_Good',\n                  'Customer Feedback_NA','Customer Feedback_Poor','Exercise Frequency_Monthly',\t'Exercise Frequency_Rarely',\n                  'Exercise Frequency_Weekly','Property Type_Condo','Property Type_House','Marital Status_Married','Marital Status_NA',\t\n                  'Marital Status_Single']  # Replace with your actual feature names\ntest_data = test_data[model_features]\n\n# Make predictions\npredictions = model.predict(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:33:43.727618Z","iopub.execute_input":"2024-12-30T09:33:43.728243Z","iopub.status.idle":"2024-12-30T09:33:43.921353Z","shell.execute_reply.started":"2024-12-30T09:33:43.728203Z","shell.execute_reply":"2024-12-30T09:33:43.919118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = pd.DataFrame({\n    'id': test_data['id'],  # Ensure 'id' exists in your test data\n    'Premium Amount': abs(predictions)\n})\n\n# Save the output to a new CSV file\noutput.to_csv('submission.csv', index=False)\nprint(\"Predictions saved to 'submission.csv'\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:37:04.879667Z","iopub.execute_input":"2024-12-30T09:37:04.880138Z","iopub.status.idle":"2024-12-30T09:37:06.514069Z","shell.execute_reply.started":"2024-12-30T09:37:04.880104Z","shell.execute_reply":"2024-12-30T09:37:06.51307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(abs(predictions))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:34:07.874418Z","iopub.execute_input":"2024-12-30T09:34:07.87492Z","iopub.status.idle":"2024-12-30T09:34:07.8878Z","shell.execute_reply.started":"2024-12-30T09:34:07.874866Z","shell.execute_reply":"2024-12-30T09:34:07.885608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd, numpy as np\n\ntrain = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\nprint(\"Train shape:\",train.shape)\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:45:24.369289Z","iopub.execute_input":"2024-12-30T09:45:24.369633Z","iopub.status.idle":"2024-12-30T09:45:28.567787Z","shell.execute_reply.started":"2024-12-30T09:45:24.369606Z","shell.execute_reply":"2024-12-30T09:45:28.566564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_log_error\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_log_error","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T09:49:15.259936Z","iopub.execute_input":"2024-12-30T09:49:15.260269Z","iopub.status.idle":"2024-12-30T09:49:15.264938Z","shell.execute_reply.started":"2024-12-30T09:49:15.260241Z","shell.execute_reply":"2024-12-30T09:49:15.263753Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}