{"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":112509,"databundleVersionId":14254895,"sourceType":"competition"}],"dockerImageVersionId":31192,"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\n#for 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":"2026-01-25T16:30:58.143279Z","iopub.execute_input":"2026-01-25T16:30:58.143515Z","iopub.status.idle":"2026-01-25T16:31:00.757955Z","shell.execute_reply.started":"2026-01-25T16:30:58.143491Z","shell.execute_reply":"2026-01-25T16:31:00.757168Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Starting from this**","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\ntrain.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:18.962974Z","iopub.execute_input":"2026-01-25T16:31:18.963775Z","iopub.status.idle":"2026-01-25T16:31:19.026235Z","shell.execute_reply.started":"2026-01-25T16:31:18.963750Z","shell.execute_reply":"2026-01-25T16:31:19.025396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:22.333167Z","iopub.execute_input":"2026-01-25T16:31:22.333910Z","iopub.status.idle":"2026-01-25T16:31:22.338662Z","shell.execute_reply.started":"2026-01-25T16:31:22.333883Z","shell.execute_reply":"2026-01-25T16:31:22.337971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.dtypes\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:25.362783Z","iopub.execute_input":"2026-01-25T16:31:25.363551Z","iopub.status.idle":"2026-01-25T16:31:25.369138Z","shell.execute_reply.started":"2026-01-25T16:31:25.363523Z","shell.execute_reply":"2026-01-25T16:31:25.368584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['target_name'].unique()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:28.352619Z","iopub.execute_input":"2026-01-25T16:31:28.353351Z","iopub.status.idle":"2026-01-25T16:31:28.364389Z","shell.execute_reply.started":"2026-01-25T16:31:28.353323Z","shell.execute_reply":"2026-01-25T16:31:28.363776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['image_path'].value_counts().head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:31.972635Z","iopub.execute_input":"2026-01-25T16:31:31.973379Z","iopub.status.idle":"2026-01-25T16:31:31.982083Z","shell.execute_reply.started":"2026-01-25T16:31:31.973354Z","shell.execute_reply":"2026-01-25T16:31:31.981236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.groupby('target_name')['target'].describe()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:35.053137Z","iopub.execute_input":"2026-01-25T16:31:35.053683Z","iopub.status.idle":"2026-01-25T16:31:35.105873Z","shell.execute_reply.started":"2026-01-25T16:31:35.053659Z","shell.execute_reply":"2026-01-25T16:31:35.105061Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Understanding + Target Conditioning","metadata":{}},{"cell_type":"code","source":"train['Sampling_Date'] = pd.to_datetime(train['Sampling_Date'])\ntrain['month'] = train['Sampling_Date'].dt.month\ntrain['year'] = train['Sampling_Date'].dt.year\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:41.953398Z","iopub.execute_input":"2026-01-25T16:31:41.954173Z","iopub.status.idle":"2026-01-25T16:31:41.965933Z","shell.execute_reply.started":"2026-01-25T16:31:41.954145Z","shell.execute_reply":"2026-01-25T16:31:41.965243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.groupby('target_name')[['Pre_GSHH_NDVI', 'Height_Ave_cm']].describe()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:44.943044Z","iopub.execute_input":"2026-01-25T16:31:44.943353Z","iopub.status.idle":"2026-01-25T16:31:44.986318Z","shell.execute_reply.started":"2026-01-25T16:31:44.943330Z","shell.execute_reply":"2026-01-25T16:31:44.985650Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[['Pre_GSHH_NDVI', 'Height_Ave_cm', 'target']].corr()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:48.417765Z","iopub.execute_input":"2026-01-25T16:31:48.418345Z","iopub.status.idle":"2026-01-25T16:31:48.429163Z","shell.execute_reply.started":"2026-01-25T16:31:48.418323Z","shell.execute_reply":"2026-01-25T16:31:48.428600Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Baseline Model","metadata":{}},{"cell_type":"code","source":"train['image_id'] = train['image_path'].apply(lambda x: x.split('/')[-1])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:52.407912Z","iopub.execute_input":"2026-01-25T16:31:52.408676Z","iopub.status.idle":"2026-01-25T16:31:52.413929Z","shell.execute_reply.started":"2026-01-25T16:31:52.408650Z","shell.execute_reply":"2026-01-25T16:31:52.413134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nfor col in ['State', 'Species', 'target_name']:\n    le = LabelEncoder()\n    train[col] = le.fit_transform(train[col])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:31:59.342816Z","iopub.execute_input":"2026-01-25T16:31:59.343398Z","iopub.status.idle":"2026-01-25T16:31:59.902455Z","shell.execute_reply.started":"2026-01-25T16:31:59.343374Z","shell.execute_reply":"2026-01-25T16:31:59.901611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    'Pre_GSHH_NDVI',\n    'Height_Ave_cm',\n    'State',\n    'Species',\n    'month',\n    'target_name'\n]\n\nX = train[features]\ny = train['target']\ngroups = train['image_id']\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:06.883642Z","iopub.execute_input":"2026-01-25T16:32:06.884588Z","iopub.status.idle":"2026-01-25T16:32:06.889948Z","shell.execute_reply.started":"2026-01-25T16:32:06.884563Z","shell.execute_reply":"2026-01-25T16:32:06.889080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:10.342719Z","iopub.execute_input":"2026-01-25T16:32:10.343320Z","iopub.status.idle":"2026-01-25T16:32:10.352983Z","shell.execute_reply.started":"2026-01-25T16:32:10.343296Z","shell.execute_reply":"2026-01-25T16:32:10.352079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"groups.nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:13.187657Z","iopub.execute_input":"2026-01-25T16:32:13.188407Z","iopub.status.idle":"2026-01-25T16:32:13.193586Z","shell.execute_reply.started":"2026-01-25T16:32:13.188379Z","shell.execute_reply":"2026-01-25T16:32:13.192681Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Tabular-Only Baseline Model","metadata":{}},{"cell_type":"code","source":"target_weight_map = {\n    0: 0.1,  # Dry_Clover_g\n    1: 0.1,  # Dry_Dead_g\n    2: 0.1,  # Dry_Green_g\n    3: 0.5,  # Dry_Total_g\n    4: 0.2   # GDM_g\n}\n\nweights = train['target_name'].map(target_weight_map)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:17.608107Z","iopub.execute_input":"2026-01-25T16:32:17.608430Z","iopub.status.idle":"2026-01-25T16:32:17.615389Z","shell.execute_reply.started":"2026-01-25T16:32:17.608409Z","shell.execute_reply":"2026-01-25T16:32:17.614623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ndef weighted_r2(y_true, y_pred, weights):\n    y_true = np.array(y_true)\n    y_pred = np.array(y_pred)\n    weights = np.array(weights)\n\n    y_mean = np.average(y_true, weights=weights)\n\n    ss_res = np.sum(weights * (y_true - y_pred) ** 2)\n    ss_tot = np.sum(weights * (y_true - y_mean) ** 2)\n\n    return 1 - (ss_res / ss_tot)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:21.252854Z","iopub.execute_input":"2026-01-25T16:32:21.253455Z","iopub.status.idle":"2026-01-25T16:32:21.257930Z","shell.execute_reply.started":"2026-01-25T16:32:21.253431Z","shell.execute_reply":"2026-01-25T16:32:21.257118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import GroupKFold\nfrom catboost import CatBoostRegressor\n\ngkf = GroupKFold(n_splits=5)\n\noof_preds = np.zeros(len(train))\n\nfor fold, (train_idx, val_idx) in enumerate(gkf.split(X, y, groups)):\n    print(f\"Fold {fold + 1}\")\n\n    X_tr, X_val = X.iloc[train_idx], X.iloc[val_idx]\n    y_tr, y_val = y.iloc[train_idx], y.iloc[val_idx]\n\n    model = CatBoostRegressor(\n        iterations=1000,\n        learning_rate=0.05,\n        depth=6,\n        loss_function='RMSE',\n        verbose=False,\n        random_seed=42\n    )\n\n    model.fit(X_tr, y_tr)\n\n    oof_preds[val_idx] = model.predict(X_val)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:32.138442Z","iopub.execute_input":"2026-01-25T16:32:32.138964Z","iopub.status.idle":"2026-01-25T16:32:36.685854Z","shell.execute_reply.started":"2026-01-25T16:32:32.138938Z","shell.execute_reply":"2026-01-25T16:32:36.685213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv_score = weighted_r2(\n    y_true=y,\n    y_pred=oof_preds,\n    weights=weights\n)\n\nprint(\"Weighted R2 (Tabular-only CV):\", cv_score)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:42.152696Z","iopub.execute_input":"2026-01-25T16:32:42.153306Z","iopub.status.idle":"2026-01-25T16:32:42.157883Z","shell.execute_reply.started":"2026-01-25T16:32:42.153284Z","shell.execute_reply":"2026-01-25T16:32:42.157314Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Sample Weight ","metadata":{}},{"cell_type":"code","source":"sample_weights = weights.values\n\nmodel.fit(\n    X_tr, y_tr,\n    sample_weight=sample_weights[train_idx]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:46.583274Z","iopub.execute_input":"2026-01-25T16:32:46.583992Z","iopub.status.idle":"2026-01-25T16:32:47.206722Z","shell.execute_reply.started":"2026-01-25T16:32:46.583970Z","shell.execute_reply":"2026-01-25T16:32:47.205896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = CatBoostRegressor(\n    iterations=2000,\n    learning_rate=0.03,\n    depth=8,\n    loss_function='RMSE',\n    random_seed=42,\n    verbose=False\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:51.519384Z","iopub.execute_input":"2026-01-25T16:32:51.520114Z","iopub.status.idle":"2026-01-25T16:32:51.523933Z","shell.execute_reply.started":"2026-01-25T16:32:51.520090Z","shell.execute_reply":"2026-01-25T16:32:51.523159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['oof'] = oof_preds\n\ntrain.groupby('target_name').apply(\n    lambda df: weighted_r2(\n        df['target'],\n        df['oof'],\n        np.ones(len(df))\n    )\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:32:54.832856Z","iopub.execute_input":"2026-01-25T16:32:54.833157Z","iopub.status.idle":"2026-01-25T16:32:54.845331Z","shell.execute_reply.started":"2026-01-25T16:32:54.833135Z","shell.execute_reply":"2026-01-25T16:32:54.844471Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Sample-Weighted CatBoost","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\ntarget_weight_map = {\n    0: 0.1,  # Dry_Clover_g\n    1: 0.1,  # Dry_Dead_g\n    2: 0.1,  # Dry_Green_g\n    3: 0.5,  # Dry_Total_g\n    4: 0.2   # GDM_g\n}\n\nweights = train['target_name'].map(target_weight_map).values\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:33:04.738961Z","iopub.execute_input":"2026-01-25T16:33:04.739528Z","iopub.status.idle":"2026-01-25T16:33:04.744355Z","shell.execute_reply.started":"2026-01-25T16:33:04.739504Z","shell.execute_reply":"2026-01-25T16:33:04.743495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def weighted_r2(y_true, y_pred, weights):\n    y_true = np.array(y_true)\n    y_pred = np.array(y_pred)\n    weights = np.array(weights)\n\n    y_mean = np.average(y_true, weights=weights)\n    ss_res = np.sum(weights * (y_true - y_pred) ** 2)\n    ss_tot = np.sum(weights * (y_true - y_mean) ** 2)\n\n    return 1 - ss_res / ss_tot\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:33:08.052692Z","iopub.execute_input":"2026-01-25T16:33:08.053452Z","iopub.status.idle":"2026-01-25T16:33:08.057725Z","shell.execute_reply.started":"2026-01-25T16:33:08.053428Z","shell.execute_reply":"2026-01-25T16:33:08.057085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import GroupKFold\nfrom catboost import CatBoostRegressor\n\ngkf = GroupKFold(n_splits=5)\n\noof_preds = np.zeros(len(train))\n\nfor fold, (train_idx, val_idx) in enumerate(gkf.split(X, y, groups)):\n    print(f\"Fold {fold + 1}\")\n\n    X_tr, X_val = X.iloc[train_idx], X.iloc[val_idx]\n    y_tr, y_val = y.iloc[train_idx], y.iloc[val_idx]\n\n    w_tr = weights[train_idx]\n\n    model = CatBoostRegressor(\n        iterations=2000,\n        learning_rate=0.03,\n        depth=8,\n        loss_function='RMSE',\n        random_seed=42,\n        verbose=False\n    )\n\n    model.fit(\n        X_tr, y_tr,\n        sample_weight=w_tr\n    )\n\n    oof_preds[val_idx] = model.predict(X_val)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:33:33.012794Z","iopub.execute_input":"2026-01-25T16:33:33.013382Z","iopub.status.idle":"2026-01-25T16:33:43.509864Z","shell.execute_reply.started":"2026-01-25T16:33:33.013356Z","shell.execute_reply":"2026-01-25T16:33:43.509243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv_score = weighted_r2(\n    y_true=y,\n    y_pred=oof_preds,\n    weights=weights\n)\n\nprint(\"Weighted R2 (sample-weighted CV):\", cv_score)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:33:47.717714Z","iopub.execute_input":"2026-01-25T16:33:47.718000Z","iopub.status.idle":"2026-01-25T16:33:47.723054Z","shell.execute_reply.started":"2026-01-25T16:33:47.717978Z","shell.execute_reply":"2026-01-25T16:33:47.722412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['oof_weighted'] = oof_preds\n\nfor t in sorted(train['target_name'].unique()):\n    mask = train['target_name'] == t\n    r2 = weighted_r2(\n        train.loc[mask, 'target'],\n        train.loc[mask, 'oof_weighted'],\n        np.ones(mask.sum())\n    )\n    print(f\"Target {t} R2:\", r2)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T16:33:56.138163Z","iopub.execute_input":"2026-01-25T16:33:56.138747Z","iopub.status.idle":"2026-01-25T16:33:56.151717Z","shell.execute_reply.started":"2026-01-25T16:33:56.138723Z","shell.execute_reply":"2026-01-25T16:33:56.150861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_ROOT = \"/kaggle/input/csiro-biomass\"\n\ntrain['full_image_path'] = train['image_path'].apply(\n    lambda x: f\"{DATA_ROOT}/{x}\"\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:04:21.005736Z","iopub.execute_input":"2026-01-25T17:04:21.006610Z","iopub.status.idle":"2026-01-25T17:04:21.011864Z","shell.execute_reply.started":"2026-01-25T17:04:21.006586Z","shell.execute_reply":"2026-01-25T17:04:21.011212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train['full_image_path'].iloc[0])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:04:38.595462Z","iopub.execute_input":"2026-01-25T17:04:38.595754Z","iopub.status.idle":"2026-01-25T17:04:38.600527Z","shell.execute_reply.started":"2026-01-25T17:04:38.595734Z","shell.execute_reply":"2026-01-25T17:04:38.599875Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train & Evaluate Image + Tabular Model","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\">\n    <b>Note:</b> Tried pretrained CNN embeddings (ResNet18). CV dropped to 0.756, so image features were discarded.\n</div>\n","metadata":{}},{"cell_type":"markdown","source":"# Final SubMission Model","metadata":{}},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/csiro-biomass/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:39:07.524491Z","iopub.execute_input":"2026-01-25T17:39:07.525121Z","iopub.status.idle":"2026-01-25T17:39:07.533421Z","shell.execute_reply.started":"2026-01-25T17:39:07.525096Z","shell.execute_reply":"2026-01-25T17:39:07.532677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test.columns)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:39:10.895908Z","iopub.execute_input":"2026-01-25T17:39:10.896524Z","iopub.status.idle":"2026-01-25T17:39:10.900411Z","shell.execute_reply.started":"2026-01-25T17:39:10.896500Z","shell.execute_reply":"2026-01-25T17:39:10.899670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_meta_cols = [\n    'image_path',\n    'Pre_GSHH_NDVI',\n    'Height_Ave_cm',\n    'State',\n    'Species',\n    'Sampling_Date'\n]\n\nimg_meta = (\n    train[img_meta_cols]\n    .drop_duplicates()\n    .reset_index(drop=True)\n)\n\nprint(img_meta.shape)  \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:41:54.929189Z","iopub.execute_input":"2026-01-25T17:41:54.929556Z","iopub.status.idle":"2026-01-25T17:41:54.939245Z","shell.execute_reply.started":"2026-01-25T17:41:54.929534Z","shell.execute_reply":"2026-01-25T17:41:54.938486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = test.merge(\n    img_meta,\n    on='image_path',\n    how='left'\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:42:05.782915Z","iopub.execute_input":"2026-01-25T17:42:05.783265Z","iopub.status.idle":"2026-01-25T17:42:05.791307Z","shell.execute_reply.started":"2026-01-25T17:42:05.783242Z","shell.execute_reply":"2026-01-25T17:42:05.790468Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.isna().sum()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:42:18.412341Z","iopub.execute_input":"2026-01-25T17:42:18.412921Z","iopub.status.idle":"2026-01-25T17:42:18.419152Z","shell.execute_reply.started":"2026-01-25T17:42:18.412895Z","shell.execute_reply":"2026-01-25T17:42:18.418515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in ['Pre_GSHH_NDVI', 'Height_Ave_cm']:\n    test[col] = test[col].fillna(train[col].median())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:43:40.674153Z","iopub.execute_input":"2026-01-25T17:43:40.674750Z","iopub.status.idle":"2026-01-25T17:43:40.680256Z","shell.execute_reply.started":"2026-01-25T17:43:40.674723Z","shell.execute_reply":"2026-01-25T17:43:40.679411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in ['State', 'Species']:\n    test[col] = test[col].fillna(train[col].mode()[0])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:43:50.515065Z","iopub.execute_input":"2026-01-25T17:43:50.515660Z","iopub.status.idle":"2026-01-25T17:43:50.527913Z","shell.execute_reply.started":"2026-01-25T17:43:50.515634Z","shell.execute_reply":"2026-01-25T17:43:50.527102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"median_date = pd.to_datetime(train['Sampling_Date']).median()\ntest['Sampling_Date'] = test['Sampling_Date'].fillna(median_date)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:44:00.392527Z","iopub.execute_input":"2026-01-25T17:44:00.393046Z","iopub.status.idle":"2026-01-25T17:44:00.401814Z","shell.execute_reply.started":"2026-01-25T17:44:00.393022Z","shell.execute_reply":"2026-01-25T17:44:00.401038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.isna().sum()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:44:08.785308Z","iopub.execute_input":"2026-01-25T17:44:08.785971Z","iopub.status.idle":"2026-01-25T17:44:08.792151Z","shell.execute_reply.started":"2026-01-25T17:44:08.785945Z","shell.execute_reply":"2026-01-25T17:44:08.791541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Sampling_Date'] = pd.to_datetime(test['Sampling_Date'])\ntest['month'] = test['Sampling_Date'].dt.month\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:45:01.132319Z","iopub.execute_input":"2026-01-25T17:45:01.133039Z","iopub.status.idle":"2026-01-25T17:45:01.138278Z","shell.execute_reply.started":"2026-01-25T17:45:01.133015Z","shell.execute_reply":"2026-01-25T17:45:01.137559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"le_state = LabelEncoder()\nle_species = LabelEncoder()\nle_target = LabelEncoder()\n\ntrain['State'] = le_state.fit_transform(train['State'])\ntrain['Species'] = le_species.fit_transform(train['Species'])\ntrain['target_name'] = le_target.fit_transform(train['target_name'])\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:46:17.709101Z","iopub.execute_input":"2026-01-25T17:46:17.709705Z","iopub.status.idle":"2026-01-25T17:46:17.716252Z","shell.execute_reply.started":"2026-01-25T17:46:17.709677Z","shell.execute_reply":"2026-01-25T17:46:17.715597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_map = {\n    'Dry_Clover_g': 0,\n    'Dry_Dead_g': 1,\n    'Dry_Green_g': 2,\n    'Dry_Total_g': 3,\n    'GDM_g': 4\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:47:53.148058Z","iopub.execute_input":"2026-01-25T17:47:53.148603Z","iopub.status.idle":"2026-01-25T17:47:53.152472Z","shell.execute_reply.started":"2026-01-25T17:47:53.148579Z","shell.execute_reply":"2026-01-25T17:47:53.151602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['target_name'] = train['target_name'].map(target_map)\ntest['target_name'] = test['target_name'].map(target_map)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:48:06.014918Z","iopub.execute_input":"2026-01-25T17:48:06.015273Z","iopub.status.idle":"2026-01-25T17:48:06.021629Z","shell.execute_reply.started":"2026-01-25T17:48:06.015250Z","shell.execute_reply":"2026-01-25T17:48:06.020836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['target_name'].unique()\ntest['target_name'].unique()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:48:17.963172Z","iopub.execute_input":"2026-01-25T17:48:17.963470Z","iopub.status.idle":"2026-01-25T17:48:17.968848Z","shell.execute_reply.started":"2026-01-25T17:48:17.963452Z","shell.execute_reply":"2026-01-25T17:48:17.968237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['State'] = le_state.transform(test['State'])\ntest['Species'] = le_species.transform(test['Species'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:48:31.018958Z","iopub.execute_input":"2026-01-25T17:48:31.019396Z","iopub.status.idle":"2026-01-25T17:48:31.024903Z","shell.execute_reply.started":"2026-01-25T17:48:31.019373Z","shell.execute_reply":"2026-01-25T17:48:31.024121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    'Pre_GSHH_NDVI',\n    'Height_Ave_cm',\n    'State',\n    'Species',\n    'month',\n    'target_name'\n]\n\nX_test = test[features]\nprint(X_test.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T17:48:44.220165Z","iopub.execute_input":"2026-01-25T17:48:44.220535Z","iopub.status.idle":"2026-01-25T17:48:44.227381Z","shell.execute_reply.started":"2026-01-25T17:48:44.220512Z","shell.execute_reply":"2026-01-25T17:48:44.226451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from catboost import CatBoostRegressor\n\nfinal_model = CatBoostRegressor(\n    iterations=1500,\n    learning_rate=0.03,\n    depth=7,\n    loss_function='RMSE',\n    random_seed=42,\n    verbose=False\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:18:21.604833Z","iopub.execute_input":"2026-01-25T18:18:21.605414Z","iopub.status.idle":"2026-01-25T18:18:21.609526Z","shell.execute_reply.started":"2026-01-25T18:18:21.605388Z","shell.execute_reply":"2026-01-25T18:18:21.608886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_model.fit(X, y)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:18:35.627095Z","iopub.execute_input":"2026-01-25T18:18:35.627803Z","iopub.status.idle":"2026-01-25T18:18:36.939671Z","shell.execute_reply.started":"2026-01-25T18:18:35.627778Z","shell.execute_reply":"2026-01-25T18:18:36.939010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['target'] = final_model.predict(X_test)\ntest['target'] = test['target'].clip(lower=0)\n\nsubmission = test[['sample_id', 'target']]\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:18:40.019377Z","iopub.execute_input":"2026-01-25T18:18:40.019912Z","iopub.status.idle":"2026-01-25T18:18:40.027761Z","shell.execute_reply.started":"2026-01-25T18:18:40.019887Z","shell.execute_reply":"2026-01-25T18:18:40.027238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:18:43.097579Z","iopub.execute_input":"2026-01-25T18:18:43.098144Z","iopub.status.idle":"2026-01-25T18:18:43.105420Z","shell.execute_reply.started":"2026-01-25T18:18:43.098121Z","shell.execute_reply":"2026-01-25T18:18:43.104758Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# check for missing values\nsubmission.isna().sum()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:18:47.913360Z","iopub.execute_input":"2026-01-25T18:18:47.913926Z","iopub.status.idle":"2026-01-25T18:18:47.919948Z","shell.execute_reply.started":"2026-01-25T18:18:47.913901Z","shell.execute_reply":"2026-01-25T18:18:47.919221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}