{"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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">Unlocking the Future of Insurance Prediction 🔮</p>\r\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport optuna\nfrom sklearn.model_selection import train_test_split\nfrom catboost import CatBoostRegressor, Pool\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import LabelEncoder\nimport optuna\nfrom sklearn.metrics import mean_squared_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:16.817935Z","iopub.execute_input":"2024-12-01T14:20:16.818887Z","iopub.status.idle":"2024-12-01T14:20:16.82364Z","shell.execute_reply.started":"2024-12-01T14:20:16.818852Z","shell.execute_reply":"2024-12-01T14:20:16.822741Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">Loading Insurance 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-01T14:20:16.825243Z","iopub.execute_input":"2024-12-01T14:20:16.825513Z","iopub.status.idle":"2024-12-01T14:20:22.525978Z","shell.execute_reply.started":"2024-12-01T14:20:16.825489Z","shell.execute_reply":"2024-12-01T14:20:22.52496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:22.526995Z","iopub.execute_input":"2024-12-01T14:20:22.527295Z","iopub.status.idle":"2024-12-01T14:20:22.547165Z","shell.execute_reply.started":"2024-12-01T14:20:22.527268Z","shell.execute_reply":"2024-12-01T14:20:22.546332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:22.548879Z","iopub.execute_input":"2024-12-01T14:20:22.549148Z","iopub.status.idle":"2024-12-01T14:20:23.127165Z","shell.execute_reply.started":"2024-12-01T14:20:22.549123Z","shell.execute_reply":"2024-12-01T14:20:23.126206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:23.12803Z","iopub.execute_input":"2024-12-01T14:20:23.128275Z","iopub.status.idle":"2024-12-01T14:20:23.684351Z","shell.execute_reply.started":"2024-12-01T14:20:23.128251Z","shell.execute_reply":"2024-12-01T14:20:23.683462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:23.685708Z","iopub.execute_input":"2024-12-01T14:20:23.685992Z","iopub.status.idle":"2024-12-01T14:20:24.221393Z","shell.execute_reply.started":"2024-12-01T14:20:23.685965Z","shell.execute_reply":"2024-12-01T14:20:24.22051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column in train.columns:\n    unique_values = train[column].unique()  \n    print(f\"Unique values in '{column}':\")\n    print(unique_values)\n    print(\"-\" * 40)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:24.222293Z","iopub.execute_input":"2024-12-01T14:20:24.222532Z","iopub.status.idle":"2024-12-01T14:20:25.115416Z","shell.execute_reply.started":"2024-12-01T14:20:24.222504Z","shell.execute_reply":"2024-12-01T14:20:25.114519Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">🎛️ Data Preprocessing</p>","metadata":{}},{"cell_type":"code","source":"mapping_dict = {\n    'Smoking Status': {'No': 0, 'Yes': 1},\n    'Exercise Frequency': {'Weekly': 1, 'Monthly': 2, 'Daily': 3, 'Rarely': 4},\n    'Property Type': {'House': 1, 'Apartment': 2, 'Condo': 3},\n    'Location': {'Urban': 1, 'Rural': 2, 'Suburban': 3},\n    'Policy Type': {'Premium': 1, 'Comprehensive': 2, 'Basic': 3},\n    'Education Level': {\"Bachelor's\": 1, \"Master's\": 2, 'High School': 3, 'PhD': 4},\n    'Gender': {'Female': 0, 'Male': 1}\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:25.116734Z","iopub.execute_input":"2024-12-01T14:20:25.117116Z","iopub.status.idle":"2024-12-01T14:20:25.122681Z","shell.execute_reply.started":"2024-12-01T14:20:25.117074Z","shell.execute_reply":"2024-12-01T14:20:25.121793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"columns_to_fill = {\n    'Age': 'mean',\n    'Customer Feedback': 'mode',\n    'Insurance Duration': 'mode',\n    'Vehicle Age': 'mean',\n    'Annual Income': 'mean',\n    'Marital Status': 'mode',\n    'Number of Dependents': 'mode',\n    'Occupation': 'Unemployed',\n    'Health Score': 'mean',\n    'Previous Claims': 'mode',\n    'Credit Score': 'mean'\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:25.123708Z","iopub.execute_input":"2024-12-01T14:20:25.123933Z","iopub.status.idle":"2024-12-01T14:20:25.13333Z","shell.execute_reply.started":"2024-12-01T14:20:25.12391Z","shell.execute_reply":"2024-12-01T14:20:25.132648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column, method in columns_to_fill.items():\n    if column in train.columns:\n        if method == 'mean':\n            train[column] = train[column].fillna(train[column].mean())\n        elif method == 'mode':\n            train[column] = train[column].fillna(train[column].mode()[0])\n        else:\n            train[column] = train[column].fillna(method)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:25.135841Z","iopub.execute_input":"2024-12-01T14:20:25.13614Z","iopub.status.idle":"2024-12-01T14:20:25.594285Z","shell.execute_reply.started":"2024-12-01T14:20:25.136096Z","shell.execute_reply":"2024-12-01T14:20:25.593548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column, mapping in mapping_dict.items():\n    if column in train.columns:\n        train[column] = train[column].map(mapping)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:25.595235Z","iopub.execute_input":"2024-12-01T14:20:25.595471Z","iopub.status.idle":"2024-12-01T14:20:26.051306Z","shell.execute_reply.started":"2024-12-01T14:20:25.595448Z","shell.execute_reply":"2024-12-01T14:20:26.050586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Occupation'] = train['Occupation'].fillna(\"Unemployed\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:26.052212Z","iopub.execute_input":"2024-12-01T14:20:26.052453Z","iopub.status.idle":"2024-12-01T14:20:26.116554Z","shell.execute_reply.started":"2024-12-01T14:20:26.052429Z","shell.execute_reply":"2024-12-01T14:20:26.115702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column, method in columns_to_fill.items():\n    if column in test.columns:\n        if method == 'mean':\n            test[column] = test[column].fillna(test[column].mean())\n        elif method == 'mode':\n            test[column] = test[column].fillna(test[column].mode()[0])\n        else:\n            test[column] = test[column].fillna(method)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:26.117501Z","iopub.execute_input":"2024-12-01T14:20:26.117764Z","iopub.status.idle":"2024-12-01T14:20:26.421935Z","shell.execute_reply.started":"2024-12-01T14:20:26.117738Z","shell.execute_reply":"2024-12-01T14:20:26.421248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column, mapping in mapping_dict.items():\n    if column in train.columns:\n        test[column] = test[column].map(mapping)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:26.422989Z","iopub.execute_input":"2024-12-01T14:20:26.423346Z","iopub.status.idle":"2024-12-01T14:20:26.733615Z","shell.execute_reply.started":"2024-12-01T14:20:26.423309Z","shell.execute_reply":"2024-12-01T14:20:26.732915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Occupation'] = test['Occupation'].fillna(\"Unemployed\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:26.734666Z","iopub.execute_input":"2024-12-01T14:20:26.735002Z","iopub.status.idle":"2024-12-01T14:20:26.781331Z","shell.execute_reply.started":"2024-12-01T14:20:26.734965Z","shell.execute_reply":"2024-12-01T14:20:26.780365Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">💡 Feature Engineering</p>","metadata":{}},{"cell_type":"code","source":"object_column_names = train.select_dtypes(include=['object']).columns\nprint(\"Object Column Names:\", object_column_names.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:26.782244Z","iopub.execute_input":"2024-12-01T14:20:26.782473Z","iopub.status.idle":"2024-12-01T14:20:26.997814Z","shell.execute_reply.started":"2024-12-01T14:20:26.782449Z","shell.execute_reply":"2024-12-01T14:20:26.996971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"object_columns = ['Marital Status', 'Occupation', 'Policy Start Date', 'Customer Feedback']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:26.998878Z","iopub.execute_input":"2024-12-01T14:20:26.999153Z","iopub.status.idle":"2024-12-01T14:20:27.003186Z","shell.execute_reply.started":"2024-12-01T14:20:26.999125Z","shell.execute_reply":"2024-12-01T14:20:27.002321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encoder = LabelEncoder()\n\nfor col in object_columns:\n    train[col] = label_encoder.fit_transform(train[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:27.004278Z","iopub.execute_input":"2024-12-01T14:20:27.004614Z","iopub.status.idle":"2024-12-01T14:20:28.364869Z","shell.execute_reply.started":"2024-12-01T14:20:27.004579Z","shell.execute_reply":"2024-12-01T14:20:28.364179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"le = LabelEncoder()\n\nfor col in object_columns:\n    test[col] = le.fit_transform(test[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:28.365694Z","iopub.execute_input":"2024-12-01T14:20:28.365938Z","iopub.status.idle":"2024-12-01T14:20:29.368647Z","shell.execute_reply.started":"2024-12-01T14:20:28.365914Z","shell.execute_reply":"2024-12-01T14:20:29.367682Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">✂️ Splitting Data</p>","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:29.369811Z","iopub.execute_input":"2024-12-01T14:20:29.370118Z","iopub.status.idle":"2024-12-01T14:20:29.39298Z","shell.execute_reply.started":"2024-12-01T14:20:29.370091Z","shell.execute_reply":"2024-12-01T14:20:29.391986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(columns=['Premium Amount', 'Policy Start Date'])\ny = train['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:29.394007Z","iopub.execute_input":"2024-12-01T14:20:29.394283Z","iopub.status.idle":"2024-12-01T14:20:29.513533Z","shell.execute_reply.started":"2024-12-01T14:20:29.394259Z","shell.execute_reply":"2024-12-01T14:20:29.512793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:29.51449Z","iopub.execute_input":"2024-12-01T14:20:29.514748Z","iopub.status.idle":"2024-12-01T14:20:29.809802Z","shell.execute_reply.started":"2024-12-01T14:20:29.514723Z","shell.execute_reply":"2024-12-01T14:20:29.808847Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">🎛️ CatBoost Training</p>","metadata":{}},{"cell_type":"code","source":"def rmsle(y_true, y_pred):\n    # Calculate RMSLE\n    return np.sqrt(np.mean((np.log1p(y_true) - np.log1p(y_pred)) ** 2))\n\ndef objective(trial):\n    # Hyperparameter search space\n    param = {\n        'iterations': trial.suggest_int('iterations', 100, 1000),  # Search over iterations\n        'learning_rate': trial.suggest_loguniform('learning_rate', 0.01, 0.2),\n        'depth': trial.suggest_int('depth', 4, 12),\n        'task_type': 'GPU'  # Ensure it's set to GPU for GPU-based training\n    }\n    \n    # Initialize the model with the hyperparameters\n    model = CatBoostRegressor(**param)\n    \n    # Fit the model on the training data\n    model.fit(X_train, y_train, early_stopping_rounds=50, verbose=0)\n    \n    # Predict and calculate RMSLE on the test set\n    y_pred = model.predict(X_test)\n    rmsle_value = rmsle(y_test, y_pred)\n    \n    return rmsle_value","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:29.811027Z","iopub.execute_input":"2024-12-01T14:20:29.811318Z","iopub.status.idle":"2024-12-01T14:20:29.817345Z","shell.execute_reply.started":"2024-12-01T14:20:29.811293Z","shell.execute_reply":"2024-12-01T14:20:29.81656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create an Optuna study to optimize the objective function\n#study = optuna.create_study(direction='minimize')\n#study.optimize(objective, n_trials=100)\n\n# Print the best hyperparameters and the corresponding RMSLE\n#print(f'Best hyperparameters: {study.best_params}')\n#print(f'Best RMSLE: {study.best_value}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:29.81855Z","iopub.execute_input":"2024-12-01T14:20:29.819161Z","iopub.status.idle":"2024-12-01T14:20:29.831318Z","shell.execute_reply.started":"2024-12-01T14:20:29.819122Z","shell.execute_reply":"2024-12-01T14:20:29.830568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = CatBoostRegressor(\n    iterations=882, \n    verbose=0, \n    eval_metric='RMSE', \n    task_type='GPU', \n    learning_rate=0.161595895248101,\n    depth=9\n)\n\nmodel.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:29.832113Z","iopub.execute_input":"2024-12-01T14:20:29.832359Z","iopub.status.idle":"2024-12-01T14:20:40.813474Z","shell.execute_reply.started":"2024-12-01T14:20:29.832335Z","shell.execute_reply":"2024-12-01T14:20:40.812501Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color: #1E88E5; font-family: 'Trebuchet MS', sans-serif; font-weight: bold; color: #FFFFFF; font-size: 75%; text-align: center; border: 2px solid #BBDEFB; border-radius: 15px; padding: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);\">🎯 Submission</p>","metadata":{}},{"cell_type":"code","source":"test_pred = model.predict(test)\ntest_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:40.814395Z","iopub.execute_input":"2024-12-01T14:20:40.814987Z","iopub.status.idle":"2024-12-01T14:20:42.147802Z","shell.execute_reply.started":"2024-12-01T14:20:40.814959Z","shell.execute_reply":"2024-12-01T14:20:42.146944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission['Premium Amount'] = test_pred\n\nsubmission = pd.DataFrame({\n    'id': test['id'],  \n    'Premium Amount': test_pred\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:42.148895Z","iopub.execute_input":"2024-12-01T14:20:42.14917Z","iopub.status.idle":"2024-12-01T14:20:42.15749Z","shell.execute_reply.started":"2024-12-01T14:20:42.149145Z","shell.execute_reply":"2024-12-01T14:20:42.156637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:20:42.161543Z","iopub.execute_input":"2024-12-01T14:20:42.161984Z","iopub.status.idle":"2024-12-01T14:20:42.168303Z","shell.execute_reply.started":"2024-12-01T14:20:42.161949Z","shell.execute_reply":"2024-12-01T14:20:42.167443Z"}},"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-01T14:20:42.169283Z","iopub.execute_input":"2024-12-01T14:20:42.16952Z","iopub.status.idle":"2024-12-01T14:20:43.612634Z","shell.execute_reply.started":"2024-12-01T14:20:42.169494Z","shell.execute_reply":"2024-12-01T14:20:43.611774Z"}},"outputs":[],"execution_count":null}]}