{"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":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Libraries and Load Data:","metadata":{}},{"cell_type":"code","source":"SEED=25","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:52.343916Z","iopub.execute_input":"2024-12-07T06:37:52.34437Z","iopub.status.idle":"2024-12-07T06:37:52.35015Z","shell.execute_reply.started":"2024-12-07T06:37:52.344332Z","shell.execute_reply":"2024-12-07T06:37:52.348679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.metrics import mean_squared_log_error as msle\nfrom sklearn.impute import KNNImputer,SimpleImputer\nfrom sklearn.compose import ColumnTransformer\n\nfrom xgboost import XGBRegressor\nfrom lightgbm import LGBMRegressor\nfrom catboost import CatBoostRegressor\n\n\npd.option_context('mode.use_inf_as_na', True)\npd.set_option('display.max_columns', 500)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:26.257213Z","iopub.execute_input":"2024-12-07T06:37:26.257758Z","iopub.status.idle":"2024-12-07T06:37:29.739899Z","shell.execute_reply.started":"2024-12-07T06:37:26.257709Z","shell.execute_reply":"2024-12-07T06:37:29.738682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_data=pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\nraw_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:29.74211Z","iopub.execute_input":"2024-12-07T06:37:29.74283Z","iopub.status.idle":"2024-12-07T06:37:36.018186Z","shell.execute_reply.started":"2024-12-07T06:37:29.742779Z","shell.execute_reply":"2024-12-07T06:37:36.017039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:36.019319Z","iopub.execute_input":"2024-12-07T06:37:36.019644Z","iopub.status.idle":"2024-12-07T06:37:36.05175Z","shell.execute_reply.started":"2024-12-07T06:37:36.019614Z","shell.execute_reply":"2024-12-07T06:37:36.050685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_data.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:04:50.684331Z","iopub.execute_input":"2024-12-02T14:04:50.684748Z","iopub.status.idle":"2024-12-02T14:04:51.330901Z","shell.execute_reply.started":"2024-12-02T14:04:50.684713Z","shell.execute_reply":"2024-12-02T14:04:51.329781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_data.hist(figsize=(20,20))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T13:23:15.913548Z","iopub.execute_input":"2024-12-02T13:23:15.914516Z","iopub.status.idle":"2024-12-02T13:23:18.608191Z","shell.execute_reply.started":"2024-12-02T13:23:15.914476Z","shell.execute_reply":"2024-12-02T13:23:18.606989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"corr=raw_data.select_dtypes(exclude='object').corr()\nsns.heatmap(corr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T13:28:31.73651Z","iopub.execute_input":"2024-12-02T13:28:31.737564Z","iopub.status.idle":"2024-12-02T13:28:32.651014Z","shell.execute_reply.started":"2024-12-02T13:28:31.737527Z","shell.execute_reply":"2024-12-02T13:28:32.649867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_data['Policy Start Date']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:09:29.379185Z","iopub.execute_input":"2024-12-02T14:09:29.3796Z","iopub.status.idle":"2024-12-02T14:09:29.388002Z","shell.execute_reply.started":"2024-12-02T14:09:29.379565Z","shell.execute_reply":"2024-12-02T14:09:29.387021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"year=raw_data['Policy Start Date'].apply(lambda x:float(x[17:]))\nyear.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:15:38.780804Z","iopub.execute_input":"2024-12-02T14:15:38.781188Z","iopub.status.idle":"2024-12-02T14:15:39.372575Z","shell.execute_reply.started":"2024-12-02T14:15:38.781155Z","shell.execute_reply":"2024-12-02T14:15:39.371515Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Preprocess Data:","metadata":{}},{"cell_type":"code","source":"def process_year(raw_data):\n    raw_data['start_year']=raw_data['Policy Start Date'].apply(lambda x:int(x[0:4]))\n    raw_data['start_month']=raw_data['Policy Start Date'].apply(lambda x:int(x[5:7]))\n    raw_data['start_day']=raw_data['Policy Start Date'].apply(lambda x:int(x[8:10]))\n    raw_data['start_hour']=raw_data['Policy Start Date'].apply(lambda x:int(x[11:13]))\n    raw_data['start_minute']=raw_data['Policy Start Date'].apply(lambda x:int(x[14:16]))\n    raw_data['start_second']=raw_data['Policy Start Date'].apply(lambda x:float(x[17:]))\n    raw_data.drop('Policy Start Date',axis=1,inplace=True)\n\n\ndate_processed=raw_data.copy()\nprocess_year(date_processed)\ndate_processed.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:36.054096Z","iopub.execute_input":"2024-12-07T06:37:36.054417Z","iopub.status.idle":"2024-12-07T06:37:40.173472Z","shell.execute_reply.started":"2024-12-07T06:37:36.054385Z","shell.execute_reply":"2024-12-07T06:37:40.17245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train,val=train_test_split(date_processed,test_size=0.1,random_state=SEED)\nprint(train.shape)\nprint(val.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:56.074461Z","iopub.execute_input":"2024-12-07T06:37:56.075402Z","iopub.status.idle":"2024-12-07T06:37:56.893325Z","shell.execute_reply.started":"2024-12-07T06:37:56.075342Z","shell.execute_reply":"2024-12-07T06:37:56.892302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# numerical_cols=train.select_dtypes(exclude='object').columns\n\n# ct=ColumnTransformer(\n#     [\n#         ('imputer',SimpleImputer(),numerical_cols)\n#     ],\n#     remainder='passthrough',\n#     verbose_feature_names_out=False\n# )\n\n# ct.set_output(transform='pandas')\n\n# ct","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:04.452562Z","iopub.execute_input":"2024-12-06T04:47:04.453591Z","iopub.status.idle":"2024-12-06T04:47:04.649169Z","shell.execute_reply.started":"2024-12-06T04:47:04.453535Z","shell.execute_reply":"2024-12-06T04:47:04.647999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train=ct.fit_transform(train)\n# val=ct.transform(val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:50:10.579632Z","iopub.execute_input":"2024-12-07T04:50:10.580028Z","iopub.status.idle":"2024-12-07T04:50:10.957144Z","shell.execute_reply.started":"2024-12-07T04:50:10.579991Z","shell.execute_reply":"2024-12-07T04:50:10.955365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def clean(cleaned):\n    for col in cleaned.select_dtypes(include='object').columns:\n        cleaned[col]=cleaned[col].fillna('-')\n        cleaned[col]=cleaned[col].astype('category')\n        \nclean(train)\nclean(val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:37:58.991707Z","iopub.execute_input":"2024-12-07T06:37:58.992549Z","iopub.status.idle":"2024-12-07T06:38:00.914878Z","shell.execute_reply.started":"2024-12-07T06:37:58.992499Z","shell.execute_reply":"2024-12-07T06:38:00.913689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def new_features(df):\n    df['Income_Deps']=df['Annual Income']/(df['Number of Dependents']+1)\n\nnew_features(train)\nnew_features(val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:38:00.916656Z","iopub.execute_input":"2024-12-07T06:38:00.917114Z","iopub.status.idle":"2024-12-07T06:38:00.929965Z","shell.execute_reply.started":"2024-12-07T06:38:00.917076Z","shell.execute_reply":"2024-12-07T06:38:00.92886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def log_target(df):\n    df['Premium Amount']=np.log(df['Premium Amount'])\n    \nlog_target(train)\nlog_target(val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:38:00.931372Z","iopub.execute_input":"2024-12-07T06:38:00.931804Z","iopub.status.idle":"2024-12-07T06:38:00.951071Z","shell.execute_reply.started":"2024-12-07T06:38:00.931759Z","shell.execute_reply":"2024-12-07T06:38:00.950063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.select_dtypes(exclude='category').corr()['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:50:23.109185Z","iopub.execute_input":"2024-12-07T04:50:23.1096Z","iopub.status.idle":"2024-12-07T04:50:24.307494Z","shell.execute_reply.started":"2024-12-07T04:50:23.109567Z","shell.execute_reply":"2024-12-07T04:50:24.306309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=train.drop(['id','Premium Amount'],axis=1)\ny=train['Premium Amount']\n\nX_val=val.drop(['id','Premium Amount'],axis=1)\ny_val=val['Premium Amount']\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:38:09.057069Z","iopub.execute_input":"2024-12-07T06:38:09.057478Z","iopub.status.idle":"2024-12-07T06:38:09.148254Z","shell.execute_reply.started":"2024-12-07T06:38:09.05744Z","shell.execute_reply":"2024-12-07T06:38:09.147165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:50:31.674487Z","iopub.execute_input":"2024-12-07T04:50:31.674885Z","iopub.status.idle":"2024-12-07T04:50:31.700743Z","shell.execute_reply.started":"2024-12-07T04:50:31.674854Z","shell.execute_reply":"2024-12-07T04:50:31.69956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.select_dtypes(include='category').isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:50:33.525777Z","iopub.execute_input":"2024-12-07T04:50:33.526161Z","iopub.status.idle":"2024-12-07T04:50:33.564159Z","shell.execute_reply.started":"2024-12-07T04:50:33.526127Z","shell.execute_reply":"2024-12-07T04:50:33.563066Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Models:","metadata":{}},{"cell_type":"code","source":"xgb_params={'gamma': 0,\n             'max_depth': 3,\n             'min_child_weight': 1.5,\n             'subsample': 0.9992535151304245}\nxgb_model=XGBRegressor(n_estimators=250,**xgb_params,enable_categorical=True,random_state=SEED)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:38:11.12663Z","iopub.execute_input":"2024-12-07T06:38:11.127028Z","iopub.status.idle":"2024-12-07T06:38:11.134131Z","shell.execute_reply.started":"2024-12-07T06:38:11.126995Z","shell.execute_reply":"2024-12-07T06:38:11.132536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_model.fit(X,y,eval_set=[(X,y),(X_val,y_val)],eval_metric='rmse')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:50:39.280584Z","iopub.execute_input":"2024-12-06T04:50:39.28097Z","iopub.status.idle":"2024-12-06T04:51:20.710844Z","shell.execute_reply.started":"2024-12-06T04:50:39.280936Z","shell.execute_reply":"2024-12-06T04:51:20.709571Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_preds_xgb=xgb_model.predict(X_val)\nval_preds_xgb[val_preds_xgb<0]=0\nprint('XGB: ',msle(y_val,val_preds_xgb)**0.5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:38:14.747561Z","iopub.execute_input":"2024-12-07T06:38:14.74799Z","iopub.status.idle":"2024-12-07T06:38:14.895023Z","shell.execute_reply.started":"2024-12-07T06:38:14.747954Z","shell.execute_reply":"2024-12-07T06:38:14.893174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_params={'feature_fraction': 0.8424285571261643,\n             'max_depth': 7,\n             'min_data_in_leaf': 20,\n             'num_leaves':130\n            }\nlgbm_model=LGBMRegressor(**lgbm_params,random_state=SEED)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:40:06.644529Z","iopub.execute_input":"2024-12-07T06:40:06.647035Z","iopub.status.idle":"2024-12-07T06:40:06.653248Z","shell.execute_reply.started":"2024-12-07T06:40:06.646972Z","shell.execute_reply":"2024-12-07T06:40:06.652213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_model.fit(X,y,eval_set=[(X_val,y_val)],eval_metric='rmse')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:40:08.896538Z","iopub.execute_input":"2024-12-07T06:40:08.897042Z","iopub.status.idle":"2024-12-07T06:40:26.69437Z","shell.execute_reply.started":"2024-12-07T06:40:08.897001Z","shell.execute_reply":"2024-12-07T06:40:26.693428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_preds_lgbm=lgbm_model.predict(X_val)\nval_preds_lgbm[val_preds_lgbm<0]=0\nprint('LGBM: ',msle(y_val,val_preds_lgbm)**0.5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T06:40:29.61907Z","iopub.execute_input":"2024-12-07T06:40:29.619477Z","iopub.status.idle":"2024-12-07T06:40:30.950256Z","shell.execute_reply.started":"2024-12-07T06:40:29.619441Z","shell.execute_reply":"2024-12-07T06:40:30.949246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_model=CatBoostRegressor(random_state=SEED)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T15:26:57.963814Z","iopub.execute_input":"2024-12-02T15:26:57.965235Z","iopub.status.idle":"2024-12-02T15:26:57.970814Z","shell.execute_reply.started":"2024-12-02T15:26:57.965175Z","shell.execute_reply":"2024-12-02T15:26:57.969583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_model.fit(X,y,eval_set=[(X_val,y_val)],cat_features=list(X.select_dtypes(include='category').columns))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T15:26:59.739592Z","iopub.execute_input":"2024-12-02T15:26:59.740008Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Prediction:","metadata":{}},{"cell_type":"code","source":"val_preds_xgb=xgb_model.predict(X_val)\nval_preds_xgb[val_preds_xgb<0]=0\n\nval_preds_lgbm=lgbm_model.predict(X_val)\nval_preds_lgbm[val_preds_lgbm<0]=0\n\nval_preds_cat=cat_model.predict(X_val)\nval_preds_cat[val_preds_cat<0]=0\n\nprint('Combined error: ',msle(y_val,(val_preds_xgb+val_preds_lgbm+val_preds_cat)/3)**0.5)\nprint('LGBM: ',msle(y_val,val_preds_lgbm)**0.5)\nprint('XGB: ',msle(y_val,val_preds_xgb)**0.5)\nprint('Cat: ',msle(y_val,val_preds_cat)**0.5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T12:38:22.235144Z","iopub.execute_input":"2024-12-03T12:38:22.235553Z","iopub.status.idle":"2024-12-03T12:38:22.388882Z","shell.execute_reply.started":"2024-12-03T12:38:22.235517Z","shell.execute_reply":"2024-12-03T12:38:22.386921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\nprocess_year(test)\nclean(test)\nnew_features(test)\nX_test=test.drop('id',axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:20:28.288167Z","iopub.execute_input":"2024-12-02T14:20:28.289308Z","iopub.status.idle":"2024-12-02T14:20:35.17079Z","shell.execute_reply.started":"2024-12-02T14:20:28.289251Z","shell.execute_reply":"2024-12-02T14:20:35.169651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_preds=xgb_model.predict(X_test)\nlgbm_preds=lgbm_model.predict(X_test)\ncat_preds=cat_model.predict(X_test)\npreds=(xgb_preds+lgbm_preds+cat_preds)/3\npreds=np.exp(preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:39:46.197885Z","iopub.execute_input":"2024-12-02T14:39:46.198376Z","iopub.status.idle":"2024-12-02T14:40:00.660753Z","shell.execute_reply.started":"2024-12-02T14:39:46.198335Z","shell.execute_reply":"2024-12-02T14:40:00.659479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds[preds<0]=0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:21:22.364922Z","iopub.execute_input":"2024-12-02T14:21:22.365308Z","iopub.status.idle":"2024-12-02T14:21:22.371036Z","shell.execute_reply.started":"2024-12-02T14:21:22.365276Z","shell.execute_reply":"2024-12-02T14:21:22.369889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=pd.DataFrame({'id':test.id,'Premium Amount':preds})\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:22:46.529273Z","iopub.execute_input":"2024-12-02T14:22:46.529824Z","iopub.status.idle":"2024-12-02T14:22:46.54533Z","shell.execute_reply.started":"2024-12-02T14:22:46.529772Z","shell.execute_reply":"2024-12-02T14:22:46.543914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T14:23:02.343313Z","iopub.execute_input":"2024-12-02T14:23:02.34376Z","iopub.status.idle":"2024-12-02T14:23:03.582598Z","shell.execute_reply.started":"2024-12-02T14:23:02.343722Z","shell.execute_reply":"2024-12-02T14:23:03.581172Z"}},"outputs":[],"execution_count":null}]}