{"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-08T18:02:53.101877Z","iopub.execute_input":"2025-10-08T18:02:53.103036Z","iopub.status.idle":"2025-10-08T18:02:53.489255Z","shell.execute_reply.started":"2025-10-08T18:02:53.102985Z","shell.execute_reply":"2025-10-08T18:02:53.488075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nimport lightgbm as lgb\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:02:58.062218Z","iopub.execute_input":"2025-10-08T18:02:58.063487Z","iopub.status.idle":"2025-10-08T18:03:07.101946Z","shell.execute_reply.started":"2025-10-08T18:02:58.063451Z","shell.execute_reply":"2025-10-08T18:03:07.100838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/carnival-risk-analytics-challenge/test.csv\")\n\nprint(f\"Train shape: {train.shape}\")\nprint(f\"test shape: {test.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:03:13.753243Z","iopub.execute_input":"2025-10-08T18:03:13.753989Z","iopub.status.idle":"2025-10-08T18:03:23.072293Z","shell.execute_reply.started":"2025-10-08T18:03:13.753956Z","shell.execute_reply":"2025-10-08T18:03:23.070835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TARGET = 'Premium Amount'\nID_COL = 'id'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:03:30.526845Z","iopub.execute_input":"2025-10-08T18:03:30.527214Z","iopub.status.idle":"2025-10-08T18:03:30.532879Z","shell.execute_reply.started":"2025-10-08T18:03:30.527188Z","shell.execute_reply":"2025-10-08T18:03:30.531364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.drop(columns=[ID_COL])\ntest_ids = test[ID_COL]\ntest = test.drop(columns=[ID_COL])\n\ny = train[TARGET]\nX = train.drop(columns=[TARGET])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:03:33.062340Z","iopub.execute_input":"2025-10-08T18:03:33.063089Z","iopub.status.idle":"2025-10-08T18:03:33.375865Z","shell.execute_reply.started":"2025-10-08T18:03:33.063052Z","shell.execute_reply":"2025-10-08T18:03:33.374600Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols = X.select_dtypes(include=['object']).columns.tolist()\nnum_cols = X.select_dtypes(exclude=['object']).columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:03:37.281702Z","iopub.execute_input":"2025-10-08T18:03:37.282116Z","iopub.status.idle":"2025-10-08T18:03:37.405262Z","shell.execute_reply.started":"2025-10-08T18:03:37.282086Z","shell.execute_reply":"2025-10-08T18:03:37.403820Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in num_cols:\n    X[col] = X[col].fillna(X[col].median())\n    test[col] = test[col].fillna(X[col].median())\n\nfor col in cat_cols:\n    X[col] = X[col].fillna('Missing')\n    test[col] = test[col].fillna('Missing')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:03:42.172202Z","iopub.execute_input":"2025-10-08T18:03:42.172518Z","iopub.status.idle":"2025-10-08T18:03:43.401945Z","shell.execute_reply.started":"2025-10-08T18:03:42.172499Z","shell.execute_reply":"2025-10-08T18:03:43.400242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nfor col in cat_cols:\n    le = LabelEncoder()\n    combined_data = pd.concat([X[col], test[col]], axis=0).astype(str)\n    le.fit(combined_data)\n    X[col] = le.transform(X[col].astype(str))\n    test[col] = le.transform(test[col].astype(str))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:16:08.733471Z","iopub.execute_input":"2025-10-08T18:16:08.734543Z","iopub.status.idle":"2025-10-08T18:16:13.946334Z","shell.execute_reply.started":"2025-10-08T18:16:08.734489Z","shell.execute_reply":"2025-10-08T18:16:13.945077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kf = KFold(n_splits=5, shuffle=True, random_state=42)\noof = np.zeros(len(X))\npreds = np.zeros(len(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:17:26.457546Z","iopub.execute_input":"2025-10-08T18:17:26.457942Z","iopub.status.idle":"2025-10-08T18:17:26.464948Z","shell.execute_reply.started":"2025-10-08T18:17:26.457918Z","shell.execute_reply":"2025-10-08T18:17:26.463861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {\n    'objective': 'regression',\n    'metric' : 'rmse',\n    'boosting_type' : 'gbdt',\n    'learning_rate' : 0.05,\n    'num_leaves' : 31,\n    'feature_fraction' : 0.8,\n    'bagging_fraction' : 0.8,\n    'bagging_freq' : 5,\n    'seed' : 42,\n    'verbose' : -1\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:21:07.437750Z","iopub.execute_input":"2025-10-08T18:21:07.438161Z","iopub.status.idle":"2025-10-08T18:21:07.443958Z","shell.execute_reply.started":"2025-10-08T18:21:07.438137Z","shell.execute_reply":"2025-10-08T18:21:07.442978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for fold, (train_idx, val_idx) in enumerate(kf.split(X, y)):\n    print(f\"\\n===== Fold {fold+1} =====\")\n    X_train, X_val = X.iloc[train_idx], X.iloc[val_idx]\n    y_train, y_val = y.iloc[train_idx], y.iloc[val_idx]\n\n    dtrain = lgb.Dataset(X_train, y_train)\n    dvalid = lgb.Dataset(X_val, y_val, reference=dtrain)\n\n    model = lgb.train(\n        params,\n        dtrain,\n        valid_sets=[dtrain, dvalid],\n        num_boost_round=10000,\n        \n    ) \n    oof[val_idx] = model.predict(X_val, num_iteration=model.best_iteration)\n    preds += model.predict(test, num_iteration=model.best_iteration) / kf.n_splits\n    \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T18:39:14.399052Z","iopub.execute_input":"2025-10-08T18:39:14.399437Z","iopub.status.idle":"2025-10-08T19:20:26.725804Z","shell.execute_reply.started":"2025-10-08T18:39:14.399407Z","shell.execute_reply":"2025-10-08T19:20:26.724853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rmse = np.sqrt(mean_squared_error(y, oof))\nprint(f\"\\nOverall RMSE: {rmse:.5f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T19:41:23.107927Z","iopub.execute_input":"2025-10-08T19:41:23.108637Z","iopub.status.idle":"2025-10-08T19:41:23.133908Z","shell.execute_reply.started":"2025-10-08T19:41:23.108593Z","shell.execute_reply":"2025-10-08T19:41:23.132810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    ID_COL: test_ids,\n    TARGET: preds\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"\\nSubmission file saved as 'submission.csv'\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-08T19:42:41.492498Z","iopub.execute_input":"2025-10-08T19:42:41.492836Z","iopub.status.idle":"2025-10-08T19:42:42.609978Z","shell.execute_reply.started":"2025-10-08T19:42:41.492812Z","shell.execute_reply":"2025-10-08T19:42:42.608974Z"}},"outputs":[],"execution_count":null}]}