{"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":"gpu","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 90%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Insurance Price Prediction - CatBoost🎉</p>","metadata":{}},{"cell_type":"markdown","source":"![](https://www.hdfclife.com/content/dam/hdfclifeinsurancecompany/knowledge-center/images/about-life-insurance/HDFC-Importance-Of-Insurance-Insurance-Needs-And-Types.png)","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 100%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Import Package🎉</p>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport optuna\nfrom catboost import CatBoostRegressor\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_log_error,make_scorer\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.metrics import mean_squared_error\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\nwarnings.filterwarnings(\"ignore\", category=RuntimeWarning)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:24.115144Z","iopub.execute_input":"2024-12-18T16:56:24.115542Z","iopub.status.idle":"2024-12-18T16:56:24.122022Z","shell.execute_reply.started":"2024-12-18T16:56:24.115491Z","shell.execute_reply":"2024-12-18T16:56:24.12086Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 100%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Read Data 🎉</p>","metadata":{}},{"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')\nsubmission = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:24.12446Z","iopub.execute_input":"2024-12-18T16:56:24.12537Z","iopub.status.idle":"2024-12-18T16:56:30.769131Z","shell.execute_reply.started":"2024-12-18T16:56:24.125324Z","shell.execute_reply":"2024-12-18T16:56:30.768241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:30.770297Z","iopub.execute_input":"2024-12-18T16:56:30.770596Z","iopub.status.idle":"2024-12-18T16:56:30.796876Z","shell.execute_reply.started":"2024-12-18T16:56:30.770567Z","shell.execute_reply":"2024-12-18T16:56:30.795806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:30.798402Z","iopub.execute_input":"2024-12-18T16:56:30.798748Z","iopub.status.idle":"2024-12-18T16:56:31.413247Z","shell.execute_reply.started":"2024-12-18T16:56:30.798712Z","shell.execute_reply":"2024-12-18T16:56:31.412129Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 100%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Handling Train Data 🎉</p>","metadata":{}},{"cell_type":"code","source":"train['Policy Start Date'] = pd.to_datetime(train['Policy Start Date'])\n\ntrain['Start Year'] = train['Policy Start Date'].dt.year\ntrain['Start Month'] = train['Policy Start Date'].dt.month\ntrain['Start Day'] = train['Policy Start Date'].dt.day\n\ntrain.drop(columns=['Policy Start Date'], inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:31.415914Z","iopub.execute_input":"2024-12-18T16:56:31.416258Z","iopub.status.idle":"2024-12-18T16:56:32.095938Z","shell.execute_reply.started":"2024-12-18T16:56:31.416227Z","shell.execute_reply":"2024-12-18T16:56:32.095103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Customer Feedback'] = train['Customer Feedback'].fillna(train['Customer Feedback'].mode()[0])\ntrain['Occupation'] = train['Occupation'].fillna(train['Occupation'].mode()[0])\ntrain['Marital Status'] = train['Marital Status'].fillna(train['Marital Status'].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:32.097023Z","iopub.execute_input":"2024-12-18T16:56:32.097289Z","iopub.status.idle":"2024-12-18T16:56:32.587076Z","shell.execute_reply.started":"2024-12-18T16:56:32.097263Z","shell.execute_reply":"2024-12-18T16:56:32.585995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features_list = [\n    'Gender',\n    'Marital Status',\n    'Education Level',\n    'Occupation',\n    'Location',\n    'Policy Type',\n    'Customer Feedback',\n    'Exercise Frequency',\n    'Property Type',\n    'Smoking Status'\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:32.588375Z","iopub.execute_input":"2024-12-18T16:56:32.588702Z","iopub.status.idle":"2024-12-18T16:56:32.59332Z","shell.execute_reply.started":"2024-12-18T16:56:32.58867Z","shell.execute_reply":"2024-12-18T16:56:32.592349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_features = ['Age', \n                'Annual Income', \n                'Number of Dependents',\n                'Health Score',\n                'Vehicle Age',\n                'Credit Score',\n                'Previous Claims',\n                'Insurance Duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:32.59439Z","iopub.execute_input":"2024-12-18T16:56:32.594693Z","iopub.status.idle":"2024-12-18T16:56:32.605476Z","shell.execute_reply.started":"2024-12-18T16:56:32.594664Z","shell.execute_reply":"2024-12-18T16:56:32.604579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imputer = SimpleImputer(strategy='median')\ntrain[num_features] = imputer.fit_transform(train[num_features])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:32.606581Z","iopub.execute_input":"2024-12-18T16:56:32.606904Z","iopub.status.idle":"2024-12-18T16:56:33.912572Z","shell.execute_reply.started":"2024-12-18T16:56:32.606876Z","shell.execute_reply":"2024-12-18T16:56:33.911829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoders = {feature: LabelEncoder() for feature in features_list}\n\nfor feature, encoder in encoders.items():\n    train[feature] = encoder.fit_transform(train[feature])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:33.913663Z","iopub.execute_input":"2024-12-18T16:56:33.913983Z","iopub.status.idle":"2024-12-18T16:56:36.078438Z","shell.execute_reply.started":"2024-12-18T16:56:33.913954Z","shell.execute_reply":"2024-12-18T16:56:36.077352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:36.079677Z","iopub.execute_input":"2024-12-18T16:56:36.07998Z","iopub.status.idle":"2024-12-18T16:56:36.105339Z","shell.execute_reply.started":"2024-12-18T16:56:36.079952Z","shell.execute_reply":"2024-12-18T16:56:36.104244Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 100%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Handling Test Data 🎉</p>","metadata":{}},{"cell_type":"code","source":"test['Customer Feedback'] = test['Customer Feedback'].fillna(test['Customer Feedback'].mode()[0])\ntest['Occupation'] = test['Occupation'].fillna(test['Occupation'].mode()[0])\ntest['Marital Status'] = test['Marital Status'].fillna(test['Marital Status'].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:36.10683Z","iopub.execute_input":"2024-12-18T16:56:36.10757Z","iopub.status.idle":"2024-12-18T16:56:36.426943Z","shell.execute_reply.started":"2024-12-18T16:56:36.107523Z","shell.execute_reply":"2024-12-18T16:56:36.426037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Policy Start Date'] = pd.to_datetime(test['Policy Start Date'])\ntest['Start Year'] = test['Policy Start Date'].dt.year\ntest['Start Month'] = test['Policy Start Date'].dt.month\ntest['Start Day'] = test['Policy Start Date'].dt.day\n\ntest.drop(columns=['Policy Start Date'], inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:36.42802Z","iopub.execute_input":"2024-12-18T16:56:36.428281Z","iopub.status.idle":"2024-12-18T16:56:36.96036Z","shell.execute_reply.started":"2024-12-18T16:56:36.428255Z","shell.execute_reply":"2024-12-18T16:56:36.959272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for feature, encoder in encoders.items():\n    if feature in test.columns:  \n        test[feature] = encoder.transform(test[feature])\n\nif set(num_features).issubset(set(test.columns)):  \n    test[num_features] = imputer.transform(test[num_features])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:36.963501Z","iopub.execute_input":"2024-12-18T16:56:36.963852Z","iopub.status.idle":"2024-12-18T16:56:38.279684Z","shell.execute_reply.started":"2024-12-18T16:56:36.963821Z","shell.execute_reply":"2024-12-18T16:56:38.278849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:38.281017Z","iopub.execute_input":"2024-12-18T16:56:38.281316Z","iopub.status.idle":"2024-12-18T16:56:38.307618Z","shell.execute_reply.started":"2024-12-18T16:56:38.281288Z","shell.execute_reply":"2024-12-18T16:56:38.306504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(columns=['Premium Amount'])\ny = train['Premium Amount']\ny = np.log1p(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:38.309062Z","iopub.execute_input":"2024-12-18T16:56:38.309408Z","iopub.status.idle":"2024-12-18T16:56:38.453886Z","shell.execute_reply.started":"2024-12-18T16:56:38.309377Z","shell.execute_reply":"2024-12-18T16:56:38.453081Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 100%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Train Model 🎉</p>","metadata":{}},{"cell_type":"code","source":"def rmsle(y_true, y_pred):\n    return np.sqrt(mean_squared_log_error(y_true, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:38.454854Z","iopub.execute_input":"2024-12-18T16:56:38.455138Z","iopub.status.idle":"2024-12-18T16:56:38.460057Z","shell.execute_reply.started":"2024-12-18T16:56:38.455111Z","shell.execute_reply":"2024-12-18T16:56:38.459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_model():\n    kf = KFold(n_splits=5, shuffle=True, random_state=42)\n    oof = np.zeros(len(X))\n    models = []\n\n    for fold, (train_idx, valid_idx) in enumerate(kf.split(X)):\n        print(f\"Fold {fold + 1}\")\n        X_train, X_valid = X.iloc[train_idx], X.iloc[valid_idx]\n        y_train, y_valid = y.iloc[train_idx], y.iloc[valid_idx]\n\n        model = CatBoostRegressor(\n            iterations=3000,\n            learning_rate=0.00795745784799412,\n            depth=9,\n            eval_metric=\"RMSE\",\n            random_seed=42,\n            verbose=200,\n            task_type='GPU',\n            l2_leaf_reg =  1.71027066260486,\n        )\n        \n        model.fit(X_train,\n                  y_train,\n                  eval_set=(X_valid, y_valid), \n                  early_stopping_rounds=300\n                 )\n        models.append(model)\n        oof[valid_idx] = np.maximum(0, model.predict(X_valid))\n        fold_rmsle = rmsle(np.expm1(y_valid), np.expm1(oof[valid_idx]))\n        print(f\"Fold {fold + 1} RMSLE: {fold_rmsle}\")\n        \n    return models, oof","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:38.461125Z","iopub.execute_input":"2024-12-18T16:56:38.461373Z","iopub.status.idle":"2024-12-18T16:56:38.473622Z","shell.execute_reply.started":"2024-12-18T16:56:38.461348Z","shell.execute_reply":"2024-12-18T16:56:38.472823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"models,oof = train_model()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:56:38.474804Z","iopub.execute_input":"2024-12-18T16:56:38.475094Z","iopub.status.idle":"2024-12-18T16:59:34.978942Z","shell.execute_reply.started":"2024-12-18T16:56:38.475066Z","shell.execute_reply":"2024-12-18T16:59:34.977951Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #FFDD67; font-family: 'Arial', sans-serif; font-weight: bold; color: #006400; font-size: 100%; text-align: center; border: 3px dashed #FFD700; border-radius: 16px; padding: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);\">🎉 Submission 🎉</p>","metadata":{}},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:59:34.980181Z","iopub.execute_input":"2024-12-18T16:59:34.980477Z","iopub.status.idle":"2024-12-18T16:59:35.006237Z","shell.execute_reply.started":"2024-12-18T16:59:34.98045Z","shell.execute_reply":"2024-12-18T16:59:35.005153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds = np.zeros(len(test))\n\nfor model in models:  \n    test_preds += model.predict(test) / len(models)  \n\ntest_preds = np.expm1(test_preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:59:35.007421Z","iopub.execute_input":"2024-12-18T16:59:35.007894Z","iopub.status.idle":"2024-12-18T16:59:56.272006Z","shell.execute_reply.started":"2024-12-18T16:59:35.007852Z","shell.execute_reply":"2024-12-18T16:59:56.270932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test['id'], \n    'Premium Amount': test_preds\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:59:56.273319Z","iopub.execute_input":"2024-12-18T16:59:56.273657Z","iopub.status.idle":"2024-12-18T16:59:56.280765Z","shell.execute_reply.started":"2024-12-18T16:59:56.273608Z","shell.execute_reply":"2024-12-18T16:59:56.279816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:59:56.281765Z","iopub.execute_input":"2024-12-18T16:59:56.282108Z","iopub.status.idle":"2024-12-18T16:59:56.297368Z","shell.execute_reply.started":"2024-12-18T16:59:56.28208Z","shell.execute_reply":"2024-12-18T16:59:56.296249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\nprint(\"File saved...\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:59:56.298604Z","iopub.execute_input":"2024-12-18T16:59:56.298988Z","iopub.status.idle":"2024-12-18T16:59:57.905252Z","shell.execute_reply.started":"2024-12-18T16:59:56.298955Z","shell.execute_reply":"2024-12-18T16:59:57.904236Z"}},"outputs":[],"execution_count":null}]}