{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"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":"2024-12-13T16:11:57.185728Z","iopub.execute_input":"2024-12-13T16:11:57.186191Z","iopub.status.idle":"2024-12-13T16:11:58.485925Z","shell.execute_reply.started":"2024-12-13T16:11:57.186151Z","shell.execute_reply":"2024-12-13T16:11:58.484561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import MinMaxScaler, OneHotEncoder, LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom catboost import CatBoostRegressor\nfrom sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:11:58.487772Z","iopub.execute_input":"2024-12-13T16:11:58.488355Z","iopub.status.idle":"2024-12-13T16:12:01.123245Z","shell.execute_reply.started":"2024-12-13T16:11:58.488304Z","shell.execute_reply":"2024-12-13T16:12:01.122043Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\n\ntest_data = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:01.125087Z","iopub.execute_input":"2024-12-13T16:12:01.125599Z","iopub.status.idle":"2024-12-13T16:12:11.218129Z","shell.execute_reply.started":"2024-12-13T16:12:01.125559Z","shell.execute_reply":"2024-12-13T16:12:11.21707Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:11.220332Z","iopub.execute_input":"2024-12-13T16:12:11.220683Z","iopub.status.idle":"2024-12-13T16:12:11.263931Z","shell.execute_reply.started":"2024-12-13T16:12:11.220648Z","shell.execute_reply":"2024-12-13T16:12:11.262616Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.isnull().sum()* 100 /len(train_data)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:11.265286Z","iopub.execute_input":"2024-12-13T16:12:11.265659Z","iopub.status.idle":"2024-12-13T16:12:11.908687Z","shell.execute_reply.started":"2024-12-13T16:12:11.265624Z","shell.execute_reply":"2024-12-13T16:12:11.907363Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.isnull().sum() / len(test_data) * 100","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:11.910118Z","iopub.execute_input":"2024-12-13T16:12:11.910544Z","iopub.status.idle":"2024-12-13T16:12:12.338145Z","shell.execute_reply.started":"2024-12-13T16:12:11.910497Z","shell.execute_reply":"2024-12-13T16:12:12.336817Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## EDA","metadata":{"execution":{"iopub.status.busy":"2024-12-08T14:19:58.637177Z","iopub.execute_input":"2024-12-08T14:19:58.637673Z","iopub.status.idle":"2024-12-08T14:19:58.642838Z","shell.execute_reply.started":"2024-12-08T14:19:58.637623Z","shell.execute_reply":"2024-12-08T14:19:58.641788Z"}}},{"cell_type":"code","source":"plt.hist(train_data['Annual Income'], bins = 5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:12.339885Z","iopub.execute_input":"2024-12-13T16:12:12.340317Z","iopub.status.idle":"2024-12-13T16:12:12.608836Z","shell.execute_reply.started":"2024-12-13T16:12:12.340273Z","shell.execute_reply":"2024-12-13T16:12:12.607624Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.scatter(train_data['Premium Amount'], train_data['Annual Income'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:12.610453Z","iopub.execute_input":"2024-12-13T16:12:12.610795Z","iopub.status.idle":"2024-12-13T16:12:15.342695Z","shell.execute_reply.started":"2024-12-13T16:12:12.610734Z","shell.execute_reply":"2024-12-13T16:12:15.341654Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.barplot(data = train_data, x = 'Gender', y= 'Annual Income')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:15.34417Z","iopub.execute_input":"2024-12-13T16:12:15.344611Z","iopub.status.idle":"2024-12-13T16:12:37.063907Z","shell.execute_reply.started":"2024-12-13T16:12:15.344564Z","shell.execute_reply":"2024-12-13T16:12:37.06264Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(data=train_data, x='Gender', hue='Marital Status')\nplt.title(\"Count of Gender with Marital Status\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:37.067649Z","iopub.execute_input":"2024-12-13T16:12:37.068368Z","iopub.status.idle":"2024-12-13T16:12:38.629022Z","shell.execute_reply.started":"2024-12-13T16:12:37.06833Z","shell.execute_reply":"2024-12-13T16:12:38.627666Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(data = train_data, x= 'Age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:38.630736Z","iopub.execute_input":"2024-12-13T16:12:38.631827Z","iopub.status.idle":"2024-12-13T16:12:38.791067Z","shell.execute_reply.started":"2024-12-13T16:12:38.631731Z","shell.execute_reply":"2024-12-13T16:12:38.789921Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.lineplot(data = train_data, x = 'Occupation', y = 'Number of Dependents', hue = 'Gender')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:38.793231Z","iopub.execute_input":"2024-12-13T16:12:38.793577Z","iopub.status.idle":"2024-12-13T16:12:46.842073Z","shell.execute_reply.started":"2024-12-13T16:12:38.793541Z","shell.execute_reply":"2024-12-13T16:12:46.840857Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:46.843884Z","iopub.execute_input":"2024-12-13T16:12:46.84425Z","iopub.status.idle":"2024-12-13T16:12:47.272276Z","shell.execute_reply.started":"2024-12-13T16:12:46.844216Z","shell.execute_reply":"2024-12-13T16:12:47.271234Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data['Marital Status'] = test_data['Marital Status'].str.strip().str.lower()\ntrain_data['Marital Status'] = train_data['Marital Status'].str.strip().str.lower()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:12:47.273608Z","iopub.execute_input":"2024-12-13T16:12:47.274057Z","iopub.status.idle":"2024-12-13T16:12:47.968674Z","shell.execute_reply.started":"2024-12-13T16:12:47.274011Z","shell.execute_reply":"2024-12-13T16:12:47.967714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[(train_data['Education Level'] == 'High School') & (train_data['Occupation'].isna()), 'Occupation'] = 'Unemployed'\ntest_data.loc[(test_data['Education Level'] == 'High School') & (test_data['Occupation'].isna()), 'Occupation'] = 'Unemployed'","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:47.969923Z","iopub.execute_input":"2024-12-13T16:12:47.970214Z","iopub.status.idle":"2024-12-13T16:12:48.24012Z","shell.execute_reply.started":"2024-12-13T16:12:47.970185Z","shell.execute_reply":"2024-12-13T16:12:48.23895Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[((train_data['Age'] > 50) | (train_data['Age'] < 21)) & train_data['Occupation'].isna(), 'Occupation'] = 'Unemployed'\ntest_data.loc[((test_data['Age'] > 50) | (test_data['Age'] < 21)) & test_data['Occupation'].isna(), 'Occupation'] = 'Unemployed'","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.241485Z","iopub.execute_input":"2024-12-13T16:12:48.241857Z","iopub.status.idle":"2024-12-13T16:12:48.362922Z","shell.execute_reply.started":"2024-12-13T16:12:48.241819Z","shell.execute_reply":"2024-12-13T16:12:48.361892Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Occupation'] = train_data['Occupation'].fillna('Unemployed')\ntest_data['Occupation'] = test_data['Occupation'].fillna('Unemployed')","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.364337Z","iopub.execute_input":"2024-12-13T16:12:48.364797Z","iopub.status.idle":"2024-12-13T16:12:48.505544Z","shell.execute_reply.started":"2024-12-13T16:12:48.364724Z","shell.execute_reply":"2024-12-13T16:12:48.504292Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[(train_data['Previous Claims'].isnull()) & (train_data['Occupation'] == 'Unemployed'), 'Previous Claims'] = 0.0\ntest_data.loc[(test_data['Previous Claims'].isnull()) & (test_data['Occupation'] == 'Unemployed'), 'Previous Claims'] = 0.0","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.506923Z","iopub.execute_input":"2024-12-13T16:12:48.507268Z","iopub.status.idle":"2024-12-13T16:12:48.692746Z","shell.execute_reply.started":"2024-12-13T16:12:48.507235Z","shell.execute_reply":"2024-12-13T16:12:48.691732Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[(train_data['Previous Claims'].isnull()) & (train_data['Annual Income'] > 50000), 'Previous Claims'] = 0.0\ntest_data.loc[(test_data['Previous Claims'].isnull()) & (test_data['Annual Income'] > 50000), 'Previous Claims'] = 0.0","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.694178Z","iopub.execute_input":"2024-12-13T16:12:48.694601Z","iopub.status.idle":"2024-12-13T16:12:48.712167Z","shell.execute_reply.started":"2024-12-13T16:12:48.694557Z","shell.execute_reply":"2024-12-13T16:12:48.711083Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[(train_data['Previous Claims'].isnull()) & (train_data['Education Level'] ==  'High School'), 'Previous Claims'] = 0.0\ntest_data.loc[(test_data['Previous Claims'].isnull()) & (test_data['Education Level'] ==  'High School'), 'Previous Claims'] = 0.0","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.71342Z","iopub.execute_input":"2024-12-13T16:12:48.713748Z","iopub.status.idle":"2024-12-13T16:12:48.885488Z","shell.execute_reply.started":"2024-12-13T16:12:48.713713Z","shell.execute_reply":"2024-12-13T16:12:48.884316Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Previous Claims'] = train_data['Previous Claims'].fillna(np.random.randint(0.0, 9.0))\ntest_data['Previous Claims'] = test_data['Previous Claims'].fillna(np.random.randint(0.0, 9.0))","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.88673Z","iopub.execute_input":"2024-12-13T16:12:48.887059Z","iopub.status.idle":"2024-12-13T16:12:48.90881Z","shell.execute_reply.started":"2024-12-13T16:12:48.887029Z","shell.execute_reply":"2024-12-13T16:12:48.907683Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[(train_data['Number of Dependents'].isnull()) & (train_data['Marital Status'] == 'Single'),'Number of Dependents']= 0.0\ntest_data.loc[(test_data['Number of Dependents'].isnull()) & (test_data['Marital Status'] == 'Single'),'Number of Dependents']= 0.0","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:48.910585Z","iopub.execute_input":"2024-12-13T16:12:48.910937Z","iopub.status.idle":"2024-12-13T16:12:49.084284Z","shell.execute_reply.started":"2024-12-13T16:12:48.910903Z","shell.execute_reply":"2024-12-13T16:12:49.083106Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Number of Dependents'] = train_data['Number of Dependents'].apply(lambda x: np.random.uniform(0.0, 5.0) if pd.isna(x) else x\n)\n\ntest_data['Number of Dependents'] = test_data['Number of Dependents'].apply(lambda x: np.random.uniform(0.0, 5.0) if pd.isna(x) else x\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:49.085677Z","iopub.execute_input":"2024-12-13T16:12:49.086075Z","iopub.status.idle":"2024-12-13T16:12:50.976059Z","shell.execute_reply.started":"2024-12-13T16:12:49.08604Z","shell.execute_reply":"2024-12-13T16:12:50.974927Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Annual Income'] = train_data['Annual Income'].fillna(np.mean(train_data['Annual Income']))\n\ntest_data['Annual Income'] = test_data['Annual Income'].fillna(np.mean(test_data['Annual Income']))","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:50.977675Z","iopub.execute_input":"2024-12-13T16:12:50.978044Z","iopub.status.idle":"2024-12-13T16:12:51.004566Z","shell.execute_reply.started":"2024-12-13T16:12:50.977996Z","shell.execute_reply":"2024-12-13T16:12:51.003312Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"total_feedback = 377905 + 375518 + 368753\n\nprobabilities = {\n    'Average': 377905 / total_feedback,\n    'Poor': 375518 / total_feedback,\n    'Good': 368753 / total_feedback\n}","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:51.006111Z","iopub.execute_input":"2024-12-13T16:12:51.006861Z","iopub.status.idle":"2024-12-13T16:12:51.01221Z","shell.execute_reply.started":"2024-12-13T16:12:51.006811Z","shell.execute_reply":"2024-12-13T16:12:51.011033Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Customer Feedback'] = train_data['Customer Feedback'].apply(\n    lambda x: np.random.choice(['Average', 'Poor', 'Good'], p=[probabilities['Average'], probabilities['Poor'], probabilities['Good']]) \n    if pd.isna(x) else x\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:51.013489Z","iopub.execute_input":"2024-12-13T16:12:51.014505Z","iopub.status.idle":"2024-12-13T16:12:53.43557Z","shell.execute_reply.started":"2024-12-13T16:12:51.014466Z","shell.execute_reply":"2024-12-13T16:12:53.4345Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"feedback_count = 251217 + 250434 + 246073\nprop = probabilities = {\n    'Average': 251217 / feedback_count,\n    'Poor': 250434 / feedback_count,\n    'Good': 246073 / feedback_count\n}","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:53.436786Z","iopub.execute_input":"2024-12-13T16:12:53.437105Z","iopub.status.idle":"2024-12-13T16:12:53.44254Z","shell.execute_reply.started":"2024-12-13T16:12:53.437075Z","shell.execute_reply":"2024-12-13T16:12:53.441339Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data['Customer Feedback'] = test_data['Customer Feedback'].apply(\n    lambda x: np.random.choice(['Average', 'Poor', 'Good'], p=[probabilities['Average'], probabilities['Poor'], probabilities['Good']]) \n    if pd.isna(x) else x\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:53.449068Z","iopub.execute_input":"2024-12-13T16:12:53.449471Z","iopub.status.idle":"2024-12-13T16:12:55.165819Z","shell.execute_reply.started":"2024-12-13T16:12:53.449437Z","shell.execute_reply":"2024-12-13T16:12:55.164678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Credit Score'] = train_data['Credit Score'].fillna(train_data['Credit Score'].mean()) \n\ntest_data['Credit Score'] = test_data['Credit Score'].fillna(test_data['Credit Score'].mean()) ","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:55.167159Z","iopub.execute_input":"2024-12-13T16:12:55.167583Z","iopub.status.idle":"2024-12-13T16:12:55.198485Z","shell.execute_reply.started":"2024-12-13T16:12:55.167539Z","shell.execute_reply":"2024-12-13T16:12:55.197254Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"min_val = train_data['Health Score'].min()\nmax_val = train_data['Health Score'].max()\ntrain_data['Health Score'] = train_data['Health Score'].apply(lambda x: np.random.uniform(min_val, max_val) if pd.isnull(x) else x)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:55.199789Z","iopub.execute_input":"2024-12-13T16:12:55.20013Z","iopub.status.idle":"2024-12-13T16:12:56.308048Z","shell.execute_reply.started":"2024-12-13T16:12:55.200095Z","shell.execute_reply":"2024-12-13T16:12:56.307183Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"min_val_test = test_data['Health Score'].min()\nmax_val_test = test_data['Health Score'].max()\ntest_data['Health Score'] = test_data['Health Score'].apply(lambda x: np.random.uniform(min_val_test, max_val_test) if pd.isnull(x) else x)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:56.309369Z","iopub.execute_input":"2024-12-13T16:12:56.309825Z","iopub.status.idle":"2024-12-13T16:12:57.016429Z","shell.execute_reply.started":"2024-12-13T16:12:56.309777Z","shell.execute_reply":"2024-12-13T16:12:57.015149Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":" train_data['Age'] = train_data['Age'].apply(lambda x: np.random.uniform(18.0, 64.0) if pd.isnull(x) else x)\n\n test_data['Age'] = test_data['Age'].apply(lambda x: np.random.uniform(18.0, 64.0) if pd.isnull(x) else x)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:57.01791Z","iopub.execute_input":"2024-12-13T16:12:57.018358Z","iopub.status.idle":"2024-12-13T16:12:58.532251Z","shell.execute_reply.started":"2024-12-13T16:12:57.018311Z","shell.execute_reply":"2024-12-13T16:12:58.531032Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Marital Status'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:12:58.533515Z","iopub.execute_input":"2024-12-13T16:12:58.533892Z","iopub.status.idle":"2024-12-13T16:12:58.665568Z","shell.execute_reply.started":"2024-12-13T16:12:58.533853Z","shell.execute_reply":"2024-12-13T16:12:58.664246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Marital Status'].mode()[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:12:58.667211Z","iopub.execute_input":"2024-12-13T16:12:58.667551Z","iopub.status.idle":"2024-12-13T16:12:58.773144Z","shell.execute_reply.started":"2024-12-13T16:12:58.667519Z","shell.execute_reply":"2024-12-13T16:12:58.771783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Marital Status'] = train_data['Marital Status'].fillna(train_data['Marital Status'].mode()[0])\n\ntest_data['Marital Status'] = test_data['Marital Status'].fillna(test_data['Marital Status'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:58.774376Z","iopub.execute_input":"2024-12-13T16:12:58.774675Z","iopub.status.idle":"2024-12-13T16:12:59.15215Z","shell.execute_reply.started":"2024-12-13T16:12:58.774646Z","shell.execute_reply":"2024-12-13T16:12:59.150841Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":" train_data['Insurance Duration'] = train_data['Insurance Duration'].fillna(0)\ntest_data['Insurance Duration'] = test_data['Insurance Duration'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2024-12-13T16:12:59.153619Z","iopub.execute_input":"2024-12-13T16:12:59.154386Z","iopub.status.idle":"2024-12-13T16:12:59.17106Z","shell.execute_reply.started":"2024-12-13T16:12:59.15435Z","shell.execute_reply":"2024-12-13T16:12:59.169791Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":" test_data['Vehicle Age'] = test_data['Vehicle Age'].apply(lambda x: np.random.uniform(0.0, 11.0) if pd.isnull(x) else x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:12:59.172447Z","iopub.execute_input":"2024-12-13T16:12:59.172987Z","iopub.status.idle":"2024-12-13T16:12:59.755503Z","shell.execute_reply.started":"2024-12-13T16:12:59.172937Z","shell.execute_reply":"2024-12-13T16:12:59.75439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:12:59.756909Z","iopub.execute_input":"2024-12-13T16:12:59.757354Z","iopub.status.idle":"2024-12-13T16:12:59.786276Z","shell.execute_reply.started":"2024-12-13T16:12:59.757308Z","shell.execute_reply":"2024-12-13T16:12:59.785193Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Policy Start Date'] = pd.to_datetime(train_data['Policy Start Date'])\ntrain_data['date'] = train_data['Policy Start Date'].dt.date\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:12:59.787679Z","iopub.execute_input":"2024-12-13T16:12:59.788135Z","iopub.status.idle":"2024-12-13T16:13:00.506073Z","shell.execute_reply.started":"2024-12-13T16:12:59.788087Z","shell.execute_reply":"2024-12-13T16:13:00.505002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = train_data.drop(\"Policy Start Date\", axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:00.507553Z","iopub.execute_input":"2024-12-13T16:13:00.50802Z","iopub.status.idle":"2024-12-13T16:13:00.74553Z","shell.execute_reply.started":"2024-12-13T16:13:00.507961Z","shell.execute_reply":"2024-12-13T16:13:00.744307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data['Policy Start Date'] = pd.to_datetime(test_data['Policy Start Date'])\ntest_data['date'] = test_data['Policy Start Date'].dt.date\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:00.746895Z","iopub.execute_input":"2024-12-13T16:13:00.747212Z","iopub.status.idle":"2024-12-13T16:13:01.235599Z","shell.execute_reply.started":"2024-12-13T16:13:00.747182Z","shell.execute_reply":"2024-12-13T16:13:01.234344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = test_data.drop(\"Policy Start Date\", axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:01.237019Z","iopub.execute_input":"2024-12-13T16:13:01.23734Z","iopub.status.idle":"2024-12-13T16:13:01.441817Z","shell.execute_reply.started":"2024-12-13T16:13:01.23731Z","shell.execute_reply":"2024-12-13T16:13:01.440517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['Gender'] = train_data['Gender'].replace({\"Female\": 1, \"Male\" : 0})\ntrain_data['Smoking Status'] = train_data['Smoking Status'].replace({\"Yes\" : 1, \"No\": 0})\ntest_data['Gender'] = test_data['Gender'].replace({\"Female\": 1, \"Male\" : 0})\ntest_data['Smoking Status'] = test_data['Smoking Status'].replace({\"Yes\" : 1, \"No\": 0})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:01.443105Z","iopub.execute_input":"2024-12-13T16:13:01.443438Z","iopub.status.idle":"2024-12-13T16:13:02.895387Z","shell.execute_reply.started":"2024-12-13T16:13:01.443405Z","shell.execute_reply":"2024-12-13T16:13:02.894261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" train_data['Vehicle Age'] = train_data['Vehicle Age'].fillna(0.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:02.896792Z","iopub.execute_input":"2024-12-13T16:13:02.89723Z","iopub.status.idle":"2024-12-13T16:13:02.909226Z","shell.execute_reply.started":"2024-12-13T16:13:02.897182Z","shell.execute_reply":"2024-12-13T16:13:02.908113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lb = LabelEncoder()\ncat_cols = []  \n\nfor col in train_data.columns:\n    if train_data[col].dtype == 'object':  \n        cat_cols.append(col)  \n        train_data[col] = lb.fit_transform(train_data[col])\n        test_data[col] = lb.transform(test_data[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:02.910695Z","iopub.execute_input":"2024-12-13T16:13:02.911203Z","iopub.status.idle":"2024-12-13T16:13:06.222436Z","shell.execute_reply.started":"2024-12-13T16:13:02.911153Z","shell.execute_reply":"2024-12-13T16:13:06.221273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ip = train_data.copy()\nip = ip.drop(\"Premium Amount\", axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:06.224095Z","iopub.execute_input":"2024-12-13T16:13:06.224564Z","iopub.status.idle":"2024-12-13T16:13:06.528186Z","shell.execute_reply.started":"2024-12-13T16:13:06.224513Z","shell.execute_reply":"2024-12-13T16:13:06.527096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sc = MinMaxScaler()\n  \nfor col in ip.columns:\n    if ip[col].dtype == 'int' or ip[col].dtype == 'float':  \n         \n        ip[col] = sc.fit_transform(ip[[col]])\n        test_data[col] = sc.transform(test_data[[col]])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:06.529688Z","iopub.execute_input":"2024-12-13T16:13:06.530185Z","iopub.status.idle":"2024-12-13T16:13:07.034407Z","shell.execute_reply.started":"2024-12-13T16:13:06.530135Z","shell.execute_reply":"2024-12-13T16:13:07.033303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(ip, train_data['Premium Amount'], test_size = 0.2, random_state = 45)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:07.03589Z","iopub.execute_input":"2024-12-13T16:13:07.036321Z","iopub.status.idle":"2024-12-13T16:13:07.573255Z","shell.execute_reply.started":"2024-12-13T16:13:07.036273Z","shell.execute_reply":"2024-12-13T16:13:07.572106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def calculate_rmlse(y_true, y_pred):\n    log_true = np.log(y_true + 1)\n    log_pred = np.log(y_pred + 1)\n    rmlse = np.sqrt(np.mean((log_true - log_pred) ** 2))\n    return rmlse","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:08.966165Z","iopub.execute_input":"2024-12-13T16:13:08.968181Z","iopub.status.idle":"2024-12-13T16:13:08.977289Z","shell.execute_reply.started":"2024-12-13T16:13:08.968129Z","shell.execute_reply":"2024-12-13T16:13:08.97429Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rt = RandomForestRegressor(n_estimators=30,max_depth= 20,max_samples= 20,min_samples_leaf= 15,max_features=25)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:31.821507Z","iopub.execute_input":"2024-12-13T16:13:31.821864Z","iopub.status.idle":"2024-12-13T16:13:31.826977Z","shell.execute_reply.started":"2024-12-13T16:13:31.821829Z","shell.execute_reply":"2024-12-13T16:13:31.825847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rt.fit(X_train, Y_train)\nrt_pred = rt.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:31.828309Z","iopub.execute_input":"2024-12-13T16:13:31.828641Z","iopub.status.idle":"2024-12-13T16:13:32.215938Z","shell.execute_reply.started":"2024-12-13T16:13:31.828608Z","shell.execute_reply":"2024-12-13T16:13:32.21485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_test_data_rt = rt.predict(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:32.217203Z","iopub.execute_input":"2024-12-13T16:13:32.217529Z","iopub.status.idle":"2024-12-13T16:13:32.413062Z","shell.execute_reply.started":"2024-12-13T16:13:32.217499Z","shell.execute_reply":"2024-12-13T16:13:32.411877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"calculate_rmlse(Y_test, rt_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:13:32.414356Z","iopub.execute_input":"2024-12-13T16:13:32.414652Z","iopub.status.idle":"2024-12-13T16:13:32.42808Z","shell.execute_reply.started":"2024-12-13T16:13:32.414623Z","shell.execute_reply":"2024-12-13T16:13:32.426653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_data = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:16:16.169475Z","iopub.execute_input":"2024-12-13T16:16:16.169923Z","iopub.status.idle":"2024-12-13T16:16:16.44281Z","shell.execute_reply.started":"2024-12-13T16:16:16.169886Z","shell.execute_reply":"2024-12-13T16:16:16.441389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Assuming y_pred_test_data_rt is an array or Series containing predictions\ntest_data_pred = pd.DataFrame({\n    'id': sample_data['id'],\n    'Premium Amount': y_pred_test_data_rt\n})\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:16:19.190256Z","iopub.execute_input":"2024-12-13T16:16:19.190621Z","iopub.status.idle":"2024-12-13T16:16:19.198625Z","shell.execute_reply.started":"2024-12-13T16:16:19.190589Z","shell.execute_reply":"2024-12-13T16:16:19.197516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:16:22.050091Z","iopub.execute_input":"2024-12-13T16:16:22.050452Z","iopub.status.idle":"2024-12-13T16:16:22.062429Z","shell.execute_reply.started":"2024-12-13T16:16:22.05042Z","shell.execute_reply":"2024-12-13T16:16:22.061391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = test_data_pred.to_csv(\"sample_submission_data.csv\", index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:16:26.756086Z","iopub.execute_input":"2024-12-13T16:16:26.756467Z","iopub.status.idle":"2024-12-13T16:16:28.452165Z","shell.execute_reply.started":"2024-12-13T16:16:26.756433Z","shell.execute_reply":"2024-12-13T16:16:28.450903Z"}},"outputs":[],"execution_count":null}]}