{"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":31153,"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,"execution":{"iopub.status.busy":"2025-10-11T04:50:25.762386Z","iopub.execute_input":"2025-10-11T04:50:25.762895Z","iopub.status.idle":"2025-10-11T04:50:28.442577Z","shell.execute_reply.started":"2025-10-11T04:50:25.762687Z","shell.execute_reply":"2025-10-11T04:50:28.441248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\nimport lightgbm as lgb\nimport time\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T06:57:56.549147Z","iopub.execute_input":"2025-10-11T06:57:56.549729Z","iopub.status.idle":"2025-10-11T06:57:56.556169Z","shell.execute_reply.started":"2025-10-11T06:57:56.549703Z","shell.execute_reply":"2025-10-11T06:57:56.554994Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/carnival-risk-analytics-challenge/train.csv')\ntarget = 'Premium Amount'\nid_col = 'id'\ntest_df = pd.read_csv('/kaggle/input/carnival-risk-analytics-challenge/test.csv')\nprint(f\"\\nFull Training Data Shape: {train_df.shape}\")\nprint(f\"Test Data Shape: {test_df.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T06:59:46.668225Z","iopub.execute_input":"2025-10-11T06:59:46.668580Z","iopub.status.idle":"2025-10-11T06:59:52.260024Z","shell.execute_reply.started":"2025-10-11T06:59:46.668554Z","shell.execute_reply":"2025-10-11T06:59:52.259178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n Basic Dataset Overview:\")\nprint(train_df.info())\n\nplt.figure(figsize=(6,4))\nsns.histplot(train_df['Premium Amount'].dropna(), bins=40, kde=True)\nplt.title(\"Distribution of Premium Amount (Target)\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T06:59:52.261396Z","iopub.execute_input":"2025-10-11T06:59:52.261675Z","iopub.status.idle":"2025-10-11T06:59:55.909272Z","shell.execute_reply.started":"2025-10-11T06:59:52.261653Z","shell.execute_reply":"2025-10-11T06:59:55.908130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = 'Premium Amount'\nid_col = 'id'\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T06:59:55.910746Z","iopub.execute_input":"2025-10-11T06:59:55.911125Z","iopub.status.idle":"2025-10-11T06:59:55.931480Z","shell.execute_reply.started":"2025-10-11T06:59:55.911095Z","shell.execute_reply":"2025-10-11T06:59:55.930510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\nplt.figure(figsize=(12,5))\nsns.heatmap(train_df.isnull(), cbar=False)\nplt.title(\"Missing Value Heatmap\")\nplt.show()\n\nmissing = train_df.isnull().mean().sort_values(ascending=False)\nprint(\"\\nMissing Values (%):\")\nprint((missing*100).round(1))\n\n\nnum_cols = train_df.select_dtypes(include=np.number).columns\nprint(\"\\nNumerical Feature Summary:\")\ndisplay(train_df[num_cols].describe().T)\n\nplt.figure(figsize=(14, 10))\ncols_to_plot = [\"Age\", \"Annual Income\", \"Health Score\", \"Credit Score\", \"Vehicle Age\", \"Insurance Duration\", \"Premium Amount\"]\nfor i, col in enumerate(cols_to_plot, 1):\n    plt.subplot(3, 3, i)\n    sns.histplot(train_df[col].dropna(), bins=40, kde=True)\n    plt.title(f\"{col} distribution\")\nplt.tight_layout()\nplt.show()\n\nplt.figure(figsize=(10, 6))\nsns.heatmap(train_df[num_cols].corr(), cmap='coolwarm', annot=False)\nplt.title(\"Correlation Matrix (Numeric Features)\")\nplt.show()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T05:22:59.148679Z","iopub.execute_input":"2025-10-11T05:22:59.149029Z","iopub.status.idle":"2025-10-11T05:23:38.739743Z","shell.execute_reply.started":"2025-10-11T05:22:59.149002Z","shell.execute_reply":"2025-10-11T05:23:38.737951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['Policy Start Date'] = pd.to_datetime(train_df['Policy Start Date'])\ntrain_df['start_year'] = train_df['Policy Start Date'].dt.year\ntrain_df['start_month'] = train_df['Policy Start Date'].dt.month\ntrain_df['start_dayofweek'] = train_df['Policy Start Date'].dt.dayofweek\n\nX = train_df.drop(columns=[target, id_col, 'Policy Start Date'])\ny = train_df[target]\n\nvalid_y_indices = y.dropna().index\nX = X.loc[valid_y_indices]\ny = y.loc[valid_y_indices]\n\nnum_cols = X.select_dtypes(include=np.number).columns\ncat_cols = X.select_dtypes(include=['object']).columns\n\nprint(f\"\\nNumerical Features: {len(num_cols)} | Categorical Features: {len(cat_cols)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:01:59.930677Z","iopub.execute_input":"2025-10-11T07:01:59.931705Z","iopub.status.idle":"2025-10-11T07:02:00.758418Z","shell.execute_reply.started":"2025-10-11T07:01:59.931675Z","shell.execute_reply":"2025-10-11T07:02:00.757431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- 5. PREPROCESSING PIPELINE ---\nnumeric_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='median'))\n])\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('encoder', OneHotEncoder(handle_unknown='ignore', sparse_output=True))\n])\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numeric_transformer, num_cols),\n        ('cat', categorical_transformer, cat_cols)\n    ],\n    n_jobs=-1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:02:56.785805Z","iopub.execute_input":"2025-10-11T07:02:56.786186Z","iopub.status.idle":"2025-10-11T07:02:56.792079Z","shell.execute_reply.started":"2025-10-11T07:02:56.786161Z","shell.execute_reply":"2025-10-11T07:02:56.790913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=42)\nprint(\"\\n Data split into training and validation sets.\")\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:03:44.475815Z","iopub.execute_input":"2025-10-11T07:03:44.476165Z","iopub.status.idle":"2025-10-11T07:03:44.788641Z","shell.execute_reply.started":"2025-10-11T07:03:44.476144Z","shell.execute_reply":"2025-10-11T07:03:44.787678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n Training Random Forest Regressor...\")\nstart_time = time.time()\n\nrf_model = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('model', RandomForestRegressor(\n        n_estimators=100, \n        max_depth=15,\n        n_jobs=-1,\n        random_state=42\n    ))\n])\n\nrf_model.fit(X_train, y_train)\nrf_train_time = time.time() - start_time\nrf_preds = rf_model.predict(X_valid)\n\nrf_rmse = np.sqrt(mean_squared_error(y_valid, rf_preds))\n\nprint(f\"\\n Random Forest RMSE: {rf_rmse:.4f}\")\nprint(f\"Training Time: {rf_train_time/60:.2f} minutes\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n Training LightGBM Regressor...\")\nstart_time = time.time()\n\nX_train_proc = preprocessor.fit_transform(X_train)\nX_valid_proc = preprocessor.transform(X_valid)\n\nlgb_model = lgb.LGBMRegressor(\n    objective='regression_l1', \n    metric='rmse',\n    n_estimators=2000,         \n    learning_rate=0.035,        \n    feature_fraction=0.75,      \n    bagging_fraction=0.75,      \n    bagging_freq=1,\n    lambda_l1=0.2,            \n    lambda_l2=0.2,             \n    num_leaves=64,             \n    verbose=-1,\n    n_jobs=-1,\n    seed=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:07:06.931342Z","iopub.execute_input":"2025-10-11T07:07:06.931688Z","iopub.status.idle":"2025-10-11T07:07:14.409516Z","shell.execute_reply.started":"2025-10-11T07:07:06.931655Z","shell.execute_reply":"2025-10-11T07:07:14.408270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgb_model.fit(X_train_proc, y_train,\n              eval_set=[(X_valid_proc, y_valid)],\n              eval_metric='rmse',\n              callbacks=[lgb.early_stopping(100, verbose=False)])\n\nlgb_train_time = time.time() - start_time\nlgb_preds = lgb_model.predict(X_valid_proc)\n\nlgb_rmse = np.sqrt(mean_squared_error(y_valid, lgb_preds))\n\nprint(f\"\\n LightGBM RMSE: {lgb_rmse:.4f}\")\nprint(f\"Training Time: {lgb_train_time/60:.2f} minutes\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:07:16.536294Z","iopub.execute_input":"2025-10-11T07:07:16.536635Z","iopub.status.idle":"2025-10-11T07:07:40.081906Z","shell.execute_reply.started":"2025-10-11T07:07:16.536610Z","shell.execute_reply":"2025-10-11T07:07:40.081086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n Model Comparison:\")\nprint(pd.DataFrame({\n    'Model': ['Random Forest', 'LightGBM'],\n    'RMSE': [rf_rmse, lgb_rmse],\n    'Training Time (min)': [rf_train_time/60, lgb_train_time/60]\n}))\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gc.collect()\nprint(\"\\n Pipeline completed successfully!\")\ntest_df = pd.read_csv('/kaggle/input/carnival-risk-analytics-challenge/train.csv')\ntest_df['Policy Start Date'] = pd.to_datetime(test_df['Policy Start Date'])\ntest_df['start_year'] = test_df['Policy Start Date'].dt.year\ntest_df['start_month'] = test_df['Policy Start Date'].dt.month\ntest_df['start_dayofweek'] = test_df['Policy Start Date'].dt.dayofweek\n\nX_test = test_df.drop(columns=['id', 'Policy Start Date'])\n\nX_test_proc = preprocessor.transform(X_test)\n\ntest_preds = lgb_model.predict(X_test_proc)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:12:55.943041Z","iopub.execute_input":"2025-10-11T07:12:55.943374Z","iopub.status.idle":"2025-10-11T07:13:13.838589Z","shell.execute_reply.started":"2025-10-11T07:12:55.943351Z","shell.execute_reply":"2025-10-11T07:13:13.837899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test_df['id'],\n    'Premium Amount': test_preds\n})\n\nsubmission.to_csv('submission.csv', index=False)\nprint(\"\\n Submission file 'submission.csv' created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T07:13:34.426670Z","iopub.execute_input":"2025-10-11T07:13:34.427959Z","iopub.status.idle":"2025-10-11T07:13:36.069710Z","shell.execute_reply.started":"2025-10-11T07:13:34.427926Z","shell.execute_reply":"2025-10-11T07:13:36.068675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}