{"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":"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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndata = pd.read_csv('/kaggle/input/carnival-risk-analytics-challenge/train.csv')\ndf = pd.DataFrame(data)\ndf2 = pd.DataFrame(data)\nprint(data.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-09T17:19:41.260940Z","iopub.execute_input":"2025-10-09T17:19:41.261203Z","iopub.status.idle":"2025-10-09T17:19:47.730224Z","shell.execute_reply.started":"2025-10-09T17:19:41.261171Z","shell.execute_reply":"2025-10-09T17:19:47.729258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom xgboost import XGBRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_error, r2_score, mean_squared_error\n\n# Add target column\ntop_features = [\n    'Health Score', 'Annual Income', 'Credit Score', \n    'Age', 'Vehicle Age', 'Insurance Duration', 'Premium Amount'\n]\n\n# Keep only selected columns\ndf = df[top_features].copy()\n\n# Fill missing values with median\ndf = df.fillna(df.median(numeric_only=True))\n\n# Separate features (X) and target (y)\nX = df.drop('Premium Amount', axis=1)\ny = df['Premium Amount']\n\n# Split into train/test sets\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n\n# Initialize and train XGBoost regressor\nmodel = XGBRegressor(\n    n_estimators=300,\n    learning_rate=0.05,\n    max_depth=6,\n    subsample=0.8,\n    colsample_bytree=0.8,\n    random_state=42,\n    n_jobs=-1\n)\n\nmodel.fit(X_train, y_train)\n\n# Predictions\ny_pred = model.predict(X_test)\n\n# Evaluate\nmae = mean_absolute_error(y_test, y_pred)\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\nr2 = r2_score(y_test, y_pred)\n\nprint(f\"Mean Absolute Error (MAE): {mae:.2f}\")\nprint(f\"Root Mean Squared Error (RMSE): {rmse:.2f}\")\nprint(f\"R² Score: {r2:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:21:01.582384Z","iopub.execute_input":"2025-10-06T14:21:01.582849Z","iopub.status.idle":"2025-10-06T14:21:08.231822Z","shell.execute_reply.started":"2025-10-06T14:21:01.582819Z","shell.execute_reply":"2025-10-06T14:21:08.230795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Load the test dataset\ntest_df = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/test.csv\")\n\n# Keep only the columns the model was trained on\ntop_features = [\n    'Health Score', 'Annual Income', 'Credit Score', \n    'Age', 'Vehicle Age', 'Insurance Duration'\n]\n\n# Subset test data\nX_test_final = test_df[top_features].copy()\n\n# Fill missing values with median (same logic as training)\nX_test_final = X_test_final.fillna(df[top_features].median(numeric_only=True))\n\n# Predict Premium Amount\npredictions = model.predict(X_test_final)\n\n# Create submission DataFrame\nsubmission = pd.DataFrame({\n    'id': test_df['id'],\n    'Premium Amount': predictions\n})\n\n# Save to CSV in Kaggle working directory\noutput_path = \"/kaggle/working/premium_predictions.csv\"\nsubmission.to_csv(output_path, index=False)\n\nprint(f\"✅ Predictions saved successfully to: {output_path}\")\nprint(submission.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:23:05.114748Z","iopub.execute_input":"2025-10-06T14:23:05.115063Z","iopub.status.idle":"2025-10-06T14:23:10.199907Z","shell.execute_reply.started":"2025-10-06T14:23:05.115042Z","shell.execute_reply":"2025-10-06T14:23:10.198859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df.info())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T05:32:47.735834Z","iopub.execute_input":"2025-10-04T05:32:47.736141Z","iopub.status.idle":"2025-10-04T05:32:48.143152Z","shell.execute_reply.started":"2025-10-04T05:32:47.736120Z","shell.execute_reply":"2025-10-04T05:32:48.142162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = df.drop(columns=['id', 'Occupation', 'Location', 'Previous Claims', 'Policy Start Date'])\ndf['Age'].fillna(df['Age'].mean(), inplace=True)\ndf['Annual Income'].fillna(df['Annual Income'].mean(), inplace=True)\nprint(df[['Age', 'Annual Income']].isnull().sum())\nprint(df.info())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T05:36:34.176448Z","iopub.execute_input":"2025-10-04T05:36:34.176758Z","iopub.status.idle":"2025-10-04T05:36:34.546620Z","shell.execute_reply.started":"2025-10-04T05:36:34.176737Z","shell.execute_reply":"2025-10-04T05:36:34.545766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Fill Marital Status\nmask = df['Marital Status'].isna()\ndf.loc[mask, 'Marital Status'] = np.where(\n    (df.loc[mask, 'Age'] < 25) | (df.loc[mask, 'Number of Children'] == 0),\n    'Single',\n    'Married'\n)\n\n# Compute mean number of children for married people (excluding zeros)\nmean_children = df.loc[\n    (df['Marital Status'] == 'Married') & (df['Number of Children'] > 0),\n    'Number of Children'\n].mean()\n\n# Fill Number of Children properly\nmask_children = df['Number of Children'].isna()\ndf.loc[mask_children, 'Number of Children'] = df.loc[mask_children, 'Marital Status'].apply(\n    lambda x: 0 if x == 'Single' else mean_children\n)\n\n# Drop Customer Feedback\ndf = df.drop(columns=['Customer Feedback'])\n\n# Fill remaining numeric columns with their mean\nnumeric_cols = df.select_dtypes(include=['float64', 'int64']).columns\ndf[numeric_cols] = df[numeric_cols].fillna(df[numeric_cols].mean())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T05:43:31.849963Z","iopub.execute_input":"2025-10-04T05:43:31.850375Z","iopub.status.idle":"2025-10-04T05:43:32.292101Z","shell.execute_reply.started":"2025-10-04T05:43:31.850349Z","shell.execute_reply":"2025-10-04T05:43:32.291253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df.info())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T05:43:44.275039Z","iopub.execute_input":"2025-10-04T05:43:44.275713Z","iopub.status.idle":"2025-10-04T05:43:44.523366Z","shell.execute_reply.started":"2025-10-04T05:43:44.275684Z","shell.execute_reply":"2025-10-04T05:43:44.522437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ncontinuous_cols = [\n    'Age', 'Annual Income', 'Health Score', \n    'Vehicle Age', 'Credit Score', 'Insurance Duration'\n]\n\ncategorical_cols = [\n    'Marital Status', 'Education Level', 'Policy Type',\n    'Smoking Status', 'Exercise Frequency', 'Property Type'\n]\n\ntarget_col = 'Premium Amount'\n\ndf_encoded = pd.get_dummies(df[continuous_cols + categorical_cols], drop_first=True)\ny = df[target_col]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:16:49.957289Z","iopub.execute_input":"2025-10-06T14:16:49.957698Z","iopub.status.idle":"2025-10-06T14:16:50.493936Z","shell.execute_reply.started":"2025-10-06T14:16:49.957666Z","shell.execute_reply":"2025-10-06T14:16:50.492654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"correlations = df_encoded.corrwith(y).sort_values(ascending=False)\nprint(correlations)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T05:47:30.379529Z","iopub.execute_input":"2025-10-04T05:47:30.379876Z","iopub.status.idle":"2025-10-04T05:47:30.745570Z","shell.execute_reply.started":"2025-10-04T05:47:30.379850Z","shell.execute_reply":"2025-10-04T05:47:30.744718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np\n\nX = df_encoded\ny = y  # Premium Amount\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nmodel = RandomForestRegressor(\n    n_estimators=200,\n    max_depth=None,\n    random_state=42,\n    n_jobs=-1\n)\n\nmodel.fit(X_train, y_train)\n\ny_pred = model.predict(X_test)\n\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\nprint(\"RMSE:\", rmse)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:17:06.250340Z","iopub.execute_input":"2025-10-06T14:17:06.250704Z","iopub.status.idle":"2025-10-06T14:17:06.522069Z","shell.execute_reply.started":"2025-10-06T14:17:06.250678Z","shell.execute_reply":"2025-10-06T14:17:06.520620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np\n\nxgb_model = xgb.XGBRegressor(\n    n_estimators=200,\n    max_depth=6,\n    learning_rate=0.1,\n    random_state=42,\n    n_jobs=-1\n)\n\nxgb_model.fit(X_train, y_train)\ny_pred_xgb = xgb_model.predict(X_test)\n\nrmse_xgb = np.sqrt(mean_squared_error(y_test, y_pred_xgb))\nprint(\"XGBoost RMSE:\", rmse_xgb)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T06:03:30.733153Z","iopub.execute_input":"2025-10-04T06:03:30.733676Z","iopub.status.idle":"2025-10-04T06:03:37.924620Z","shell.execute_reply.started":"2025-10-04T06:03:30.733650Z","shell.execute_reply":"2025-10-04T06:03:37.923965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np\n\nlgb_model = lgb.LGBMRegressor(\n    n_estimators=200,\n    max_depth=-1,\n    learning_rate=0.1,\n    random_state=42,\n    n_jobs=-1\n)\n\nlgb_model.fit(X_train, y_train)\ny_pred_lgb = lgb_model.predict(X_test)\n\nrmse_lgb = np.sqrt(mean_squared_error(y_test, y_pred_lgb))\nprint(\"LightGBM RMSE:\", rmse_lgb)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T06:04:24.875991Z","iopub.execute_input":"2025-10-04T06:04:24.876358Z","iopub.status.idle":"2025-10-04T06:04:33.986883Z","shell.execute_reply.started":"2025-10-04T06:04:24.876334Z","shell.execute_reply":"2025-10-04T06:04:33.986166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\n\nimportances = model.feature_importances_  # use your trained RandomForest/XGB/LGBM\nfeature_names = X_train.columns\nfeat_imp = pd.Series(importances, index=feature_names).sort_values(ascending=False)\n\nplt.figure(figsize=(10,6))\nfeat_imp[:20].plot(kind='barh')  # top 20 features\nplt.title('Top Feature Importances')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-04T06:05:40.767693Z","iopub.execute_input":"2025-10-04T06:05:40.768389Z","iopub.status.idle":"2025-10-04T06:05:41.897273Z","shell.execute_reply.started":"2025-10-04T06:05:40.768363Z","shell.execute_reply":"2025-10-04T06:05:41.896298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\nimport matplotlib.pyplot as plt\n\n# Features and target\nX = df_encoded\ny = y  # Premium Amount\n\n# Train-test split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Train initial RandomForest\nmodel = RandomForestRegressor(n_estimators=300, max_depth=None, random_state=42, n_jobs=-1)\nmodel.fit(X_train, y_train)\n\n# Predict and calculate RMSE\ny_pred = model.predict(X_test)\nrmse_initial = np.sqrt(mean_squared_error(y_test, y_pred))\nprint(\"Initial RMSE:\", rmse_initial)\n\n# Feature importance\nimportances = pd.Series(model.feature_importances_, index=X_train.columns).sort_values(ascending=False)\n\n# Plot top 20 features\nplt.figure(figsize=(10,6))\nimportances[:20].plot(kind='barh')\nplt.title('Top 20 Feature Importances')\nplt.show()\n\n# Drop features with very low importance (e.g., <0.001)\nlow_importance_features = importances[importances < 0.001].index\nX_train_reduced = X_train.drop(columns=low_importance_features)\nX_test_reduced = X_test.drop(columns=low_importance_features)\n\n# Retrain model with reduced features\nmodel_reduced = RandomForestRegressor(n_estimators=300, max_depth=None, random_state=42, n_jobs=-1)\nmodel_reduced.fit(X_train_reduced, y_train)\n\n# Predict and calculate new RMSE\ny_pred_reduced = model_reduced.predict(X_test_reduced)\nrmse_reduced = np.sqrt(mean_squared_error(y_test, y_pred_reduced))\nprint(\"RMSE after dropping low-importance features:\", rmse_reduced)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:15:37.933827Z","iopub.execute_input":"2025-10-06T14:15:37.934165Z","iopub.status.idle":"2025-10-06T14:15:37.951482Z","shell.execute_reply.started":"2025-10-06T14:15:37.934142Z","shell.execute_reply":"2025-10-06T14:15:37.950340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# ==============================\n# 1️⃣ Select top features\n# ==============================\ntop_features = [\n    'Health Score', 'Annual Income', 'Credit Score', \n    'Age', 'Vehicle Age', 'Insurance Duration'\n]\n\nX_selected = X[top_features]\ny_selected = y  # Premium Amount\n\n# ==============================\n# 2️⃣ Split data\n# ==============================\nX_train, X_test, y_train, y_test = train_test_split(\n    X_selected, y_selected, test_size=0.2, random_state=42\n)\n\n# ==============================\n# 3️⃣ Train model\n# ==============================\nmodel = RandomForestRegressor(\n    n_estimators=200,\n    max_depth=10,\n    random_state=42\n)\nmodel.fit(X_train, y_train)\n\n# ==============================\n# 4️⃣ Predict and evaluate\n# ==============================\ny_pred = model.predict(X_test)\n\nmae = mean_absolute_error(y_test, y_pred)\nmse = mean_squared_error(y_test, y_pred)\nrmse = np.sqrt(mse)\nr2 = r2_score(y_test, y_pred)\n\nprint(\"✅ Model trained using only top features\\n\")\nprint(f\"Mean Absolute Error (MAE): {mae:.2f}\")\nprint(f\"Root Mean Squared Error (RMSE): {rmse:.2f}\")\nprint(f\"R² Score: {r2:.3f}\")\n\n# ==============================\n# 5️⃣ Plot feature importances\n# ==============================\nimportances = pd.Series(model.feature_importances_, index=top_features)\nimportances.sort_values().plot(kind='barh', figsize=(8,5))\nplt.title(\"Feature Importances (Top Features Only)\")\nplt.xlabel(\"Importance\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:14:05.658745Z","iopub.execute_input":"2025-10-06T14:14:05.658964Z","iopub.status.idle":"2025-10-06T14:14:07.972166Z","shell.execute_reply.started":"2025-10-06T14:14:05.658945Z","shell.execute_reply":"2025-10-06T14:14:07.970616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# keep only selected features\ntop_features = [\n    'Health Score', 'Annual Income', 'Credit Score', \n    'Age', 'Vehicle Age', 'Insurance Duration'\n]\n\ndf = df[top_features].copy()\n\n# Check missing values before filling\nprint(\"Missing values before filling:\")\nprint(df.isna().sum())\n\n# Fill missing values\n# We'll use median for numeric data (robust against outliers)\ndf = df.fillna(df.median(numeric_only=True))\n\n# Verify filling worked\nprint(\"\\nMissing values after filling:\")\nprint(df.isna().sum())\n\n# Optional: show summary\nprint(\"\\nDataFrame shape after cleaning:\", df.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-06T14:19:12.213134Z","iopub.execute_input":"2025-10-06T14:19:12.213523Z","iopub.status.idle":"2025-10-06T14:19:12.451990Z","shell.execute_reply.started":"2025-10-06T14:19:12.213487Z","shell.execute_reply":"2025-10-06T14:19:12.450838Z"}},"outputs":[],"execution_count":null}]}