{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":115000,"databundleVersionId":13897013,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#  Objective  \n\nThis notebook aims to build a complete machine learning pipeline for the **Premium Prediction** dataset.  \n\n\nWe’ll go step by step — from data loading, preprocessing (numeric and categorical), feature engineering, and model training to final submission — keeping everything clean, modular, and function-based.  ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-10T15:35:29.968192Z","iopub.execute_input":"2025-10-10T15:35:29.968485Z","iopub.status.idle":"2025-10-10T15:35:29.975336Z","shell.execute_reply.started":"2025-10-10T15:35:29.968464Z","shell.execute_reply":"2025-10-10T15:35:29.974508Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# some basic libraries\n\nimport pandas as pd\nimport numpy as np \nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings('ignore')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:44:52.333618Z","iopub.execute_input":"2025-10-11T01:44:52.333988Z","iopub.status.idle":"2025-10-11T01:44:53.733595Z","shell.execute_reply.started":"2025-10-11T01:44:52.333961Z","shell.execute_reply":"2025-10-11T01:44:53.732589Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#  Load and Overview the Datasets","metadata":{}},{"cell_type":"code","source":"# loading the dataset\n\ntrain_dataset=pd.read_csv('/kaggle/input/carnival-risk-analytics-challenge/train.csv')\ntest_dataset=pd.read_csv('/kaggle/input/carnival-risk-analytics-challenge/test.csv')\n\nprint(\"✅ Data Loaded Successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:44:53.735005Z","iopub.execute_input":"2025-10-11T01:44:53.735571Z","iopub.status.idle":"2025-10-11T01:45:01.706945Z","shell.execute_reply.started":"2025-10-11T01:44:53.735536Z","shell.execute_reply":"2025-10-11T01:45:01.706050Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:01.708297Z","iopub.execute_input":"2025-10-11T01:45:01.708639Z","iopub.status.idle":"2025-10-11T01:45:01.744457Z","shell.execute_reply.started":"2025-10-11T01:45:01.708617Z","shell.execute_reply":"2025-10-11T01:45:01.743639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('trainig data shape: ',train_dataset.shape)\nprint('test data shape: ',test_dataset.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:01.745439Z","iopub.execute_input":"2025-10-11T01:45:01.745710Z","iopub.status.idle":"2025-10-11T01:45:01.751925Z","shell.execute_reply.started":"2025-10-11T01:45:01.745688Z","shell.execute_reply":"2025-10-11T01:45:01.750691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#training dataset null values\n\ntrain_dataset.isnull().sum() ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:01.790221Z","iopub.execute_input":"2025-10-11T01:45:01.790467Z","iopub.status.idle":"2025-10-11T01:45:02.206212Z","shell.execute_reply.started":"2025-10-11T01:45:01.790448Z","shell.execute_reply":"2025-10-11T01:45:02.204033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test dataset null values\n\ntest_dataset.isnull().sum() ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:02.207274Z","iopub.execute_input":"2025-10-11T01:45:02.207687Z","iopub.status.idle":"2025-10-11T01:45:02.487453Z","shell.execute_reply.started":"2025-10-11T01:45:02.207663Z","shell.execute_reply":"2025-10-11T01:45:02.486313Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#  Some Important Variables","metadata":{}},{"cell_type":"code","source":"X=train_dataset.iloc[:,1:-1] #all features except 'id' and 'premium amount'\ny=train_dataset.iloc[:,-1] # target feature\nids=test_dataset.iloc[:,0] # 'id' column\nX_test=test_dataset.iloc[:,1:] # all columns except 'id'\n\ncolumns=X.columns\ncolumns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:30:54.969319Z","iopub.execute_input":"2025-10-11T02:30:54.969697Z","iopub.status.idle":"2025-10-11T02:30:55.535132Z","shell.execute_reply.started":"2025-10-11T02:30:54.969674Z","shell.execute_reply":"2025-10-11T02:30:55.533908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# identifying categorical and numerical columns for preprocessing\n\nnum_cols=[col for col in columns if X[col].dtypes in ('int64','float64')]\ncat_cols=[col for col in columns if col not in num_cols]\nprint(f'numercial columns: {num_cols}')\nprint()\nprint(f'categorical_columns: {cat_cols}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:30:56.445256Z","iopub.execute_input":"2025-10-11T02:30:56.445683Z","iopub.status.idle":"2025-10-11T02:30:56.454013Z","shell.execute_reply.started":"2025-10-11T02:30:56.445649Z","shell.execute_reply":"2025-10-11T02:30:56.452925Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Missing Value Handling ","metadata":{}},{"cell_type":"code","source":"#calculating missing data percentage for training dataset\n\nmissing_values=X.isnull().sum()\nmissing_percentage= (missing_values/len(X))*100\nmissing_percentage.round(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:18.392438Z","iopub.execute_input":"2025-10-11T01:45:18.392772Z","iopub.status.idle":"2025-10-11T01:45:18.807067Z","shell.execute_reply.started":"2025-10-11T01:45:18.392748Z","shell.execute_reply":"2025-10-11T01:45:18.805534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# calculating missing data percentage for test dataset\n\nmissing_values_test=X_test.isnull().sum()\nmissing_percentage_test= (missing_values_test/len(X))*100\nmissing_percentage_test.round(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:18.813610Z","iopub.execute_input":"2025-10-11T01:45:18.813903Z","iopub.status.idle":"2025-10-11T01:45:19.084897Z","shell.execute_reply.started":"2025-10-11T01:45:18.813884Z","shell.execute_reply":"2025-10-11T01:45:19.084162Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Observation:\n\n'previous claims', 'occupation', 'credit score'   require extra attention on missing data","metadata":{}},{"cell_type":"code","source":"# Categorize columns by missing percentage\n\nlow_missing=[] # <=5%\nmoderate_missing=[] # >5% and <=15%\nhigh_missing=[] # >15%\n\nfor i in range(18):\n    if(missing_percentage[i]<=5):\n        low_missing.append(columns[i])\n    elif(missing_percentage[i]>5 and missing_percentage[i]<=15):\n        moderate_missing.append(columns[i])\n    else:\n        high_missing.append(columns[i])\n\nprint(\"\\nLow missing:\", low_missing)\nprint(\"\\nModerate missing:\", moderate_missing)\nprint(\"\\nHigh missing:\", high_missing)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:19.462546Z","iopub.execute_input":"2025-10-11T01:45:19.462822Z","iopub.status.idle":"2025-10-11T01:45:19.470435Z","shell.execute_reply.started":"2025-10-11T01:45:19.462802Z","shell.execute_reply":"2025-10-11T01:45:19.469463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# code for handeling numerical missing values\n\ndef handle_numeric_missing(train_df,test_df):\n\n    #low missing -> mean\n    for i in low_missing:\n        if ( i in num_cols):\n            temp_mean=train_df[i].mean()\n            train_df[i].fillna(temp_mean,inplace =True)\n            test_df[i].fillna(temp_mean,inplace=True)\n\n    # moderate missing -> median\n    for i in moderate_missing:\n        if ( i in num_cols):\n            temp_median=train_df[i].median()\n            train_df[i].fillna(temp_median,inplace=True)\n            test_df[i].fillna(temp_median,inplace=True)\n\n    #high missing -> drop\n    train_df.drop(columns=high_missing,inplace=True,errors='ignore')\n    test_df.drop(columns=high_missing,inplace=True,errors='ignore')\n    \n    return train_df,test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:45:58.123780Z","iopub.execute_input":"2025-10-11T01:45:58.124151Z","iopub.status.idle":"2025-10-11T01:45:58.130819Z","shell.execute_reply.started":"2025-10-11T01:45:58.124121Z","shell.execute_reply":"2025-10-11T01:45:58.129578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#code for handeling categorical missing values\n\ndef handle_categorical_missing(train_df,test_df):\n\n    for i in low_missing + moderate_missing:\n        if i in cat_cols:\n            temp_mode=train_df[i].mode()[0]\n            train_df[i].fillna(temp_mode, inplace= True)\n            test_df[i].fillna(temp_mode, inplace= True)\n    \n    #train_df[col].fillna('Unknown', inplace=True)\n    #test_df[col].fillna('Unknown', inplace=True)\n    return train_df,test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:46:10.027632Z","iopub.execute_input":"2025-10-11T01:46:10.027937Z","iopub.status.idle":"2025-10-11T01:46:10.033378Z","shell.execute_reply.started":"2025-10-11T01:46:10.027912Z","shell.execute_reply":"2025-10-11T01:46:10.032151Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 🛠 Feature Engineering\n\nAfter inspecting the dataset, we observed that some features, although independent at first glance, have relationships that could be useful for the model. To help the model capture these patterns, we will create additional features:\n\n1. **Income_per_Age** → This feature represents the ratio of **Annual Income** to **Age**, which can help the model understand income relative to age.  \n\n2. **Age_Health** → An interaction term calculated as **Age × Health Score**, capturing the combined effect of age and health on the target variable.  \n\n3. **Policy Start Date features** → The datetime column is decomposed into multiple numeric features such as **Year, Month, Day, Weekday, Hour, Minute, and Second** to provide the model with granular temporal information.\n","metadata":{}},{"cell_type":"code","source":"def feature_engineering_numeric(train_df, test_df):\n    \"\"\"\n    Adds numeric interaction features to both train and test datasets.\n    \"\"\"\n    for df in [train_df, test_df]:\n        if 'Annual Income' in df.columns and 'Age' in df.columns:\n            df['Income_per_Age'] = df['Annual Income'] / df['Age']\n        if 'Age' in df.columns and 'Health_score' in df.columns:\n            df['Age_Health'] = df['Age'] * df['Health_score']\n\n    return train_df, test_df\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:46:22.522925Z","iopub.execute_input":"2025-10-11T01:46:22.524264Z","iopub.status.idle":"2025-10-11T01:46:22.530019Z","shell.execute_reply.started":"2025-10-11T01:46:22.524214Z","shell.execute_reply":"2025-10-11T01:46:22.529224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def process_datetime(train_df,test_df):\n    # Convert to datetime\n    col = 'Policy Start Date'\n    train_df[col] = pd.to_datetime(train_df[col])\n    test_df[col] = pd.to_datetime(test_df[col])\n\n    for df in [train_df, test_df]:\n        df[f'{col}_Year'] = df[col].dt.year\n        df[f'{col}_Month'] = df[col].dt.month\n        df[f'{col}_Day'] = df[col].dt.day\n        df[f'{col}_Weekday'] = df[col].dt.weekday\n        df[f'{col}_Hour'] = df[col].dt.hour\n        df[f'{col}_Minute'] = df[col].dt.minute\n        df[f'{col}_Second'] = df[col].dt.second\n\n    # Drop original datetime column\n    train_df.drop(columns=[col], inplace=True)\n    test_df.drop(columns=[col], inplace=True)\n\n    return train_df, test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:46:29.742394Z","iopub.execute_input":"2025-10-11T01:46:29.742710Z","iopub.status.idle":"2025-10-11T01:46:29.749439Z","shell.execute_reply.started":"2025-10-11T01:46:29.742689Z","shell.execute_reply":"2025-10-11T01:46:29.748442Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Checking Skewness","metadata":{}},{"cell_type":"code","source":"# Skewness of numerical columns\n\nskew_values= train_dataset[num_cols].skew()\nprint('skewness of numerical columns: ')\nprint(skew_values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:46:32.922397Z","iopub.execute_input":"2025-10-11T01:46:32.922710Z","iopub.status.idle":"2025-10-11T01:46:33.053888Z","shell.execute_reply.started":"2025-10-11T01:46:32.922688Z","shell.execute_reply":"2025-10-11T01:46:33.052730Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### observation:\n\n'Annual Income', 'Previous Claims', 'Vehicle Age'-----> require attention regarding skewness","metadata":{}},{"cell_type":"markdown","source":"## Identify and Visualize Outliers","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18,10))\n\nfor i,col in enumerate(num_cols):\n    plt.subplot(2,4,i+1)\n    sns.boxplot(y=train_dataset[col])\n    plt.title(f'Boxplot of {col}')\n    plt.tight_layout()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:49:46.756293Z","iopub.execute_input":"2025-10-11T01:49:46.756763Z","iopub.status.idle":"2025-10-11T01:49:49.138408Z","shell.execute_reply.started":"2025-10-11T01:49:46.756731Z","shell.execute_reply":"2025-10-11T01:49:49.137170Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Observation: \n\n'Annual Income', 'Previous Claims' require oulier treatment","metadata":{}},{"cell_type":"code","source":"def compute_outlier_bounds(df,lower_percentile=0.01,upper_percentile=0.99):\n\n    lower_bounds=df.quantile(lower_percentile)\n    upper_bounds= df.quantile(upper_percentile)\n    \n    return lower_bounds, upper_bounds\n\n\ndef cap_outliers(train_df, test_df, lower_bounds, upper_bounds):\n    \"\"\"\n    Caps outliers in numeric columns for both train and test datasets using provided bounds.\n    \"\"\"\n    train_df_capped = train_df.copy()\n    test_df_capped = test_df.copy()\n\n    for col in lower_bounds.index:  # assuming lower_bounds and upper_bounds are pandas Series\n        if col in train_df_capped.columns:\n            train_df_capped[col] = np.clip(train_df_capped[col], lower_bounds[col], upper_bounds[col])\n        if col in test_df_capped.columns:\n            test_df_capped[col] = np.clip(test_df_capped[col], lower_bounds[col], upper_bounds[col])\n\n    return train_df_capped, test_df_capped\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:50:04.710935Z","iopub.execute_input":"2025-10-11T01:50:04.712427Z","iopub.status.idle":"2025-10-11T01:50:04.722397Z","shell.execute_reply.started":"2025-10-11T01:50:04.712388Z","shell.execute_reply":"2025-10-11T01:50:04.719830Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Preprocessing Pipeline\n","metadata":{}},{"cell_type":"code","source":"def preprocess(X,X_test):\n    \n    X,X_test=handle_numeric_missing(X,X_test)\n    X,X_test=handle_categorical_missing(X,X_test)\n    X,X_test=feature_engineering_numeric(X,X_test)\n    X,X_test= process_datetime(X,X_test)\n\n    global columns,num_cols,car_cols\n    columns=X.columns\n    \n    num_cols=[col for col in columns if X[col].dtypes in ('int64','float64')]\n    cat_cols=[col for col in columns if col not in num_cols]\n\n    lower_bounds,upper_bounds= compute_outlier_bounds(X[num_cols])\n    X,X_test=cap_outliers(X,X_test,lower_bounds,upper_bounds)\n\n    return X,X_test, cat_cols,num_cols,lower_bounds,upper_bounds\n\nX,X_test,cat_cols,num_cols,lower_bounds,upper_bounds=preprocess(X,X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:31:04.698196Z","iopub.execute_input":"2025-10-11T02:31:04.698513Z","iopub.status.idle":"2025-10-11T02:31:08.326725Z","shell.execute_reply.started":"2025-10-11T02:31:04.698484Z","shell.execute_reply":"2025-10-11T02:31:08.325398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Libraries for Preprocessing\n\nfrom sklearn.pipeline import Pipeline       \nfrom sklearn.compose import ColumnTransformer           \nfrom sklearn.preprocessing import StandardScaler, PowerTransformer ,OrdinalEncoder","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:52:07.478159Z","iopub.execute_input":"2025-10-11T01:52:07.478982Z","iopub.status.idle":"2025-10-11T01:52:07.484196Z","shell.execute_reply.started":"2025-10-11T01:52:07.478940Z","shell.execute_reply":"2025-10-11T01:52:07.483065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Numeric Columns Pipeline\nfrom sklearn.impute import SimpleImputer\nnum_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='median'))      # gonna fill with mean if any nan value remains\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:51:30.725458Z","iopub.execute_input":"2025-10-11T01:51:30.725774Z","iopub.status.idle":"2025-10-11T01:51:31.163562Z","shell.execute_reply.started":"2025-10-11T01:51:30.725755Z","shell.execute_reply":"2025-10-11T01:51:31.162456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Categorical Columns Pipeline\n\ncat_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='constant', fill_value='unknown')),      # # gonna fill with 'unknown' if any nan value remains\n    ('ordinal', OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1))        # ordinal encode categorical features\n])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:52:12.794973Z","iopub.execute_input":"2025-10-11T01:52:12.796459Z","iopub.status.idle":"2025-10-11T01:52:12.801781Z","shell.execute_reply.started":"2025-10-11T01:52:12.796424Z","shell.execute_reply":"2025-10-11T01:52:12.800843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Combine Numeric & Categorical Pipelines\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', num_transformer, num_cols),    # Apply numeric pipeline to numeric columns\n        ('cat', cat_transformer, cat_cols)     # Apply categorical pipeline to categorical columns\n    ]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T01:52:28.513737Z","iopub.execute_input":"2025-10-11T01:52:28.514763Z","iopub.status.idle":"2025-10-11T01:52:28.519878Z","shell.execute_reply.started":"2025-10-11T01:52:28.514730Z","shell.execute_reply":"2025-10-11T01:52:28.518657Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# XGBRegressor Model\n","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBRegressor ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:12:27.022942Z","iopub.execute_input":"2025-10-11T03:12:27.023880Z","iopub.status.idle":"2025-10-11T03:12:27.028630Z","shell.execute_reply.started":"2025-10-11T03:12:27.023847Z","shell.execute_reply":"2025-10-11T03:12:27.027367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Pipeline : preprocessing + Random Forest Regressor\n\nxgb_pipeline = Pipeline([\n    ('preprocessing', preprocessor),\n    ('xgb', XGBRegressor(\n    n_estimators=400,\n    random_state=42,\n    n_jobs=-1,))\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:12:28.241942Z","iopub.execute_input":"2025-10-11T03:12:28.242285Z","iopub.status.idle":"2025-10-11T03:12:28.247700Z","shell.execute_reply.started":"2025-10-11T03:12:28.242259Z","shell.execute_reply":"2025-10-11T03:12:28.246688Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Finding the best Parameter for our model","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import RandomizedSearchCV, KFold \nfrom scipy.stats import randint, uniform\n\n# --- Define the Hyperparameter Search Space ---\nparam_dist = {\n    'xgb__max_depth': randint(5, 8),\n    'xgb__subsample': uniform(0.6, 0.4),\n    'xgb__colsample_bytree': uniform(0.6, 0.4),\n    'xgb__reg_lambda': uniform(0.5, 2.5),\n    'xgb__learning_rate': [0.01, 0.02, 0.05],\n}\n\n# --- Prepare KFold for the Search ---\nkf = KFold(n_splits=3, shuffle=True, random_state=42) \n\n# --- Set up and Run a Much Faster RandomizedSearchCV ---\nrs_cv = RandomizedSearchCV(\n    estimator=xgb_pipeline,\n    param_distributions=param_dist,\n    n_iter=10,         \\\n    cv=kf,\n    scoring='neg_root_mean_squared_error',\n    verbose=2,\n    n_jobs=-1,\n    random_state=42\n)\n\nprint(\"Starting FAST Randomized Search for best hyperparameters...\")\n\nrs_cv.fit(X, y)\n\n# --- Print the Best Results ---\nprint(\"\\n===== Randomized Search Complete =====\")\nprint(f\"Best parameters found: {rs_cv.best_params_}\")\nbest_rmse = -rs_cv.best_score_\nprint(f\"Best cross-validated RMSE: {best_rmse:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:01:29.670674Z","iopub.execute_input":"2025-10-11T03:01:29.671001Z","iopub.status.idle":"2025-10-11T03:07:57.014145Z","shell.execute_reply.started":"2025-10-11T03:01:29.670975Z","shell.execute_reply":"2025-10-11T03:07:57.013399Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## From the previous results redifinig the model with best parameters","metadata":{}},{"cell_type":"code","source":"final_params = {\n    'colsample_bytree': 0.7801997007878172,\n    'learning_rate': 0.02,\n    'max_depth': 5,\n    'reg_lambda': 2.9140800826863984,\n    'subsample': 0.9233589392465844,\n    'n_estimators': 500,  # A reasonable number, since we don't have early stopping here\n    'random_state': 42,\n    'n_jobs': -1\n}\n\nfinal_xgb_pipeline = Pipeline([\n    ('preprocessing', preprocessor),\n    ('xgb', XGBRegressor(**final_params))\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:15:26.991648Z","iopub.execute_input":"2025-10-11T03:15:26.991931Z","iopub.status.idle":"2025-10-11T03:15:26.998043Z","shell.execute_reply.started":"2025-10-11T03:15:26.991912Z","shell.execute_reply":"2025-10-11T03:15:26.997135Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train on Full Data & Predict on Test Set\n\n- Fit the Random Forest pipeline on the entire training dataset\n- Predict `Premium Amount` for the test dataset\n- Prepare submission CSV for Kaggle\n","metadata":{}},{"cell_type":"code","source":"#Train the final model on ALL your training data\n\nprint(\"Training the final model on all data...\")\nfinal_xgb_pipeline.fit(X, y)\nprint(\"Final model is ready!\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:15:30.172026Z","iopub.execute_input":"2025-10-11T03:15:30.172412Z","iopub.status.idle":"2025-10-11T03:15:50.834223Z","shell.execute_reply.started":"2025-10-11T03:15:30.172379Z","shell.execute_reply":"2025-10-11T03:15:50.832751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#predictions on test dataset\n\npredictions= final_xgb_pipeline.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:16:20.962306Z","iopub.execute_input":"2025-10-11T03:16:20.962667Z","iopub.status.idle":"2025-10-11T03:16:24.877942Z","shell.execute_reply.started":"2025-10-11T03:16:20.962640Z","shell.execute_reply":"2025-10-11T03:16:24.877277Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Prepare & Save Submission\n- Combine `id` column with predicted `Premium Amount`\n- Save the result as `submission.csv` for Kaggle upload\n","metadata":{}},{"cell_type":"code","source":"# Prepare submission DataFrame and save CSV\n\nsubmission = pd.DataFrame({\n    'id': ids, \n    'Premium Amount': predictions   \n})\n\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission file created: submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T03:16:30.486805Z","iopub.execute_input":"2025-10-11T03:16:30.487155Z","iopub.status.idle":"2025-10-11T03:16:31.252965Z","shell.execute_reply.started":"2025-10-11T03:16:30.487128Z","shell.execute_reply":"2025-10-11T03:16:31.252230Z"}},"outputs":[],"execution_count":null}]}