{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:01.083874Z","iopub.execute_input":"2024-12-11T13:44:01.084298Z","iopub.status.idle":"2024-12-11T13:44:01.090372Z","shell.execute_reply.started":"2024-12-11T13:44:01.08426Z","shell.execute_reply":"2024-12-11T13:44:01.088872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntrain_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:01.092791Z","iopub.execute_input":"2024-12-11T13:44:01.093204Z","iopub.status.idle":"2024-12-11T13:44:05.979457Z","shell.execute_reply.started":"2024-12-11T13:44:01.093166Z","shell.execute_reply":"2024-12-11T13:44:05.978229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\ntest_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:05.981069Z","iopub.execute_input":"2024-12-11T13:44:05.981533Z","iopub.status.idle":"2024-12-11T13:44:10.120335Z","shell.execute_reply.started":"2024-12-11T13:44:05.981483Z","shell.execute_reply":"2024-12-11T13:44:10.119145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:10.121547Z","iopub.execute_input":"2024-12-11T13:44:10.122405Z","iopub.status.idle":"2024-12-11T13:44:10.773Z","shell.execute_reply.started":"2024-12-11T13:44:10.122367Z","shell.execute_reply":"2024-12-11T13:44:10.77169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def num_values(df: pd.DataFrame):\n    return df.isna().sum()\n\nprint(num_values(train_data),'\\n\\n',num_values(test_data))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:10.775412Z","iopub.execute_input":"2024-12-11T13:44:10.775744Z","iopub.status.idle":"2024-12-11T13:44:11.82565Z","shell.execute_reply.started":"2024-12-11T13:44:10.775709Z","shell.execute_reply":"2024-12-11T13:44:11.82453Z"}},"outputs":[],"execution_count":null},{"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['DayOfWeek'] = df['Policy Start Date'].dt.dayofweek\n    df.drop('Policy Start Date' , axis =1, inplace = True)\n    return df\n\n\ntrain_data = date_trans(train_data)\ntest_data = date_trans(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:11.827056Z","iopub.execute_input":"2024-12-11T13:44:11.827477Z","iopub.status.idle":"2024-12-11T13:44:13.187408Z","shell.execute_reply.started":"2024-12-11T13:44:11.827431Z","shell.execute_reply":"2024-12-11T13:44:13.186538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def feature_gen(df1: pd.DataFrame):\n    df1[\"Annual_Income_Health_Score_Ratio\"] = df1[\"Annual Income\"] / df1[\"Health Score\"] \n    df1[\"Annual_Income_Health_Score\"] = df1[\"Annual Income\"] * df1[\"Health Score\"]\n    \n    df1[\"Annual_Income_Credit_Score_Ratio\"] = df1[\"Annual Income\"] / df1[\"Credit Score\"]\n    df1[\"Annual_Income_Credit_Score\"] = df1[\"Annual Income\"] * df1[\"Credit Score\"]\n\n    df1[\"Vehicle_Age_Insurance_Duration\"] = df1[\"Vehicle Age\"] / df1[\"Insurance Duration\"]\n    df1['Annual_Income_Previous_Claims'] = df1['Previous Claims'] * df1['Annual Income']\n\n    df1['Policy Start Month Sin'] = np.sin(df1['Month'] / 12 * 2 * np.pi)\n    df1['Policy Start Month Cos'] = np.cos(df1['Month'] / 12 * 2 * np.pi)\n    df1['Policy Start Day Sin'] = np.sin(df1['Day'] / 31 * 2 * np.pi)\n    df1['Policy Start Day Cos'] = np.cos(df1['Day'] / 31 * 2 * np.pi)\n    df1['Policy Start DayOfWeek Sin'] = np.sin(df1['DayOfWeek'] / 7 * 2 * np.pi)\n    df1['Policy Start DayOfWeek Cos'] = np.cos(df1['DayOfWeek'] / 7 * 2 * np.pi)\n    return df1\n\ntrain_data = feature_gen(train_data)\ntest_data = feature_gen(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:13.188666Z","iopub.execute_input":"2024-12-11T13:44:13.189033Z","iopub.status.idle":"2024-12-11T13:44:13.692379Z","shell.execute_reply.started":"2024-12-11T13:44:13.188997Z","shell.execute_reply":"2024-12-11T13:44:13.691028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols = train_data.select_dtypes(include=['number']).columns.to_list()\ncat_cols = train_data.select_dtypes(include=['object']).columns.to_list()\n\nprint(num_cols,'\\n\\n', cat_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:13.693738Z","iopub.execute_input":"2024-12-11T13:44:13.69409Z","iopub.status.idle":"2024-12-11T13:44:14.615139Z","shell.execute_reply.started":"2024-12-11T13:44:13.694056Z","shell.execute_reply":"2024-12-11T13:44:14.613964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in cat_cols:\n    fig, axis = plt.subplots(1,2 , figsize=(10,6))\n    sns.countplot(data = train_data, x = i, palette='muted', ax=axis[0])\n    axis[0].set_title(f'Distribution of {i}')\n    axis[0].set_xlabel(i)\n    axis[0].set_ylabel('count')\n    \n    sns.boxplot(data = train_data, x=i, y='Premium Amount', palette='muted', ax=axis[1])\n    axis[1].set_title(f'Boxplot of {i}')\n    axis[1].set_xlabel(i)\n    axis[1].set_ylabel('Premium Amount')\n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:14.61676Z","iopub.execute_input":"2024-12-11T13:44:14.617228Z","iopub.status.idle":"2024-12-11T13:44:31.879388Z","shell.execute_reply.started":"2024-12-11T13:44:14.617178Z","shell.execute_reply":"2024-12-11T13:44:31.878345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in num_cols:\n    fig, axis = plt.subplots(1,2 , figsize=(10,6))\n    sns.histplot(data = train_data, x = i, kde=True, bins=30, ax=axis[0])\n    axis[0].set_title(f'Distribution of {i}')\n    axis[0].set_xlabel(i)\n    axis[0].set_ylabel('count')\n    \n    sns.boxplot(data = train_data, x=i, palette='cool', ax=axis[1])\n    axis[1].set_title(f'Boxplot of {i}')\n    axis[1].set_xlabel(i)\n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:44:31.880956Z","iopub.execute_input":"2024-12-11T13:44:31.88139Z","iopub.status.idle":"2024-12-11T13:46:41.578693Z","shell.execute_reply.started":"2024-12-11T13:44:31.881341Z","shell.execute_reply":"2024-12-11T13:46:41.577115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_data.drop(columns=['id', 'Premium Amount'], axis=1)\ny = train_data['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:41.580909Z","iopub.execute_input":"2024-12-11T13:46:41.581429Z","iopub.status.idle":"2024-12-11T13:46:41.763201Z","shell.execute_reply.started":"2024-12-11T13:46:41.581374Z","shell.execute_reply":"2024-12-11T13:46:41.761804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols.remove('id')\nnum_cols.remove('Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:41.765098Z","iopub.execute_input":"2024-12-11T13:46:41.765475Z","iopub.status.idle":"2024-12-11T13:46:41.771057Z","shell.execute_reply.started":"2024-12-11T13:46:41.765439Z","shell.execute_reply":"2024-12-11T13:46:41.769705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='median')),\n    ('scaler', StandardScaler())\n])\n\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='constant')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numerical_transformer, num_cols),\n        ('cat', categorical_transformer, cat_cols)\n    ]\n)\n\nX = preprocessor.fit_transform(X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:41.77261Z","iopub.execute_input":"2024-12-11T13:46:41.773119Z","iopub.status.idle":"2024-12-11T13:46:53.626427Z","shell.execute_reply.started":"2024-12-11T13:46:41.773062Z","shell.execute_reply":"2024-12-11T13:46:53.625251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test = preprocessor.transform(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:53.630694Z","iopub.execute_input":"2024-12-11T13:46:53.631095Z","iopub.status.idle":"2024-12-11T13:46:56.827834Z","shell.execute_reply.started":"2024-12-11T13:46:53.631059Z","shell.execute_reply":"2024-12-11T13:46:56.826825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split, GridSearchCV\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:56.829Z","iopub.execute_input":"2024-12-11T13:46:56.829336Z","iopub.status.idle":"2024-12-11T13:46:57.145775Z","shell.execute_reply.started":"2024-12-11T13:46:56.829304Z","shell.execute_reply":"2024-12-11T13:46:57.1449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_log_error\n\nmodel = xgb.XGBRegressor(eval_metric = 'rmsle', tree_method='hist')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:57.147248Z","iopub.execute_input":"2024-12-11T13:46:57.147705Z","iopub.status.idle":"2024-12-11T13:46:57.343554Z","shell.execute_reply.started":"2024-12-11T13:46:57.147644Z","shell.execute_reply":"2024-12-11T13:46:57.342326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_parameter =  {'random_state': 42,\n                   'grow_policy': 'lossguide',\n                   'n_jobs': -1,\n                   'objective': 'reg:squaredlogerror',\n                   'eval_metric': 'rmsle'}\n\nxgb_mb= XGBRegressor(**best_parameter) \nxgb_mb.fit(X_train ,y_train)\n\ny_pred = xgb_mb.predict(X_test)\nrmsle= np.sqrt(mean_squared_log_error(y_test,y_pred))\nprint(f\"RMSLE : {rmsle} \" )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:46:57.344767Z","iopub.execute_input":"2024-12-11T13:46:57.345128Z","iopub.status.idle":"2024-12-11T13:47:07.915495Z","shell.execute_reply.started":"2024-12-11T13:46:57.345094Z","shell.execute_reply":"2024-12-11T13:47:07.914291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_output = xgb_mb.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:47:07.916674Z","iopub.execute_input":"2024-12-11T13:47:07.917014Z","iopub.status.idle":"2024-12-11T13:47:08.220034Z","shell.execute_reply.started":"2024-12-11T13:47:07.916974Z","shell.execute_reply":"2024-12-11T13:47:08.219117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = pd.DataFrame(test_data['id'])\noutput['Premium Amount'] = xgb_output\noutput[['id','Premium Amount']].to_csv('/kaggle/working/submission.csv', index=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T13:47:08.221097Z","iopub.execute_input":"2024-12-11T13:47:08.221408Z","iopub.status.idle":"2024-12-11T13:47:09.482589Z","shell.execute_reply.started":"2024-12-11T13:47:08.221377Z","shell.execute_reply":"2024-12-11T13:47:09.481446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}