{"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 session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:18.802556Z","iopub.execute_input":"2025-08-17T18:59:18.802864Z","iopub.status.idle":"2025-08-17T18:59:19.182607Z","shell.execute_reply.started":"2025-08-17T18:59:18.802836Z","shell.execute_reply":"2025-08-17T18:59:19.18179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:19.183532Z","iopub.execute_input":"2025-08-17T18:59:19.184414Z","iopub.status.idle":"2025-08-17T18:59:19.571346Z","shell.execute_reply.started":"2025-08-17T18:59:19.184385Z","shell.execute_reply":"2025-08-17T18:59:19.570217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:19.572368Z","iopub.execute_input":"2025-08-17T18:59:19.572744Z","iopub.status.idle":"2025-08-17T18:59:30.271374Z","shell.execute_reply.started":"2025-08-17T18:59:19.572705Z","shell.execute_reply":"2025-08-17T18:59:30.270336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:30.272394Z","iopub.execute_input":"2025-08-17T18:59:30.272804Z","iopub.status.idle":"2025-08-17T18:59:30.279686Z","shell.execute_reply.started":"2025-08-17T18:59:30.272765Z","shell.execute_reply":"2025-08-17T18:59:30.278739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:30.280667Z","iopub.execute_input":"2025-08-17T18:59:30.28096Z","iopub.status.idle":"2025-08-17T18:59:30.298248Z","shell.execute_reply.started":"2025-08-17T18:59:30.280913Z","shell.execute_reply":"2025-08-17T18:59:30.29734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:30.300756Z","iopub.execute_input":"2025-08-17T18:59:30.301151Z","iopub.status.idle":"2025-08-17T18:59:30.966343Z","shell.execute_reply.started":"2025-08-17T18:59:30.301118Z","shell.execute_reply":"2025-08-17T18:59:30.96519Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.sample(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:30.968036Z","iopub.execute_input":"2025-08-17T18:59:30.9684Z","iopub.status.idle":"2025-08-17T18:59:31.042861Z","shell.execute_reply.started":"2025-08-17T18:59:30.968363Z","shell.execute_reply":"2025-08-17T18:59:31.041849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:31.043888Z","iopub.execute_input":"2025-08-17T18:59:31.044267Z","iopub.status.idle":"2025-08-17T18:59:31.669241Z","shell.execute_reply.started":"2025-08-17T18:59:31.044232Z","shell.execute_reply":"2025-08-17T18:59:31.667677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:31.670286Z","iopub.execute_input":"2025-08-17T18:59:31.670565Z","iopub.status.idle":"2025-08-17T18:59:32.084535Z","shell.execute_reply.started":"2025-08-17T18:59:31.67054Z","shell.execute_reply":"2025-08-17T18:59:32.083479Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Age Settlement","metadata":{}},{"cell_type":"code","source":"# age settle\nimport seaborn as sns \nsns.boxplot(train['Age'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:32.085584Z","iopub.execute_input":"2025-08-17T18:59:32.085878Z","iopub.status.idle":"2025-08-17T18:59:33.119167Z","shell.execute_reply.started":"2025-08-17T18:59:32.085852Z","shell.execute_reply":"2025-08-17T18:59:33.117871Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Age'].value_counts()    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.120542Z","iopub.execute_input":"2025-08-17T18:59:33.121172Z","iopub.status.idle":"2025-08-17T18:59:33.156775Z","shell.execute_reply.started":"2025-08-17T18:59:33.12113Z","shell.execute_reply":"2025-08-17T18:59:33.155686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# no outliers \n# hence replacable with mean\n# can use sklearn imputer\nx = train['Age'].mean().astype(int)\ntrain['Age'].fillna(x,inplace=True)\n# age settled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.157725Z","iopub.execute_input":"2025-08-17T18:59:33.158078Z","iopub.status.idle":"2025-08-17T18:59:33.185225Z","shell.execute_reply.started":"2025-08-17T18:59:33.158049Z","shell.execute_reply":"2025-08-17T18:59:33.183902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(test['Age'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.186342Z","iopub.execute_input":"2025-08-17T18:59:33.186684Z","iopub.status.idle":"2025-08-17T18:59:33.389331Z","shell.execute_reply.started":"2025-08-17T18:59:33.186651Z","shell.execute_reply":"2025-08-17T18:59:33.388524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = test['Age'].mean().astype(int)\ntest['Age'].fillna(y,inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.390443Z","iopub.execute_input":"2025-08-17T18:59:33.390698Z","iopub.status.idle":"2025-08-17T18:59:33.401359Z","shell.execute_reply.started":"2025-08-17T18:59:33.390677Z","shell.execute_reply":"2025-08-17T18:59:33.400462Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Annual Income Settlement","metadata":{}},{"cell_type":"code","source":"# null values in train and test dataset\n# Annual Income            44949\n# Annual Income            29860\n# can be settled in same way as age or by using sklearn imputer\nfrom sklearn.impute import SimpleImputer \nimputer = SimpleImputer(strategy = 'mean')\ntrain['Annual Income'] = imputer.fit_transform(train[['Annual Income']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.402281Z","iopub.execute_input":"2025-08-17T18:59:33.402643Z","iopub.status.idle":"2025-08-17T18:59:33.859001Z","shell.execute_reply.started":"2025-08-17T18:59:33.402609Z","shell.execute_reply":"2025-08-17T18:59:33.858039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Annual Income'] = imputer.fit_transform(test[['Annual Income']])\n# Annual Income settled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.859749Z","iopub.execute_input":"2025-08-17T18:59:33.860117Z","iopub.status.idle":"2025-08-17T18:59:33.884364Z","shell.execute_reply.started":"2025-08-17T18:59:33.860082Z","shell.execute_reply":"2025-08-17T18:59:33.883484Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Marital Status Settlement","metadata":{}},{"cell_type":"code","source":"# Marital Status           18529\n# Marital Status           12336\ntrain['Marital Status'].value_counts()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.885236Z","iopub.execute_input":"2025-08-17T18:59:33.885497Z","iopub.status.idle":"2025-08-17T18:59:33.977757Z","shell.execute_reply.started":"2025-08-17T18:59:33.885467Z","shell.execute_reply":"2025-08-17T18:59:33.976725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# filling missing values\n# freq_imputer= SimpleImputer(strategy= 'most_frequent')\n# train['Marital Status'] = freq_imputer.fit_transform(train[['Marital Status']])\nimport numpy as np\nls = ['Single', 'Married','Divorced']\ntrain['Marital Status']=train['Marital Status'].apply(lambda x: np.random.choice(ls) if pd.isna(x) else x)\ntrain['Marital Status'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:33.97875Z","iopub.execute_input":"2025-08-17T18:59:33.979131Z","iopub.status.idle":"2025-08-17T18:59:35.120966Z","shell.execute_reply.started":"2025-08-17T18:59:33.979095Z","shell.execute_reply":"2025-08-17T18:59:35.119869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Marital Status']=test['Marital Status'].apply(lambda x: np.random.choice(ls) if pd.isna(x) else x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:35.122114Z","iopub.execute_input":"2025-08-17T18:59:35.122479Z","iopub.status.idle":"2025-08-17T18:59:35.807604Z","shell.execute_reply.started":"2025-08-17T18:59:35.122445Z","shell.execute_reply":"2025-08-17T18:59:35.80653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# applying label encoding\nfrom sklearn.preprocessing import LabelEncoder\nx = LabelEncoder()\ntrain['Marital Status'] =x.fit_transform(train['Marital Status'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:35.812624Z","iopub.execute_input":"2025-08-17T18:59:35.812927Z","iopub.status.idle":"2025-08-17T18:59:36.048053Z","shell.execute_reply.started":"2025-08-17T18:59:35.8129Z","shell.execute_reply":"2025-08-17T18:59:36.04703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Marital Status'].value_counts()\n# 0 stands for divorced\n# 1 stands for married\n# 2 stands for Single\n# can be seen by comparing the value counts or by getting out the mapping\n# this must be same for the test dataset ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.05077Z","iopub.execute_input":"2025-08-17T18:59:36.051115Z","iopub.status.idle":"2025-08-17T18:59:36.065367Z","shell.execute_reply.started":"2025-08-17T18:59:36.051086Z","shell.execute_reply":"2025-08-17T18:59:36.064256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Marital Status'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.066415Z","iopub.execute_input":"2025-08-17T18:59:36.06679Z","iopub.status.idle":"2025-08-17T18:59:36.174218Z","shell.execute_reply.started":"2025-08-17T18:59:36.06675Z","shell.execute_reply":"2025-08-17T18:59:36.17307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mapping = [['Divorced', 'Married','Single']]\nfrom sklearn.preprocessing import OrdinalEncoder \nx = OrdinalEncoder(categories = mapping)\ntest['Marital Status']=x.fit_transform(test[['Marital Status']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.17545Z","iopub.execute_input":"2025-08-17T18:59:36.177094Z","iopub.status.idle":"2025-08-17T18:59:36.432497Z","shell.execute_reply.started":"2025-08-17T18:59:36.177051Z","shell.execute_reply":"2025-08-17T18:59:36.431417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Marital Status'].value_counts()\n# Marrital status settled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.433521Z","iopub.execute_input":"2025-08-17T18:59:36.433821Z","iopub.status.idle":"2025-08-17T18:59:36.455718Z","shell.execute_reply.started":"2025-08-17T18:59:36.433791Z","shell.execute_reply":"2025-08-17T18:59:36.454676Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Number of Dependents ","metadata":{}},{"cell_type":"code","source":"# Number of Dependents    109672\n# Number of Dependents    73130\ntrain['Number of Dependents'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.456784Z","iopub.execute_input":"2025-08-17T18:59:36.457239Z","iopub.status.idle":"2025-08-17T18:59:36.495333Z","shell.execute_reply.started":"2025-08-17T18:59:36.457199Z","shell.execute_reply":"2025-08-17T18:59:36.494367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Number of Dependents'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.496831Z","iopub.execute_input":"2025-08-17T18:59:36.497222Z","iopub.status.idle":"2025-08-17T18:59:36.532883Z","shell.execute_reply.started":"2025-08-17T18:59:36.497188Z","shell.execute_reply":"2025-08-17T18:59:36.531711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = SimpleImputer(strategy = 'median')\ntrain['Number of Dependents']= x.fit_transform(train[['Number of Dependents']])\ntest['Number of Dependents']= x.fit_transform(test[['Number of Dependents']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.53411Z","iopub.execute_input":"2025-08-17T18:59:36.534589Z","iopub.status.idle":"2025-08-17T18:59:36.757085Z","shell.execute_reply.started":"2025-08-17T18:59:36.534544Z","shell.execute_reply":"2025-08-17T18:59:36.755765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Number of Dependents'].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.758171Z","iopub.execute_input":"2025-08-17T18:59:36.758485Z","iopub.status.idle":"2025-08-17T18:59:36.76739Z","shell.execute_reply.started":"2025-08-17T18:59:36.758456Z","shell.execute_reply":"2025-08-17T18:59:36.766241Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Education level settlement","metadata":{}},{"cell_type":"code","source":"# Education level encoding\ntrain['Education Level'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.768571Z","iopub.execute_input":"2025-08-17T18:59:36.76889Z","iopub.status.idle":"2025-08-17T18:59:36.869926Z","shell.execute_reply.started":"2025-08-17T18:59:36.768862Z","shell.execute_reply":"2025-08-17T18:59:36.868817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = LabelEncoder()\ntrain['Education Level'] =x.fit_transform(train['Education Level'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:36.871012Z","iopub.execute_input":"2025-08-17T18:59:36.871439Z","iopub.status.idle":"2025-08-17T18:59:37.216826Z","shell.execute_reply.started":"2025-08-17T18:59:36.871403Z","shell.execute_reply":"2025-08-17T18:59:37.215423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Education Level'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.217878Z","iopub.execute_input":"2025-08-17T18:59:37.218223Z","iopub.status.idle":"2025-08-17T18:59:37.233001Z","shell.execute_reply.started":"2025-08-17T18:59:37.218187Z","shell.execute_reply":"2025-08-17T18:59:37.231924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Education Level'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.234407Z","iopub.execute_input":"2025-08-17T18:59:37.234842Z","iopub.status.idle":"2025-08-17T18:59:37.30942Z","shell.execute_reply.started":"2025-08-17T18:59:37.234804Z","shell.execute_reply":"2025-08-17T18:59:37.308368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mapping = [[\"Master's\",\"PhD\",\"Bachelor's\",\"High School\"]]\nx = OrdinalEncoder(categories = mapping)\ntest['Education Level']=x.fit_transform(test[['Education Level']])\n# Education Level settled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.310723Z","iopub.execute_input":"2025-08-17T18:59:37.311224Z","iopub.status.idle":"2025-08-17T18:59:37.534095Z","shell.execute_reply.started":"2025-08-17T18:59:37.311183Z","shell.execute_reply":"2025-08-17T18:59:37.533054Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Occupation Settlement","metadata":{}},{"cell_type":"code","source":"train['Occupation'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.534916Z","iopub.execute_input":"2025-08-17T18:59:37.535203Z","iopub.status.idle":"2025-08-17T18:59:37.616405Z","shell.execute_reply.started":"2025-08-17T18:59:37.53518Z","shell.execute_reply":"2025-08-17T18:59:37.615409Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Occupation              358075\n# Occupation              239125\ntrain['Occupation'].fillna(\"Unknown\",inplace=True)\ntest['Occupation'].fillna(\"Unknown\",inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.617519Z","iopub.execute_input":"2025-08-17T18:59:37.617842Z","iopub.status.idle":"2025-08-17T18:59:37.746522Z","shell.execute_reply.started":"2025-08-17T18:59:37.6178Z","shell.execute_reply":"2025-08-17T18:59:37.745492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Occupation'].value_counts()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.747478Z","iopub.execute_input":"2025-08-17T18:59:37.747807Z","iopub.status.idle":"2025-08-17T18:59:37.838361Z","shell.execute_reply.started":"2025-08-17T18:59:37.747771Z","shell.execute_reply":"2025-08-17T18:59:37.837374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = LabelEncoder()\ntrain['Occupation'] =x.fit_transform(train['Occupation'])\ntrain['Occupation'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:37.839381Z","iopub.execute_input":"2025-08-17T18:59:37.839889Z","iopub.status.idle":"2025-08-17T18:59:38.080251Z","shell.execute_reply.started":"2025-08-17T18:59:37.839853Z","shell.execute_reply":"2025-08-17T18:59:38.079288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Occupation'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.081252Z","iopub.execute_input":"2025-08-17T18:59:38.081577Z","iopub.status.idle":"2025-08-17T18:59:38.146083Z","shell.execute_reply.started":"2025-08-17T18:59:38.081538Z","shell.execute_reply":"2025-08-17T18:59:38.144983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = LabelEncoder()\ntest['Occupation'] =x.fit_transform(test['Occupation'])\ntest['Occupation'].value_counts()\n# Occupation settled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.147219Z","iopub.execute_input":"2025-08-17T18:59:38.147595Z","iopub.status.idle":"2025-08-17T18:59:38.315526Z","shell.execute_reply.started":"2025-08-17T18:59:38.147566Z","shell.execute_reply":"2025-08-17T18:59:38.314257Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Health Score Settlement","metadata":{}},{"cell_type":"code","source":"train['Health Score'].mean().astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.318096Z","iopub.execute_input":"2025-08-17T18:59:38.318409Z","iopub.status.idle":"2025-08-17T18:59:38.333316Z","shell.execute_reply.started":"2025-08-17T18:59:38.318381Z","shell.execute_reply":"2025-08-17T18:59:38.33107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Health Score'].mean().astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.334893Z","iopub.execute_input":"2025-08-17T18:59:38.335328Z","iopub.status.idle":"2025-08-17T18:59:38.346717Z","shell.execute_reply.started":"2025-08-17T18:59:38.335285Z","shell.execute_reply":"2025-08-17T18:59:38.34565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Health Score'].fillna(25,inplace = True)\ntest['Health Score'].fillna(25,inplace = True)\n# Health Score settled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.347807Z","iopub.execute_input":"2025-08-17T18:59:38.348198Z","iopub.status.idle":"2025-08-17T18:59:38.373274Z","shell.execute_reply.started":"2025-08-17T18:59:38.348158Z","shell.execute_reply":"2025-08-17T18:59:38.372024Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Previous Claims Settlement","metadata":{}},{"cell_type":"code","source":"train['Previous Claims'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.37438Z","iopub.execute_input":"2025-08-17T18:59:38.374772Z","iopub.status.idle":"2025-08-17T18:59:38.403153Z","shell.execute_reply.started":"2025-08-17T18:59:38.37473Z","shell.execute_reply":"2025-08-17T18:59:38.402066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Previous Claims'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.404382Z","iopub.execute_input":"2025-08-17T18:59:38.404769Z","iopub.status.idle":"2025-08-17T18:59:38.43429Z","shell.execute_reply.started":"2025-08-17T18:59:38.404705Z","shell.execute_reply":"2025-08-17T18:59:38.433271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = SimpleImputer(strategy = 'median')\ntrain['Previous Claims']= x.fit_transform(train[['Previous Claims']])\ntest['Previous Claims']= x.fit_transform(test[['Previous Claims']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.435322Z","iopub.execute_input":"2025-08-17T18:59:38.435691Z","iopub.status.idle":"2025-08-17T18:59:38.694528Z","shell.execute_reply.started":"2025-08-17T18:59:38.435662Z","shell.execute_reply":"2025-08-17T18:59:38.693391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Previous Claims'].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.695528Z","iopub.execute_input":"2025-08-17T18:59:38.695827Z","iopub.status.idle":"2025-08-17T18:59:38.703791Z","shell.execute_reply.started":"2025-08-17T18:59:38.695793Z","shell.execute_reply":"2025-08-17T18:59:38.702762Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Vehicle Age","metadata":{}},{"cell_type":"code","source":"train['Vehicle Age'].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.704817Z","iopub.execute_input":"2025-08-17T18:59:38.705138Z","iopub.status.idle":"2025-08-17T18:59:38.7246Z","shell.execute_reply.started":"2025-08-17T18:59:38.70511Z","shell.execute_reply":"2025-08-17T18:59:38.723594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = SimpleImputer(strategy = 'most_frequent')\ntrain['Vehicle Age']= x.fit_transform(train[['Vehicle Age']])\ntest['Vehicle Age']= x.fit_transform(test[['Vehicle Age']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.725765Z","iopub.execute_input":"2025-08-17T18:59:38.726182Z","iopub.status.idle":"2025-08-17T18:59:38.887901Z","shell.execute_reply.started":"2025-08-17T18:59:38.726143Z","shell.execute_reply":"2025-08-17T18:59:38.886755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Insurance Duration","metadata":{}},{"cell_type":"code","source":"x = SimpleImputer(strategy = 'most_frequent')\ntrain['Insurance Duration']= x.fit_transform(train[['Insurance Duration']])\ntest['Insurance Duration']= x.fit_transform(test[['Insurance Duration']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:38.88926Z","iopub.execute_input":"2025-08-17T18:59:38.889658Z","iopub.status.idle":"2025-08-17T18:59:39.03536Z","shell.execute_reply.started":"2025-08-17T18:59:38.889619Z","shell.execute_reply":"2025-08-17T18:59:39.034081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Insurance Duration'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:39.036447Z","iopub.execute_input":"2025-08-17T18:59:39.036756Z","iopub.status.idle":"2025-08-17T18:59:39.061305Z","shell.execute_reply.started":"2025-08-17T18:59:39.036726Z","shell.execute_reply":"2025-08-17T18:59:39.060305Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Credit Score Settlement","metadata":{}},{"cell_type":"code","source":"x = SimpleImputer(strategy = 'mean')\ntrain['Credit Score'] = x.fit_transform(train[['Credit Score']])\ntest['Credit Score'] = x.fit_transform(test[['Credit Score']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:39.062371Z","iopub.execute_input":"2025-08-17T18:59:39.062769Z","iopub.status.idle":"2025-08-17T18:59:39.141539Z","shell.execute_reply.started":"2025-08-17T18:59:39.062732Z","shell.execute_reply":"2025-08-17T18:59:39.14051Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Policy type Settlement","metadata":{}},{"cell_type":"code","source":"train['Policy Type'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:39.142528Z","iopub.execute_input":"2025-08-17T18:59:39.142816Z","iopub.status.idle":"2025-08-17T18:59:39.239412Z","shell.execute_reply.started":"2025-08-17T18:59:39.142791Z","shell.execute_reply":"2025-08-17T18:59:39.238246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = LabelEncoder()\ntrain['Policy Type']  = x.fit_transform(train['Policy Type'])\ntest['Policy Type']  = x.fit_transform(test['Policy Type'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:39.240422Z","iopub.execute_input":"2025-08-17T18:59:39.240802Z","iopub.status.idle":"2025-08-17T18:59:39.609021Z","shell.execute_reply.started":"2025-08-17T18:59:39.240765Z","shell.execute_reply":"2025-08-17T18:59:39.60798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:39.610014Z","iopub.execute_input":"2025-08-17T18:59:39.610318Z","iopub.status.idle":"2025-08-17T18:59:40.057517Z","shell.execute_reply.started":"2025-08-17T18:59:39.610293Z","shell.execute_reply":"2025-08-17T18:59:40.056304Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Policy Date Settlement","metadata":{}},{"cell_type":"code","source":"def date(df):\n    df['Policy Start Date'] = pd.to_datetime(df['Policy Start Date'])\n    df['Year'] = df['Policy Start Date'].dt.year\n    df['Day'] = df['Policy Start Date'].dt.day\n    df['Month'] = df['Policy Start Date'].dt.month\n    df['Month_name'] = df['Policy Start Date'].dt.month_name()\n    df['Day_of_week'] = df['Policy Start Date'].dt.day_name()\n    df['Week'] = df['Policy Start Date'].dt.isocalendar().week\n    df['Year_sin'] = np.sin(2 * np.pi * df['Year'])\n    df['Year_cos'] = np.cos(2 * np.pi * df['Year'])\n    df['Month_sin'] = np.sin(2 * np.pi * df['Month'] / 12) \n    df['Month_cos'] = np.cos(2 * np.pi * df['Month'] / 12)\n    df['Day_sin'] = np.sin(2 * np.pi * df['Day'] / 31)  \n    df['Day_cos'] = np.cos(2 * np.pi * df['Day'] / 31)\n    df['Group'] = (df['Year']-2020)*48 + df['Month']*4 + df['Day']//7\n    df.drop('Policy Start Date', axis=1, inplace=True)\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:40.05866Z","iopub.execute_input":"2025-08-17T18:59:40.059021Z","iopub.status.idle":"2025-08-17T18:59:40.069361Z","shell.execute_reply.started":"2025-08-17T18:59:40.058982Z","shell.execute_reply":"2025-08-17T18:59:40.068112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"date(train)\ndate(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:40.071087Z","iopub.execute_input":"2025-08-17T18:59:40.071385Z","iopub.status.idle":"2025-08-17T18:59:43.965711Z","shell.execute_reply.started":"2025-08-17T18:59:40.071359Z","shell.execute_reply":"2025-08-17T18:59:43.964543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:43.967051Z","iopub.execute_input":"2025-08-17T18:59:43.967494Z","iopub.status.idle":"2025-08-17T18:59:44.473918Z","shell.execute_reply.started":"2025-08-17T18:59:43.967456Z","shell.execute_reply":"2025-08-17T18:59:44.472671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:44.475003Z","iopub.execute_input":"2025-08-17T18:59:44.475373Z","iopub.status.idle":"2025-08-17T18:59:44.798653Z","shell.execute_reply.started":"2025-08-17T18:59:44.475344Z","shell.execute_reply":"2025-08-17T18:59:44.797503Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Modelling using XGB","metadata":{}},{"cell_type":"code","source":"x = train.drop(columns = ['id','Gender','Location','Customer Feedback','Smoking Status','Exercise Frequency','Property Type','Month_name','Day_of_week'],axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:44.799706Z","iopub.execute_input":"2025-08-17T18:59:44.800102Z","iopub.status.idle":"2025-08-17T18:59:44.914141Z","shell.execute_reply.started":"2025-08-17T18:59:44.800065Z","shell.execute_reply":"2025-08-17T18:59:44.913017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y= test.drop(columns = ['id','Gender','Location','Customer Feedback','Smoking Status','Exercise Frequency','Property Type','Month_name','Day_of_week'],axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:44.915184Z","iopub.execute_input":"2025-08-17T18:59:44.915583Z","iopub.status.idle":"2025-08-17T18:59:45.018756Z","shell.execute_reply.started":"2025-08-17T18:59:44.915532Z","shell.execute_reply":"2025-08-17T18:59:45.017608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = x.drop('Premium Amount',axis=1)\nY = x['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:45.01989Z","iopub.execute_input":"2025-08-17T18:59:45.02033Z","iopub.status.idle":"2025-08-17T18:59:45.187826Z","shell.execute_reply.started":"2025-08-17T18:59:45.020286Z","shell.execute_reply":"2025-08-17T18:59:45.186692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(X,Y,test_size=0.25,random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:45.194378Z","iopub.execute_input":"2025-08-17T18:59:45.194797Z","iopub.status.idle":"2025-08-17T18:59:45.867759Z","shell.execute_reply.started":"2025-08-17T18:59:45.19476Z","shell.execute_reply":"2025-08-17T18:59:45.866714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_error\n\n# best_params = study.best_params\nbest_model = XGBRegressor()\nbest_model.fit(x_train, y_train)\n\n# Now, use the model for predictions (e.g., on the test set)\ny_pred = best_model.predict(x_test)\n\n# Evaluate the model performance (e.g., RMSE or any other metric)\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\nprint(\"RMSE on Test Set:\", rmse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:45.869185Z","iopub.execute_input":"2025-08-17T18:59:45.869567Z","iopub.status.idle":"2025-08-17T18:59:51.578149Z","shell.execute_reply.started":"2025-08-17T18:59:45.869527Z","shell.execute_reply":"2025-08-17T18:59:51.577262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T19:02:27.277448Z","iopub.execute_input":"2025-08-17T19:02:27.277863Z","iopub.status.idle":"2025-08-17T19:02:27.283842Z","shell.execute_reply.started":"2025-08-17T19:02:27.277824Z","shell.execute_reply":"2025-08-17T19:02:27.282699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test[y_test.values<0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T19:02:46.289391Z","iopub.execute_input":"2025-08-17T19:02:46.289881Z","iopub.status.idle":"2025-08-17T19:02:46.300043Z","shell.execute_reply.started":"2025-08-17T19:02:46.289834Z","shell.execute_reply":"2025-08-17T19:02:46.298676Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport numpy as np\n\n# Shift so all values are >= 0\nshift = -min(np.min(y_test), np.min(y_pred), 0)\ny_test_shifted = y_test + shift\ny_pred_shifted = y_pred + shift\n\nfrom sklearn.metrics import mean_squared_log_error\nrmsle_score = np.sqrt(mean_squared_log_error(y_test_shifted, y_pred_shifted))\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T19:03:25.325784Z","iopub.execute_input":"2025-08-17T19:03:25.326166Z","iopub.status.idle":"2025-08-17T19:03:25.347458Z","shell.execute_reply.started":"2025-08-17T19:03:25.326134Z","shell.execute_reply":"2025-08-17T19:03:25.346421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rmsle_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T19:03:36.997582Z","iopub.execute_input":"2025-08-17T19:03:36.997969Z","iopub.status.idle":"2025-08-17T19:03:37.003456Z","shell.execute_reply.started":"2025-08-17T19:03:36.997914Z","shell.execute_reply":"2025-08-17T19:03:37.002465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.tail(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:51.579197Z","iopub.execute_input":"2025-08-17T18:59:51.579529Z","iopub.status.idle":"2025-08-17T18:59:51.58829Z","shell.execute_reply.started":"2025-08-17T18:59:51.579501Z","shell.execute_reply":"2025-08-17T18:59:51.587295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:51.589297Z","iopub.execute_input":"2025-08-17T18:59:51.589695Z","iopub.status.idle":"2025-08-17T18:59:51.603992Z","shell.execute_reply.started":"2025-08-17T18:59:51.589666Z","shell.execute_reply":"2025-08-17T18:59:51.602995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred2 = best_model.predict(y)\ny_pred2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:51.605028Z","iopub.execute_input":"2025-08-17T18:59:51.605397Z","iopub.status.idle":"2025-08-17T18:59:52.467478Z","shell.execute_reply.started":"2025-08-17T18:59:51.605359Z","shell.execute_reply":"2025-08-17T18:59:52.466278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"l = []\nfor i in range (1200000,2000000):\n    l.append(i)\ndata = {\n    'id': l,\n    'Premium Amount': y_pred2\n}\nsubmission_df = pd.DataFrame(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:52.468504Z","iopub.execute_input":"2025-08-17T18:59:52.468961Z","iopub.status.idle":"2025-08-17T18:59:52.766276Z","shell.execute_reply.started":"2025-08-17T18:59:52.468906Z","shell.execute_reply":"2025-08-17T18:59:52.765065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv('Submission.csv',index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-17T18:59:52.767365Z","iopub.execute_input":"2025-08-17T18:59:52.767818Z","iopub.status.idle":"2025-08-17T18:59:53.951132Z","shell.execute_reply.started":"2025-08-17T18:59:52.767774Z","shell.execute_reply":"2025-08-17T18:59:53.949881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}