{"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":30839,"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-01-30T14:12:12.978036Z","iopub.execute_input":"2025-01-30T14:12:12.978428Z","iopub.status.idle":"2025-01-30T14:12:12.990056Z","shell.execute_reply.started":"2025-01-30T14:12:12.978395Z","shell.execute_reply":"2025-01-30T14:12:12.988802Z"}},"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-01-30T14:12:12.991677Z","iopub.execute_input":"2025-01-30T14:12:12.992221Z","iopub.status.idle":"2025-01-30T14:12:21.228325Z","shell.execute_reply.started":"2025-01-30T14:12:12.992175Z","shell.execute_reply":"2025-01-30T14:12:21.226542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Previous Claims'] = train['Previous Claims'].fillna(0)\ntrain['Marital Status'] = train['Marital Status'].fillna(0)\ntrain['Customer Feedback'] = train['Customer Feedback'].fillna(0)\ntest['Previous Claims'] = test['Previous Claims'].fillna(0)\ntest['Marital Status'] = test['Marital Status'].fillna(0)\ntest['Customer Feedback'] = test['Customer Feedback'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:21.229943Z","iopub.execute_input":"2025-01-30T14:12:21.230272Z","iopub.status.idle":"2025-01-30T14:12:21.543515Z","shell.execute_reply.started":"2025-01-30T14:12:21.230242Z","shell.execute_reply":"2025-01-30T14:12:21.542342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"float_feature = ['Age','Annual Income','Number of Dependents','Health Score','Vehicle Age','Credit Score','Insurance Duration']\nfor feature in float_feature:\n    train[feature] = train[feature].fillna(train[feature].mean())\n    test[feature] = test[feature].fillna(train[feature].mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:21.545372Z","iopub.execute_input":"2025-01-30T14:12:21.545797Z","iopub.status.idle":"2025-01-30T14:12:21.833948Z","shell.execute_reply.started":"2025-01-30T14:12:21.545765Z","shell.execute_reply":"2025-01-30T14:12:21.832368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:21.835057Z","iopub.execute_input":"2025-01-30T14:12:21.835359Z","iopub.status.idle":"2025-01-30T14:12:22.608078Z","shell.execute_reply.started":"2025-01-30T14:12:21.835335Z","shell.execute_reply":"2025-01-30T14:12:22.606943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Marital Status'] = train['Marital Status'].map({'Married':1,'Divorced':-2,'Single':-1})\ntest['Marital Status'] = test['Marital Status'].map({'Married':1,'Divorced':-2,'Single':-1})\ntrain['Gender'] = train['Gender'].map({'Female':1,'Male':-1})\ntest['Gender'] = test['Gender'].map({'Female':1,'Male':-1})\ntrain['Smoking Status'] = train['Smoking Status'].map({'Yes':1,'No':-1})\ntest['Smoking Status'] = test['Smoking Status'].map({'Yes':1,'No':-1})\ntrain['Customer Feedback'] = train['Customer Feedback'].map({'Good':1,'Average':0,'Poor':-1})\ntest['Customer Feedback'] = test['Customer Feedback'].map({'Good':1,'Average':0,'Poor':-1})\ntrain['Policy Type'] = train['Policy Type'].map({'Premium':3, 'Comprehensive':2, 'Basic':1})\ntest['Policy Type'] = test['Policy Type'].map({'Premium':3, 'Comprehensive':2, 'Basic':1})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:22.609476Z","iopub.execute_input":"2025-01-30T14:12:22.609897Z","iopub.status.idle":"2025-01-30T14:12:23.325985Z","shell.execute_reply.started":"2025-01-30T14:12:22.609852Z","shell.execute_reply":"2025-01-30T14:12:23.324859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Policy Start Date'] = pd.to_datetime(test['Policy Start Date']).dt.strftime('%Y')\ntrain['Policy Start Date'] = pd.to_datetime(train['Policy Start Date']).dt.strftime('%Y')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:23.330085Z","iopub.execute_input":"2025-01-30T14:12:23.330475Z","iopub.status.idle":"2025-01-30T14:12:34.314134Z","shell.execute_reply.started":"2025-01-30T14:12:23.330444Z","shell.execute_reply":"2025-01-30T14:12:34.311812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dm1 = pd.get_dummies(train['Education Level'])\ndm2 = pd.get_dummies(train['Occupation'])\ndm3 = pd.get_dummies(train['Location'])\ndm4 = pd.get_dummies(train['Exercise Frequency'])\ndm5 = pd.get_dummies(train['Property Type'])\ntdm1 = pd.get_dummies(test['Education Level'])\ntdm2 = pd.get_dummies(test['Occupation'])\ntdm3 = pd.get_dummies(test['Location'])\ntdm4 = pd.get_dummies(test['Exercise Frequency'])\ntdm5 = pd.get_dummies(test['Property Type'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:34.317315Z","iopub.execute_input":"2025-01-30T14:12:34.317744Z","iopub.status.idle":"2025-01-30T14:12:35.174914Z","shell.execute_reply.started":"2025-01-30T14:12:34.317698Z","shell.execute_reply":"2025-01-30T14:12:35.173507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.concat([train,dm1,dm2,dm3,dm4,dm5],axis=1)\ntest = pd.concat([test,tdm1,tdm2,tdm3,tdm4,tdm5],axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:35.176367Z","iopub.execute_input":"2025-01-30T14:12:35.176741Z","iopub.status.idle":"2025-01-30T14:12:36.118685Z","shell.execute_reply.started":"2025-01-30T14:12:35.176712Z","shell.execute_reply":"2025-01-30T14:12:36.11712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.drop(['Education Level','Occupation','Location','id','Exercise Frequency','Property Type'],axis = 1)\ntest = test.drop(['Education Level','Occupation','Location','id','Exercise Frequency','Property Type'],axis = 1)\nx = train.drop(['Premium Amount'],axis = 1)\ny = train['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:36.120229Z","iopub.execute_input":"2025-01-30T14:12:36.120618Z","iopub.status.idle":"2025-01-30T14:12:36.37155Z","shell.execute_reply.started":"2025-01-30T14:12:36.120583Z","shell.execute_reply":"2025-01-30T14:12:36.37015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x[\"Policy Start Date\"] = pd.to_numeric(x[\"Policy Start Date\"],errors='coerce')\ntest[\"Policy Start Date\"] = pd.to_numeric(test[\"Policy Start Date\"],errors='coerce')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:36.372638Z","iopub.execute_input":"2025-01-30T14:12:36.373157Z","iopub.status.idle":"2025-01-30T14:12:37.538797Z","shell.execute_reply.started":"2025-01-30T14:12:36.373125Z","shell.execute_reply":"2025-01-30T14:12:37.535786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:37.541158Z","iopub.execute_input":"2025-01-30T14:12:37.541561Z","iopub.status.idle":"2025-01-30T14:12:37.657162Z","shell.execute_reply.started":"2025-01-30T14:12:37.541525Z","shell.execute_reply":"2025-01-30T14:12:37.655929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from xgboost import XGBRegressor\nparams = {\n    'objective':'reg:squarederror',\n    'n_estimators':200,\n    'learning_rate':0.3,\n    'eval_metric':'mae',\n    'enable_categorical' : True\n}\nmodel = XGBRegressor(**params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:37.658366Z","iopub.execute_input":"2025-01-30T14:12:37.658743Z","iopub.status.idle":"2025-01-30T14:12:37.66658Z","shell.execute_reply.started":"2025-01-30T14:12:37.658701Z","shell.execute_reply":"2025-01-30T14:12:37.664572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:37.668449Z","iopub.execute_input":"2025-01-30T14:12:37.668933Z","iopub.status.idle":"2025-01-30T14:12:37.69987Z","shell.execute_reply.started":"2025-01-30T14:12:37.668892Z","shell.execute_reply":"2025-01-30T14:12:37.697959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_t,x_v,y_t,y_v = train_test_split(x,y,test_size = 0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:37.700679Z","iopub.execute_input":"2025-01-30T14:12:37.701098Z","iopub.status.idle":"2025-01-30T14:12:38.199156Z","shell.execute_reply.started":"2025-01-30T14:12:37.701056Z","shell.execute_reply":"2025-01-30T14:12:38.197159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(x_t,y_t)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:38.202587Z","iopub.execute_input":"2025-01-30T14:12:38.203022Z","iopub.status.idle":"2025-01-30T14:12:58.359064Z","shell.execute_reply.started":"2025-01-30T14:12:38.20299Z","shell.execute_reply":"2025-01-30T14:12:58.356729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(x_v)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:58.360398Z","iopub.execute_input":"2025-01-30T14:12:58.360818Z","iopub.status.idle":"2025-01-30T14:12:59.475481Z","shell.execute_reply.started":"2025-01-30T14:12:58.360786Z","shell.execute_reply":"2025-01-30T14:12:59.474714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import r2_score\nfrom sklearn.metrics import mean_absolute_percentage_error\nmean_absolute_percentage_error(y_pred,y_v)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:59.476312Z","iopub.execute_input":"2025-01-30T14:12:59.476668Z","iopub.status.idle":"2025-01-30T14:12:59.487972Z","shell.execute_reply.started":"2025-01-30T14:12:59.476618Z","shell.execute_reply":"2025-01-30T14:12:59.486788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"r2_score(y_pred,y_v)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:12:59.49152Z","iopub.execute_input":"2025-01-30T14:12:59.491933Z","iopub.status.idle":"2025-01-30T14:12:59.519374Z","shell.execute_reply.started":"2025-01-30T14:12:59.4919Z","shell.execute_reply":"2025-01-30T14:12:59.517728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred = model.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:13:53.693955Z","iopub.execute_input":"2025-01-30T14:13:53.694483Z","iopub.status.idle":"2025-01-30T14:13:57.491852Z","shell.execute_reply.started":"2025-01-30T14:13:53.694449Z","shell.execute_reply":"2025-01-30T14:13:57.490924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tmp = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:15:09.728979Z","iopub.execute_input":"2025-01-30T14:15:09.729346Z","iopub.status.idle":"2025-01-30T14:15:12.79801Z","shell.execute_reply.started":"2025-01-30T14:15:09.729317Z","shell.execute_reply":"2025-01-30T14:15:12.79681Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({'id':tmp['id'],'Premium Amount':test_pred})\nsubmission.to_csv('submission.csv',index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-30T14:15:28.392957Z","iopub.execute_input":"2025-01-30T14:15:28.393379Z","iopub.status.idle":"2025-01-30T14:15:29.608095Z","shell.execute_reply.started":"2025-01-30T14:15:28.393348Z","shell.execute_reply":"2025-01-30T14:15:29.606736Z"}},"outputs":[],"execution_count":null}]}