{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":115000,"databundleVersionId":13897013,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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,"execution":{"iopub.status.busy":"2025-10-11T02:43:32.059614Z","iopub.execute_input":"2025-10-11T02:43:32.059907Z","iopub.status.idle":"2025-10-11T02:43:32.067084Z","shell.execute_reply.started":"2025-10-11T02:43:32.059887Z","shell.execute_reply":"2025-10-11T02:43:32.066478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd\nimport lightgbm as lgb\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import KFold\nfrom sklearn.preprocessing import PowerTransformer\nfrom sklearn.metrics import mean_squared_error\nimport warnings\n\n\nwarnings.filterwarnings('ignore')\n\n\ndf_train = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/test.csv\")\n\n\ntest_ids = df_test['id']\n\n# Combine train and test for consistent and efficient preprocessing\ncombined_df = pd.concat([df_train.drop('Premium Amount', axis=1), df_test], ignore_index=True)\n\nprint(\"Data loaded successfully.\")\n\n# --- Corrected Preprocessing Function ---\ndef thoughtful_preprocess(df):\n    \"\"\"Applies preprocessing steps based on logical, human-like assumptions.\"\"\"\n    df_copy = df.copy()\n\n    \n    df_copy['Policy Start Date'] = pd.to_datetime(df_copy['Policy Start Date'], errors='coerce')\n    df_copy['policy_year'] = df_copy['Policy Start Date'].dt.year\n    df_copy['policy_month'] = df_copy['Policy Start Date'].dt.month\n    df_copy = df_copy.drop(\"Policy Start Date\", axis=1)\n    df_copy['Previous Claims'] = df_copy['Previous Claims'].fillna(0)\n    for col in ['Age', 'Annual Income', 'Health Score', 'Credit Score', 'Insurance Duration']:\n        df_copy[col] = df_copy[col].fillna(df_copy[col].median())\n\n   \n    exercise_map = {'Never': 0, 'Rarely': 1, 'Monthly': 2, 'Weekly': 3, 'Daily': 4}\n    # Fill NaNs in this column before mapping, using a logical default like 'Rarely' or a new 'Unknown' category\n    df_copy['Exercise Frequency'] = df_copy['Exercise Frequency'].fillna('Unknown')\n    exercise_map['Unknown'] = 0 # Add 'Unknown' to the map\n    \n    df_copy['Health_Lifestyle_Score'] = df_copy['Health Score'] * df_copy['Exercise Frequency'].map(exercise_map)\n    df_copy['Income_per_Dependent'] = df_copy['Annual Income'] / (df_copy['Number of Children'] + 1)\n    df_copy['Claims_per_Year'] = df_copy['Previous Claims'] / (df_copy['Insurance Duration'] + 1)\n    \n    \n    for col in df_copy.select_dtypes(include=['object']).columns:\n        df_copy[col] = df_copy[col].astype('category')\n        \n    # Binning can be done last as well\n    df_copy['Age_Group'] = pd.cut(df_copy['Age'], bins=[0, 25, 40, 65, 100], labels=False, include_lowest=True).astype('category')\n    df_copy['Income_Bracket'] = pd.qcut(df_copy['Annual Income'], q=4, labels=False).astype('category')\n    \n    return df_copy\n\nprint(\"Preprocessing function is ready.\")\n\n\nprocessed_df = thoughtful_preprocess(combined_df)\n\n\nX = processed_df[processed_df['id'].isin(df_train['id'])].drop('id', axis=1)\nX_test = processed_df[processed_df['id'].isin(df_test['id'])].drop('id', axis=1)\ny = df_train['Premium Amount']\n\nprint(\"Preprocessing applied. Train and test sets are ready.\")\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:43:32.068152Z","iopub.execute_input":"2025-10-11T02:43:32.068749Z","iopub.status.idle":"2025-10-11T02:43:38.60354Z","shell.execute_reply.started":"2025-10-11T02:43:32.068722Z","shell.execute_reply":"2025-10-11T02:43:38.602751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\npt = PowerTransformer(method='yeo-johnson')\n\n\ny_transformed = pt.fit_transform(y.values.reshape(-1, 1))\n\nprint(\"Target variable transformed successfully.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:43:38.604287Z","iopub.execute_input":"2025-10-11T02:43:38.604489Z","iopub.status.idle":"2025-10-11T02:43:38.802545Z","shell.execute_reply.started":"2025-10-11T02:43:38.604472Z","shell.execute_reply":"2025-10-11T02:43:38.801821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.select_dtypes(include=['object','category']).nunique().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:43:38.804291Z","iopub.execute_input":"2025-10-11T02:43:38.804513Z","iopub.status.idle":"2025-10-11T02:43:39.226462Z","shell.execute_reply.started":"2025-10-11T02:43:38.804496Z","shell.execute_reply":"2025-10-11T02:43:39.225731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\ncorr_matrix = df_train.corr(numeric_only=True)\n\n\nsns.heatmap(corr_matrix, \n            cmap='RdBu_r', \n            annot=False,  # Set to False if too many features, else it gets crowded\n            center=0,\n            square=True,\n            fmt='.2f',\n            cbar_kws={'shrink': 0.8},\n            linewidths=0.5)\n\nplt.title('Correlation Matrix', fontsize=16, fontweight='bold', pad=20)\nplt.xticks(rotation=45, ha='right', fontsize=8)\nplt.yticks(rotation=0, fontsize=8)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:43:39.227309Z","iopub.execute_input":"2025-10-11T02:43:39.228001Z","iopub.status.idle":"2025-10-11T02:43:39.795593Z","shell.execute_reply.started":"2025-10-11T02:43:39.227979Z","shell.execute_reply":"2025-10-11T02:43:39.794963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nkfold = KFold(n_splits=5, shuffle=True, random_state=42)\noof_predictions = np.zeros(X.shape[0])\ntest_predictions = np.zeros(X_test.shape[0])\n\n\nmodel_params = {\n    'objective': 'regression_l1', 'metric': 'rmse', 'n_estimators': 2000,\n    'learning_rate': 0.01, 'feature_fraction': 0.8, 'bagging_fraction': 0.8,\n    'bagging_freq': 1, 'lambda_l1': 0.1, 'lambda_l2': 0.1,\n    'num_leaves': 31, 'verbose': -1, 'n_jobs': -1, 'seed': 42\n}\n\n\nfor fold, (train_idx, val_idx) in enumerate(kfold.split(X, y_transformed)):\n    print(f\"--- Training Fold {fold+1} ---\")\n    X_train, y_train = X.iloc[train_idx], y_transformed[train_idx]\n    X_val, y_val = X.iloc[val_idx], y_transformed[val_idx]\n    \n    model = lgb.LGBMRegressor(**model_params)\n    model.fit(X_train, y_train,\n              eval_set=[(X_val, y_val)],\n              eval_metric='rmse',\n              callbacks=[lgb.early_stopping(100, verbose=False)])\n    \n    val_preds = model.predict(X_val)\n    oof_predictions[val_idx] = val_preds\n    test_predictions += model.predict(X_test) / kfold.n_splits\n\nprint(\"\\nModel training complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:43:39.796394Z","iopub.execute_input":"2025-10-11T02:43:39.796659Z","iopub.status.idle":"2025-10-11T02:46:39.831162Z","shell.execute_reply.started":"2025-10-11T02:43:39.796634Z","shell.execute_reply":"2025-10-11T02:46:39.830596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport lightgbm as lgb\nfrom sklearn.model_selection import KFold, RandomizedSearchCV\nfrom sklearn.metrics import mean_squared_error\n\n\nkfold = KFold(n_splits=5, shuffle=True, random_state=42)\n\n\nX_numeric = X.select_dtypes(include=[np.number])\nX_test_numeric = X_test.select_dtypes(include=[np.number])\n\n\ny_log = np.log1p(y)\n\n\nmodel = lgb.LGBMRegressor(random_state=42, n_jobs=-1)\n\n\nparam_dist = {\n    'n_estimators': [200],\n    'learning_rate': [0.05],\n    'num_leaves': [20],\n    'max_depth': [12],\n    'reg_alpha': [0.1], \n    'reg_lambda': [0.1]  \n}\n\n\nsearch = RandomizedSearchCV(\n    model,\n    param_distributions=param_dist,\n    n_iter=25,  \n    cv=kfold,\n    scoring='neg_mean_squared_error',\n    n_jobs=-1,\n    verbose=1,\n    random_state=42\n)\n\n\nsearch.fit(X_numeric, y_log)\n\n\nbest_model = search.best_estimator_\nbest_params = search.best_params_\ncv_rmse = np.sqrt(-search.best_score_)\n\nprint(f\"\\nBest LightGBM Params: {best_params}\")\nprint(f\"Cross-validated RMSE: {cv_rmse:.6f}\")\n\n\nfinal_predictions = np.expm1(best_model.predict(X_test_numeric))\nprint(f\"\\nFinal Predictions ready - Mean: {final_predictions.mean():.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:46:39.831642Z","iopub.execute_input":"2025-10-11T02:46:39.831887Z","iopub.status.idle":"2025-10-11T02:50:39.603381Z","shell.execute_reply.started":"2025-10-11T02:46:39.831862Z","shell.execute_reply":"2025-10-11T02:50:39.602665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndata=pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/test.csv\")\nid_row=data[\"id\"]\ndata=data.drop(columns=\"id\")\nprediction=best_model.predict(data)\ndata_frame=pd.DataFrame({\"id\":id_row,\"Premium Amount\":prediction})\ndata_frame.head()\nsample_submission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T02:50:39.604374Z","iopub.execute_input":"2025-10-11T02:50:39.604585Z","iopub.status.idle":"2025-10-11T02:50:40.989728Z","shell.execute_reply.started":"2025-10-11T02:50:39.604567Z","shell.execute_reply":"2025-10-11T02:50:40.988817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}