{"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":"code","source":"\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:30.360324Z","iopub.execute_input":"2024-12-03T19:09:30.360765Z","iopub.status.idle":"2024-12-03T19:09:30.393546Z","shell.execute_reply.started":"2024-12-03T19:09:30.360693Z","shell.execute_reply":"2024-12-03T19:09:30.392343Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 🔥 **Regression with an Insurance Dataset**  🔥\n<div style=\"border: 2px solid #FF4500; padding: 10px; background-color: #FFF5E1; border-radius: 8px; text-align: center; font-size: 20px; font-weight: bold; color: #FF6347;\">\n    📊 Regression Analysis on Insurance Data 📊\n</div>\n\n---\n<div style=\"text-align: center;\">\n        <img src=\"https://cdn.prod.website-files.com/63c856d889d2ff1bd85e9773/665bc55f71bbadf62b154985_medical.jpg\" alt=\"Insurance Dataset Image\" style=\"width: 100%; max-width: 100%; height: auto;\"/>\n\n</div>\n\n<div style=\"text-align: right; font-size: 12px; color: gray; padding-top: 20px;\">\n    Created by <strong>Nouri RIDA</strong> | \n    <a href='https://www.linkedin.com/in/votre-profil/' target='_blank'>LinkedIn</a>\n</div>\r\n","metadata":{}},{"cell_type":"markdown","source":"# **Importing Modules, Building Classes and Reading CSV :**","metadata":{}},{"cell_type":"markdown","source":"**Importing necessary libraries and Models :**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split, cross_val_score, KFold\nfrom sklearn.metrics import mean_squared_log_error, make_scorer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.impute import SimpleImputer\n\nfrom xgboost import XGBRegressor\nimport lightgbm as lgb\nfrom sklearn.neighbors import KNeighborsRegressor\nimport catboost as cb\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:30.395563Z","iopub.execute_input":"2024-12-03T19:09:30.395911Z","iopub.status.idle":"2024-12-03T19:09:33.760922Z","shell.execute_reply.started":"2024-12-03T19:09:30.395878Z","shell.execute_reply":"2024-12-03T19:09:33.759848Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Reading datasets**","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest_data = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:33.762182Z","iopub.execute_input":"2024-12-03T19:09:33.762686Z","iopub.status.idle":"2024-12-03T19:09:44.871228Z","shell.execute_reply.started":"2024-12-03T19:09:33.762652Z","shell.execute_reply":"2024-12-03T19:09:44.86989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:44.872663Z","iopub.execute_input":"2024-12-03T19:09:44.873044Z","iopub.status.idle":"2024-12-03T19:09:44.878874Z","shell.execute_reply.started":"2024-12-03T19:09:44.873009Z","shell.execute_reply":"2024-12-03T19:09:44.877417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:44.881844Z","iopub.execute_input":"2024-12-03T19:09:44.882238Z","iopub.status.idle":"2024-12-03T19:09:44.928939Z","shell.execute_reply.started":"2024-12-03T19:09:44.882194Z","shell.execute_reply":"2024-12-03T19:09:44.927376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.tail(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:44.930874Z","iopub.execute_input":"2024-12-03T19:09:44.931416Z","iopub.status.idle":"2024-12-03T19:09:44.964997Z","shell.execute_reply.started":"2024-12-03T19:09:44.931355Z","shell.execute_reply":"2024-12-03T19:09:44.96366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:44.966542Z","iopub.execute_input":"2024-12-03T19:09:44.967005Z","iopub.status.idle":"2024-12-03T19:09:44.995157Z","shell.execute_reply.started":"2024-12-03T19:09:44.966955Z","shell.execute_reply":"2024-12-03T19:09:44.993835Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Exploratory Data Analysis (EDA)**","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:44.996251Z","iopub.execute_input":"2024-12-03T19:09:44.996673Z","iopub.status.idle":"2024-12-03T19:09:45.703341Z","shell.execute_reply.started":"2024-12-03T19:09:44.996627Z","shell.execute_reply":"2024-12-03T19:09:45.701987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:45.704812Z","iopub.execute_input":"2024-12-03T19:09:45.705167Z","iopub.status.idle":"2024-12-03T19:09:45.715445Z","shell.execute_reply.started":"2024-12-03T19:09:45.705133Z","shell.execute_reply":"2024-12-03T19:09:45.713985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:45.717059Z","iopub.execute_input":"2024-12-03T19:09:45.717526Z","iopub.status.idle":"2024-12-03T19:09:46.450441Z","shell.execute_reply.started":"2024-12-03T19:09:45.717478Z","shell.execute_reply":"2024-12-03T19:09:46.449344Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Data Shape :**","metadata":{}},{"cell_type":"code","source":"train_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:46.451853Z","iopub.execute_input":"2024-12-03T19:09:46.45228Z","iopub.status.idle":"2024-12-03T19:09:46.460692Z","shell.execute_reply.started":"2024-12-03T19:09:46.452234Z","shell.execute_reply":"2024-12-03T19:09:46.459388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:46.462203Z","iopub.execute_input":"2024-12-03T19:09:46.462572Z","iopub.status.idle":"2024-12-03T19:09:46.478079Z","shell.execute_reply.started":"2024-12-03T19:09:46.462535Z","shell.execute_reply":"2024-12-03T19:09:46.476827Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Checking data types :**","metadata":{}},{"cell_type":"code","source":"train_data.dtypes.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:46.479438Z","iopub.execute_input":"2024-12-03T19:09:46.479812Z","iopub.status.idle":"2024-12-03T19:09:46.49349Z","shell.execute_reply.started":"2024-12-03T19:09:46.479767Z","shell.execute_reply":"2024-12-03T19:09:46.491929Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Checking for duplicates values :**","metadata":{}},{"cell_type":"code","source":"train_data.duplicated().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:46.499094Z","iopub.execute_input":"2024-12-03T19:09:46.499617Z","iopub.status.idle":"2024-12-03T19:09:48.248377Z","shell.execute_reply.started":"2024-12-03T19:09:46.49958Z","shell.execute_reply":"2024-12-03T19:09:48.24689Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Checking for missing values :**","metadata":{}},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:48.249512Z","iopub.execute_input":"2024-12-03T19:09:48.249838Z","iopub.status.idle":"2024-12-03T19:09:48.896213Z","shell.execute_reply.started":"2024-12-03T19:09:48.249797Z","shell.execute_reply":"2024-12-03T19:09:48.895064Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Numerical features :**","metadata":{}},{"cell_type":"code","source":"numerical_data = train_data.select_dtypes(include=['float64', 'int64'])\nnumerical_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:48.897704Z","iopub.execute_input":"2024-12-03T19:09:48.898067Z","iopub.status.idle":"2024-12-03T19:09:48.938539Z","shell.execute_reply.started":"2024-12-03T19:09:48.898029Z","shell.execute_reply":"2024-12-03T19:09:48.93746Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Categorecal features :**","metadata":{}},{"cell_type":"code","source":"categorical_data = train_data.select_dtypes(exclude=['float64', 'int64'])\ncategorical_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:48.939877Z","iopub.execute_input":"2024-12-03T19:09:48.940394Z","iopub.status.idle":"2024-12-03T19:09:49.078457Z","shell.execute_reply.started":"2024-12-03T19:09:48.940357Z","shell.execute_reply":"2024-12-03T19:09:49.077118Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Visualizing missing values :**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.heatmap(train_data.isnull(), cbar=False, cmap='viridis', yticklabels=False)\nplt.title('Missing Values Heatmap')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:09:49.080201Z","iopub.execute_input":"2024-12-03T19:09:49.0806Z","iopub.status.idle":"2024-12-03T19:10:14.386993Z","shell.execute_reply.started":"2024-12-03T19:09:49.080566Z","shell.execute_reply":"2024-12-03T19:10:14.3859Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Improved correlation matrix heatmap :**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\nsns.heatmap(numerical_data.corr(), annot=True, fmt='.2f', cmap='coolwarm', linewidths=0.5)\nplt.title('Correlation Heatmap')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:14.388477Z","iopub.execute_input":"2024-12-03T19:10:14.388847Z","iopub.status.idle":"2024-12-03T19:10:15.433935Z","shell.execute_reply.started":"2024-12-03T19:10:14.388812Z","shell.execute_reply":"2024-12-03T19:10:15.432739Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Plotting histograms for all numerical features :**","metadata":{}},{"cell_type":"code","source":"numerical_data.hist(bins=30, figsize=(15, 10), edgecolor='black')\nplt.suptitle('Histograms of Numerical Features', fontsize=16, y=1.02)  # Adjust the y position of the title\nplt.tight_layout(rect=[0, 0, 1, 0.96])  # Leave space for the title\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:15.435535Z","iopub.execute_input":"2024-12-03T19:10:15.436112Z","iopub.status.idle":"2024-12-03T19:10:18.505983Z","shell.execute_reply.started":"2024-12-03T19:10:15.436076Z","shell.execute_reply":"2024-12-03T19:10:18.504872Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Distribution of the target variable with and without log transformation :**","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(12, 6))\nsns.histplot(train_data['Premium Amount'], kde=True, bins=30, ax=axes[0])\naxes[0].set_title('Original Distribution')\n\nsns.histplot(np.log1p(train_data['Premium Amount']), kde=True, bins=30, ax=axes[1], color='orange')\naxes[1].set_title('Log-Transformed Distribution')\n\nplt.suptitle('Premium Amount Distribution', fontsize=16)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:18.507264Z","iopub.execute_input":"2024-12-03T19:10:18.507627Z","iopub.status.idle":"2024-12-03T19:10:30.42552Z","shell.execute_reply.started":"2024-12-03T19:10:18.507594Z","shell.execute_reply":"2024-12-03T19:10:30.424415Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Plotting relationship between variables :**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 7))\n\nplt1 = sns.scatterplot(data=train_data, x='Age', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Gender', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:30.427092Z","iopub.execute_input":"2024-12-03T19:10:30.427544Z","iopub.status.idle":"2024-12-03T19:10:36.303613Z","shell.execute_reply.started":"2024-12-03T19:10:30.427497Z","shell.execute_reply":"2024-12-03T19:10:36.302474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 7))\n\nplt1 = sns.boxplot(data=train_data, x='Number of Dependents', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Marital Status', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:36.30494Z","iopub.execute_input":"2024-12-03T19:10:36.305266Z","iopub.status.idle":"2024-12-03T19:10:37.834262Z","shell.execute_reply.started":"2024-12-03T19:10:36.305234Z","shell.execute_reply":"2024-12-03T19:10:37.832981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 7))\n\nplt1 = sns.boxplot(data=train_data, x='Education Level', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Occupation', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:37.835636Z","iopub.execute_input":"2024-12-03T19:10:37.835995Z","iopub.status.idle":"2024-12-03T19:10:39.672856Z","shell.execute_reply.started":"2024-12-03T19:10:37.835962Z","shell.execute_reply":"2024-12-03T19:10:39.671487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 7))\n\nplt1 = sns.scatterplot(data=train_data, x='Health Score', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Location', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:39.673962Z","iopub.execute_input":"2024-12-03T19:10:39.674322Z","iopub.status.idle":"2024-12-03T19:10:45.287533Z","shell.execute_reply.started":"2024-12-03T19:10:39.674266Z","shell.execute_reply":"2024-12-03T19:10:45.286226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(15, 7))\n\nplt1 = sns.boxplot(data=train_data, x='Previous Claims', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Policy Type', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:45.289356Z","iopub.execute_input":"2024-12-03T19:10:45.289704Z","iopub.status.idle":"2024-12-03T19:10:46.982497Z","shell.execute_reply.started":"2024-12-03T19:10:45.289674Z","shell.execute_reply":"2024-12-03T19:10:46.981238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(18, 7))\n\nplt1 = sns.boxplot(data=train_data, x='Customer Feedback', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Insurance Duration', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:46.983903Z","iopub.execute_input":"2024-12-03T19:10:46.984251Z","iopub.status.idle":"2024-12-03T19:10:48.546122Z","shell.execute_reply.started":"2024-12-03T19:10:46.984218Z","shell.execute_reply":"2024-12-03T19:10:48.544534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(18, 7))\n\nplt1 = sns.scatterplot(data=train_data, x='Annual Income', y='Premium Amount', ax=ax[0])\nplt2 = sns.boxplot(data=train_data, x='Property Type', y='Premium Amount', ax=ax[1]);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:48.54801Z","iopub.execute_input":"2024-12-03T19:10:48.548519Z","iopub.status.idle":"2024-12-03T19:10:54.405585Z","shell.execute_reply.started":"2024-12-03T19:10:48.548469Z","shell.execute_reply":"2024-12-03T19:10:54.404235Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Data Preprocessing**","metadata":{}},{"cell_type":"markdown","source":"**-Splitting features and target :**","metadata":{}},{"cell_type":"code","source":"X = train_data.drop(['id', 'Premium Amount'], axis=1)\ny = train_data['Premium Amount']\ntest_ids = test_data['id']\nX_test = test_data.drop('id', axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.40725Z","iopub.execute_input":"2024-12-03T19:10:54.407811Z","iopub.status.idle":"2024-12-03T19:10:54.682Z","shell.execute_reply.started":"2024-12-03T19:10:54.407749Z","shell.execute_reply":"2024-12-03T19:10:54.680704Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Identifying numerical and categorical columns :**","metadata":{}},{"cell_type":"code","source":"numerical_cols = X.select_dtypes(include=['float64', 'int64']).columns\ncategorical_cols = X.select_dtypes(include=['object']).columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.683757Z","iopub.execute_input":"2024-12-03T19:10:54.684143Z","iopub.status.idle":"2024-12-03T19:10:54.873612Z","shell.execute_reply.started":"2024-12-03T19:10:54.684107Z","shell.execute_reply":"2024-12-03T19:10:54.872374Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Preprocessing for numerical and categorical data :**","metadata":{}},{"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='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.875027Z","iopub.execute_input":"2024-12-03T19:10:54.875378Z","iopub.status.idle":"2024-12-03T19:10:54.881961Z","shell.execute_reply.started":"2024-12-03T19:10:54.875347Z","shell.execute_reply":"2024-12-03T19:10:54.880653Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Combining preprocessors :**","metadata":{}},{"cell_type":"code","source":"preprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numerical_transformer, numerical_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.883475Z","iopub.execute_input":"2024-12-03T19:10:54.883896Z","iopub.status.idle":"2024-12-03T19:10:54.896878Z","shell.execute_reply.started":"2024-12-03T19:10:54.883861Z","shell.execute_reply":"2024-12-03T19:10:54.895871Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Creating cross-validation strategy :**","metadata":{}},{"cell_type":"code","source":"kf = KFold(n_splits=5, shuffle=True, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.898318Z","iopub.execute_input":"2024-12-03T19:10:54.898668Z","iopub.status.idle":"2024-12-03T19:10:54.914099Z","shell.execute_reply.started":"2024-12-03T19:10:54.898635Z","shell.execute_reply":"2024-12-03T19:10:54.912954Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Model Building and Training**","metadata":{}},{"cell_type":"markdown","source":"**Defining models**","metadata":{}},{"cell_type":"code","source":"models = {\n    'LightGBM': lgb.LGBMRegressor(n_estimators=50, learning_rate=0.1, random_state=42, force_row_wise=True),\n    'XGBoost': XGBRegressor(n_estimators=50, learning_rate=0.1, random_state=42),\n    'CatBoost': cb.CatBoostRegressor(iterations=50, learning_rate=0.1, depth=5, random_state=42, verbose=0)\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.915635Z","iopub.execute_input":"2024-12-03T19:10:54.916211Z","iopub.status.idle":"2024-12-03T19:10:54.933106Z","shell.execute_reply.started":"2024-12-03T19:10:54.916162Z","shell.execute_reply":"2024-12-03T19:10:54.931796Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Custom scorer for RMSLE :**","metadata":{}},{"cell_type":"code","source":"rmsle_scorer = make_scorer(mean_squared_log_error, greater_is_better=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.934445Z","iopub.execute_input":"2024-12-03T19:10:54.934845Z","iopub.status.idle":"2024-12-03T19:10:54.946915Z","shell.execute_reply.started":"2024-12-03T19:10:54.9348Z","shell.execute_reply":"2024-12-03T19:10:54.945524Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Evaluating models using cross-validation :**","metadata":{}},{"cell_type":"code","source":"results = {}\nfor name, model in models.items():\n    pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('model', model)])\n    scores = cross_val_score(pipeline, X, y, cv=kf, scoring=rmsle_scorer)\n    results[name] = -np.mean(scores)\n    print(f'{name}: RMSLE = {results[name]:.5f}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:10:54.948659Z","iopub.execute_input":"2024-12-03T19:10:54.94912Z","iopub.status.idle":"2024-12-03T19:32:12.994139Z","shell.execute_reply.started":"2024-12-03T19:10:54.949071Z","shell.execute_reply":"2024-12-03T19:32:12.992751Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Final Model Training and Predictions**","metadata":{}},{"cell_type":"markdown","source":"**-Choosing the best model :**","metadata":{}},{"cell_type":"code","source":"best_model_name = min(results, key=results.get)\nprint(f\"Best model: {best_model_name}\")\nbest_model = models[best_model_name]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:32:12.995763Z","iopub.execute_input":"2024-12-03T19:32:12.996271Z","iopub.status.idle":"2024-12-03T19:32:13.002818Z","shell.execute_reply.started":"2024-12-03T19:32:12.996216Z","shell.execute_reply":"2024-12-03T19:32:13.0017Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Training best model on entire dataset :**","metadata":{}},{"cell_type":"code","source":"final_pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('model', best_model)])\nfinal_pipeline.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:32:13.004272Z","iopub.execute_input":"2024-12-03T19:32:13.00473Z","iopub.status.idle":"2024-12-03T19:32:36.499207Z","shell.execute_reply.started":"2024-12-03T19:32:13.004683Z","shell.execute_reply":"2024-12-03T19:32:36.497723Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**-Making predictions :**","metadata":{}},{"cell_type":"code","source":"predictions = final_pipeline.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:32:36.500625Z","iopub.execute_input":"2024-12-03T19:32:36.500952Z","iopub.status.idle":"2024-12-03T19:32:55.103751Z","shell.execute_reply.started":"2024-12-03T19:32:36.500922Z","shell.execute_reply":"2024-12-03T19:32:55.1024Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Creating Submission File**","metadata":{}},{"cell_type":"code","source":"# Ensuring no negative predictions\npredictions = np.maximum(predictions, 0)\n\n# Preparing the submission\nsubmission = pd.DataFrame({'id': test_ids, 'Premium Amount': predictions})\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission file saved!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:32:55.105548Z","iopub.execute_input":"2024-12-03T19:32:55.106128Z","iopub.status.idle":"2024-12-03T19:32:56.94956Z","shell.execute_reply.started":"2024-12-03T19:32:55.106078Z","shell.execute_reply":"2024-12-03T19:32:56.947982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T19:32:56.955164Z","iopub.execute_input":"2024-12-03T19:32:56.95558Z","iopub.status.idle":"2024-12-03T19:32:56.968217Z","shell.execute_reply.started":"2024-12-03T19:32:56.955545Z","shell.execute_reply":"2024-12-03T19:32:56.966944Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"text-align: center;\">\n    <img src=\"https://blog.getcompass.ai/content/images/size/w1384/format/webp/2023/07/Best-Appreciation-Thank-You-Message.webp\" alt=\"Insurance Dataset Image\" style=\"width: 100%; max-width: 100%; height: auto;\"/>\n</div>","metadata":{}}]}