{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nimport xgboost as xgb\nfrom sklearn.model_selection import GridSearchCV\nfrom xgboost import XGBRegressor\nfrom xgboost import plot_importance\nfrom sklearn.metrics import mean_squared_log_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:31:32.990177Z","iopub.execute_input":"2024-12-23T10:31:32.990496Z","iopub.status.idle":"2024-12-23T10:31:36.130887Z","shell.execute_reply.started":"2024-12-23T10:31:32.990458Z","shell.execute_reply":"2024-12-23T10:31:36.129806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = 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":"2024-12-23T10:33:40.125391Z","iopub.execute_input":"2024-12-23T10:33:40.125821Z","iopub.status.idle":"2024-12-23T10:33:50.095086Z","shell.execute_reply.started":"2024-12-23T10:33:40.125788Z","shell.execute_reply":"2024-12-23T10:33:50.093914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:33:51.537856Z","iopub.execute_input":"2024-12-23T10:33:51.538236Z","iopub.status.idle":"2024-12-23T10:33:51.563512Z","shell.execute_reply.started":"2024-12-23T10:33:51.538204Z","shell.execute_reply":"2024-12-23T10:33:51.561891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:34:01.018427Z","iopub.execute_input":"2024-12-23T10:34:01.018884Z","iopub.status.idle":"2024-12-23T10:34:01.025932Z","shell.execute_reply.started":"2024-12-23T10:34:01.01885Z","shell.execute_reply":"2024-12-23T10:34:01.024837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.describe().round(2).T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:34:14.065877Z","iopub.execute_input":"2024-12-23T10:34:14.066267Z","iopub.status.idle":"2024-12-23T10:34:14.949698Z","shell.execute_reply.started":"2024-12-23T10:34:14.066241Z","shell.execute_reply":"2024-12-23T10:34:14.948648Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Columns ","metadata":{}},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:34:42.206027Z","iopub.execute_input":"2024-12-23T10:34:42.206463Z","iopub.status.idle":"2024-12-23T10:34:42.875669Z","shell.execute_reply.started":"2024-12-23T10:34:42.206432Z","shell.execute_reply":"2024-12-23T10:34:42.874324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"Policy Start Date\"] = pd.to_datetime(data[\"Policy Start Date\"], errors='coerce', format='%Y-%m-%d')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:35:34.239975Z","iopub.execute_input":"2024-12-23T10:35:34.240348Z","iopub.status.idle":"2024-12-23T10:35:36.306054Z","shell.execute_reply.started":"2024-12-23T10:35:34.240323Z","shell.execute_reply":"2024-12-23T10:35:36.304857Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Check for outliers and missing values","metadata":{}},{"cell_type":"code","source":"data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:36:32.264964Z","iopub.execute_input":"2024-12-23T10:36:32.265325Z","iopub.status.idle":"2024-12-23T10:36:32.833221Z","shell.execute_reply.started":"2024-12-23T10:36:32.2653Z","shell.execute_reply":"2024-12-23T10:36:32.832208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.duplicated().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:36:41.579122Z","iopub.execute_input":"2024-12-23T10:36:41.57947Z","iopub.status.idle":"2024-12-23T10:36:43.030157Z","shell.execute_reply.started":"2024-12-23T10:36:41.579446Z","shell.execute_reply":"2024-12-23T10:36:43.028811Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Split the data in terms of Numerical values and various Categories.","metadata":{}},{"cell_type":"code","source":"tar_col ='Premium Amount';\nnum_col = data.select_dtypes(include = ['number']).columns\ncat_col = data.select_dtypes(include = ['object']).columns\nprint(\"Target Column :\" ,tar_col)\nprint( \"\\nNumerical Columns :\" , num_col.tolist())\nprint( \"\\nCategorical Columns :\" , cat_col.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:37:29.789991Z","iopub.execute_input":"2024-12-23T10:37:29.790332Z","iopub.status.idle":"2024-12-23T10:37:30.348254Z","shell.execute_reply.started":"2024-12-23T10:37:29.790309Z","shell.execute_reply":"2024-12-23T10:37:30.347041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_data = data.select_dtypes(include = ['number'])\ncat_data = data.select_dtypes(include = ['object'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:37:52.936493Z","iopub.execute_input":"2024-12-23T10:37:52.936938Z","iopub.status.idle":"2024-12-23T10:37:53.551645Z","shell.execute_reply.started":"2024-12-23T10:37:52.936907Z","shell.execute_reply":"2024-12-23T10:37:53.550156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_data.describe().round(2).T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:38:01.502932Z","iopub.execute_input":"2024-12-23T10:38:01.503348Z","iopub.status.idle":"2024-12-23T10:38:02.286239Z","shell.execute_reply.started":"2024-12-23T10:38:01.503314Z","shell.execute_reply":"2024-12-23T10:38:02.285178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_data.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:38:34.368998Z","iopub.execute_input":"2024-12-23T10:38:34.369383Z","iopub.status.idle":"2024-12-23T10:38:35.924513Z","shell.execute_reply.started":"2024-12-23T10:38:34.369355Z","shell.execute_reply":"2024-12-23T10:38:35.923267Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Statistical Analysis","metadata":{}},{"cell_type":"code","source":"for c  in cat_col:\n    col_cnt= data[c].nunique()\n    print(f\"{c} has {col_cnt} unique values .\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:39:54.093243Z","iopub.execute_input":"2024-12-23T10:39:54.093604Z","iopub.status.idle":"2024-12-23T10:39:54.677063Z","shell.execute_reply.started":"2024-12-23T10:39:54.093578Z","shell.execute_reply":"2024-12-23T10:39:54.675693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in cat_col:\n    print (f'Value Count for {i}')\n    print(data[i].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:40:13.569976Z","iopub.execute_input":"2024-12-23T10:40:13.570381Z","iopub.status.idle":"2024-12-23T10:40:14.443658Z","shell.execute_reply.started":"2024-12-23T10:40:13.570352Z","shell.execute_reply":"2024-12-23T10:40:14.442549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_data  = data.groupby(\"Premium Amount\").sample(frac=0.2, random_state= 8).reset_index(drop = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:40:39.515797Z","iopub.execute_input":"2024-12-23T10:40:39.516148Z","iopub.status.idle":"2024-12-23T10:40:41.585108Z","shell.execute_reply.started":"2024-12-23T10:40:39.516122Z","shell.execute_reply":"2024-12-23T10:40:41.583838Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Data Visualization","metadata":{}},{"cell_type":"code","source":"for i in cat_col:\n    fig , axes = plt.subplots(1,2 , figsize=(10,6))\n    sns.countplot(data=sample_data, x=i, palette='muted', ax= axes[0])\n    axes[0].set_title(f'Distribution of {i}', fontsize=20)\n    axes[0].set_xlabel(i, fontsize=12)\n    axes[0].set_ylabel('Count', fontsize=12)\n    sns.boxplot(data , x=i , y=tar_col ,palette= 'muted', ax= axes[1])\n    axes[1].set_title(f\"Box Plot for {i}\" , fontsize = 20)\n    axes[1].set_xlabel(i,fontsize=12)\n    axes[1].set_ylabel(tar_col,fontsize=12)\n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:46:16.006299Z","iopub.execute_input":"2024-12-23T10:46:16.006705Z","iopub.status.idle":"2024-12-23T10:46:27.457657Z","shell.execute_reply.started":"2024-12-23T10:46:16.006674Z","shell.execute_reply":"2024-12-23T10:46:27.456066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in num_col:\n    fig, axes = plt.subplots(1, 2, figsize=(10, 6))\n    \n    # Histogram Plot\n    sns.histplot(\n        sample_data, \n        x=i, \n        bins=20, \n        kde=True, \n        color='orange',  \n        edgecolor='black', \n        alpha=0.6, \n        ax=axes[0]\n    )\n    axes[0].set_title(f\"Histogram Plot for {i}\", fontsize=20)\n    axes[0].set_xlabel(i, fontsize=15)\n    \n    # Box Plot\n    sns.boxplot(\n        data=sample_data, \n        x=i, \n        color='red',  \n        ax=axes[1]\n    )\n    axes[1].set_title(f\"Box Plot for {i}\", fontsize=20)\n    axes[1].set_xlabel(i, fontsize=15)\n    \n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:51:22.949182Z","iopub.execute_input":"2024-12-23T10:51:22.949551Z","iopub.status.idle":"2024-12-23T10:51:36.243133Z","shell.execute_reply.started":"2024-12-23T10:51:22.949522Z","shell.execute_reply":"2024-12-23T10:51:36.242038Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Correlation Matrix","metadata":{}},{"cell_type":"code","source":"corr_mat = sample_data[num_col].corr()\nmask = np.triu(np.ones_like(corr_mat, dtype=bool))\nplt.figure(figsize = (20,16))\nsns.heatmap(sample_data[num_col].corr(),\n            mask=mask,\n            annot = True ,   fmt ='.2f',\n            linewidth=1    ,cmap = 'flare'\n           )\nplt.xticks(fontsize = 9)\nplt.yticks(fontsize = 9)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:52:21.387223Z","iopub.execute_input":"2024-12-23T10:52:21.387695Z","iopub.status.idle":"2024-12-23T10:52:22.072244Z","shell.execute_reply.started":"2024-12-23T10:52:21.387662Z","shell.execute_reply":"2024-12-23T10:52:22.070493Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Pre-processing","metadata":{}},{"cell_type":"code","source":"def date_trans(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.drop('Policy Start Date' , axis =1, inplace = True)\n    return df\n\n\ntrain_data = date_trans(data)\ntest_data = date_trans(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:52:49.489224Z","iopub.execute_input":"2024-12-23T10:52:49.489621Z","iopub.status.idle":"2024-12-23T10:52:50.328856Z","shell.execute_reply.started":"2024-12-23T10:52:49.489592Z","shell.execute_reply":"2024-12-23T10:52:50.327749Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_data.drop(columns=[tar_col, 'id' , 'Year', 'Month', 'Day'])\ny = train_data[tar_col]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:53:16.002143Z","iopub.execute_input":"2024-12-23T10:53:16.002508Z","iopub.status.idle":"2024-12-23T10:53:16.162036Z","shell.execute_reply.started":"2024-12-23T10:53:16.00248Z","shell.execute_reply":"2024-12-23T10:53:16.160638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_col = num_col.drop(['id','Premium Amount'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:53:26.393533Z","iopub.execute_input":"2024-12-23T10:53:26.393941Z","iopub.status.idle":"2024-12-23T10:53:26.400159Z","shell.execute_reply.started":"2024-12-23T10:53:26.393912Z","shell.execute_reply":"2024-12-23T10:53:26.398612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_pipeline = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='median')),\n    # ('scaler', StandardScaler())                       # Scale numerical features\n])\n\n# Preprocessing pipeline for categorical features\ncat_pipeline = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='constant', fill_value='Unknown')),  # Handle missing values\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))                      # Encode categorical features\n])\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', num_pipeline, num_col),\n        ('cat', cat_pipeline, cat_col)\n    ]\n)\nX_processed = preprocessor.fit_transform(X)\ntest_transformed = preprocessor.transform(test_data.drop(columns=['id',  'Year', 'Month', 'Day']))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:53:43.972896Z","iopub.execute_input":"2024-12-23T10:53:43.973245Z","iopub.status.idle":"2024-12-23T10:53:55.321502Z","shell.execute_reply.started":"2024-12-23T10:53:43.973219Z","shell.execute_reply":"2024-12-23T10:53:55.32014Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Train-Test Split","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_processed, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:53:59.518424Z","iopub.execute_input":"2024-12-23T10:53:59.518858Z","iopub.status.idle":"2024-12-23T10:53:59.905231Z","shell.execute_reply.started":"2024-12-23T10:53:59.518827Z","shell.execute_reply":"2024-12-23T10:53:59.904085Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"XGBRegressor","metadata":{}},{"cell_type":"code","source":"xgb_model = xgb.XGBRegressor(eval_metric='rmsle', tree_method='hist')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:54:13.268073Z","iopub.execute_input":"2024-12-23T10:54:13.268493Z","iopub.status.idle":"2024-12-23T10:54:13.273782Z","shell.execute_reply.started":"2024-12-23T10:54:13.268464Z","shell.execute_reply":"2024-12-23T10:54:13.2725Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Hyper-Parameter Tuning","metadata":{}},{"cell_type":"code","source":"param_grid = {\n    'learning_rate': [0.01, 0.1, 0.2],\n    'n_estimators': [50, 100, 200],\n    'max_depth': [3, 6, 10],\n    'subsample': [0.7, 0.8, 1.0],\n    'colsample_bytree': [0.7, 0.8, 1.0]\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:55:17.459101Z","iopub.execute_input":"2024-12-23T10:55:17.459559Z","iopub.status.idle":"2024-12-23T10:55:17.46558Z","shell.execute_reply.started":"2024-12-23T10:55:17.459525Z","shell.execute_reply":"2024-12-23T10:55:17.464143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grid_search = GridSearchCV(estimator=xgb_model, param_grid=param_grid, error_score='raise')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:55:28.312491Z","iopub.execute_input":"2024-12-23T10:55:28.312952Z","iopub.status.idle":"2024-12-23T10:55:28.319175Z","shell.execute_reply.started":"2024-12-23T10:55:28.31292Z","shell.execute_reply":"2024-12-23T10:55:28.317853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_parameter =  {'colsample_bytree': 1.0, 'learning_rate': 0.01, 'max_depth': 10, 'n_estimators': 200, 'subsample': 0.8}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:55:38.984504Z","iopub.execute_input":"2024-12-23T10:55:38.984951Z","iopub.status.idle":"2024-12-23T10:55:38.990499Z","shell.execute_reply.started":"2024-12-23T10:55:38.984919Z","shell.execute_reply":"2024-12-23T10:55:38.989129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_mb= XGBRegressor(**best_parameter) # Unpack the best_parameter dictionary\nxgb_mb.fit(X_train ,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:55:45.939946Z","iopub.execute_input":"2024-12-23T10:55:45.940366Z","iopub.status.idle":"2024-12-23T10:56:14.188449Z","shell.execute_reply.started":"2024-12-23T10:55:45.940337Z","shell.execute_reply":"2024-12-23T10:56:14.187266Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Model Evaluation","metadata":{}},{"cell_type":"code","source":"y_val = np.nan_to_num(y_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:56:37.441596Z","iopub.execute_input":"2024-12-23T10:56:37.441992Z","iopub.status.idle":"2024-12-23T10:56:37.448566Z","shell.execute_reply.started":"2024-12-23T10:56:37.441964Z","shell.execute_reply":"2024-12-23T10:56:37.447329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = xgb_mb.predict(X_val)\nrmsle= np.sqrt(mean_squared_log_error(y_val,y_pred))\nprint(f\"RMSLE : {rmsle} \" )\n\n\nplot_importance(xgb_mb, importance_type='gain', title='XGB Feature Importance', max_num_features=10, color='purple')\nsns.set_palette('viridis')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:56:46.310585Z","iopub.execute_input":"2024-12-23T10:56:46.311068Z","iopub.status.idle":"2024-12-23T10:56:47.816375Z","shell.execute_reply.started":"2024-12-23T10:56:46.311036Z","shell.execute_reply":"2024-12-23T10:56:47.81503Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Submission","metadata":{}},{"cell_type":"code","source":"output= pd.DataFrame(test_data['id'])\nxgb_output = xgb_mb.predict(test_transformed)\noutput['Premium Amount']= xgb_output\noutput.to_csv(\"insurance_project.csv\", index = None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:58:15.682242Z","iopub.execute_input":"2024-12-23T10:58:15.682609Z","iopub.status.idle":"2024-12-23T10:58:20.894588Z","shell.execute_reply.started":"2024-12-23T10:58:15.682583Z","shell.execute_reply":"2024-12-23T10:58:20.89324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-23T10:58:35.076396Z","iopub.execute_input":"2024-12-23T10:58:35.077138Z","iopub.status.idle":"2024-12-23T10:58:35.087352Z","shell.execute_reply.started":"2024-12-23T10:58:35.077087Z","shell.execute_reply":"2024-12-23T10:58:35.085428Z"}},"outputs":[],"execution_count":null}]}