{"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":112509,"databundleVersionId":14254895,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-02T14:58:56.593683Z","iopub.execute_input":"2026-01-02T14:58:56.593939Z","iopub.status.idle":"2026-01-02T14:58:58.014527Z","shell.execute_reply.started":"2026-01-02T14:58:56.593858Z","shell.execute_reply":"2026-01-02T14:58:58.013711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:04:02.398169Z","iopub.execute_input":"2026-01-02T15:04:02.398398Z","iopub.status.idle":"2026-01-02T15:04:03.296992Z","shell.execute_reply.started":"2026-01-02T15:04:02.398384Z","shell.execute_reply":"2026-01-02T15:04:03.295998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path = \"/kaggle/input/csiro-biomass/train.csv\"\ntrain_df = pd.read_csv(train_path)\n\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:01:43.971755Z","iopub.execute_input":"2026-01-02T15:01:43.972051Z","iopub.status.idle":"2026-01-02T15:01:43.995063Z","shell.execute_reply.started":"2026-01-02T15:01:43.972029Z","shell.execute_reply":"2026-01-02T15:01:43.993915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = train_df.sample(frac=0.5, random_state=42).reset_index(drop=True)\n\nprint(\"Rows after downsampling:\", len(train_df))\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:03:07.158186Z","iopub.execute_input":"2026-01-02T15:03:07.158440Z","iopub.status.idle":"2026-01-02T15:03:07.173798Z","shell.execute_reply.started":"2026-01-02T15:03:07.158424Z","shell.execute_reply":"2026-01-02T15:03:07.172579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"Pre_GSHH_NDVI\",\n    \"Height_Ave_cm\",\n    \"State\",\n    \"Species\",\n    \"target_name\"\n]\n\nX = train_df[features]\ny = train_df[\"target\"]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:03:44.357425Z","iopub.execute_input":"2026-01-02T15:03:44.357755Z","iopub.status.idle":"2026-01-02T15:03:44.368124Z","shell.execute_reply.started":"2026-01-02T15:03:44.357733Z","shell.execute_reply":"2026-01-02T15:03:44.367148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_features = [\"Pre_GSHH_NDVI\", \"Height_Ave_cm\"]\ncat_features = [\"State\", \"Species\", \"target_name\"]\n\npreprocess = ColumnTransformer(\n    transformers=[\n        (\"num\", SimpleImputer(strategy=\"median\"), num_features),\n        (\"cat\", OneHotEncoder(handle_unknown=\"ignore\"), cat_features),\n    ]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:04:20.967797Z","iopub.execute_input":"2026-01-02T15:04:20.968195Z","iopub.status.idle":"2026-01-02T15:04:20.972240Z","shell.execute_reply.started":"2026-01-02T15:04:20.968178Z","shell.execute_reply":"2026-01-02T15:04:20.971701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_weights = {\n    \"Dry_Green_g\": 0.1,\n    \"Dry_Dead_g\": 0.1,\n    \"Dry_Clover_g\": 0.1,\n    \"GDM_g\": 0.2,\n    \"Dry_Total_g\": 0.5\n}\n\nweights = train_df[\"target_name\"].map(target_weights).values\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:05:55.037354Z","iopub.execute_input":"2026-01-02T15:05:55.037625Z","iopub.status.idle":"2026-01-02T15:05:55.045484Z","shell.execute_reply.started":"2026-01-02T15:05:55.037607Z","shell.execute_reply":"2026-01-02T15:05:55.044810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ndef weighted_r2(y_true, y_pred, weights):\n    y_bar = 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_bar) ** 2)\n    return 1 - ss_res / ss_tot\n    \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:06:06.049044Z","iopub.execute_input":"2026-01-02T15:06:06.050028Z","iopub.status.idle":"2026-01-02T15:06:06.057848Z","shell.execute_reply.started":"2026-01-02T15:06:06.049999Z","shell.execute_reply":"2026-01-02T15:06:06.056697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMRegressor\nfrom sklearn.model_selection import train_test_split\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:06:17.014461Z","iopub.execute_input":"2026-01-02T15:06:17.014769Z","iopub.status.idle":"2026-01-02T15:06:22.665170Z","shell.execute_reply.started":"2026-01-02T15:06:17.014751Z","shell.execute_reply":"2026-01-02T15:06:22.663947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val, w_train, w_val = train_test_split(\n    X, y, weights, test_size=0.5, random_state=42\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:17:23.905961Z","iopub.execute_input":"2026-01-02T15:17:23.906233Z","iopub.status.idle":"2026-01-02T15:17:23.913137Z","shell.execute_reply.started":"2026-01-02T15:17:23.906216Z","shell.execute_reply":"2026-01-02T15:17:23.912212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = LGBMRegressor(\n    n_estimators=500,\n    learning_rate=0.05,\n    num_leaves=31,\n    random_state=42\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:17:25.342406Z","iopub.execute_input":"2026-01-02T15:17:25.342722Z","iopub.status.idle":"2026-01-02T15:17:25.347370Z","shell.execute_reply.started":"2026-01-02T15:17:25.342699Z","shell.execute_reply":"2026-01-02T15:17:25.346109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pipeline = Pipeline(\n    steps=[\n        (\"preprocess\", preprocess),\n        (\"model\", model),\n    ]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:17:27.463126Z","iopub.execute_input":"2026-01-02T15:17:27.463427Z","iopub.status.idle":"2026-01-02T15:17:27.467628Z","shell.execute_reply.started":"2026-01-02T15:17:27.463411Z","shell.execute_reply":"2026-01-02T15:17:27.466742Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pipeline.fit(X_train, y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:17:35.301445Z","iopub.execute_input":"2026-01-02T15:17:35.301774Z","iopub.status.idle":"2026-01-02T15:17:35.454251Z","shell.execute_reply.started":"2026-01-02T15:17:35.301754Z","shell.execute_reply":"2026-01-02T15:17:35.452794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_preds = pipeline.predict(X_val)\nscore = weighted_r2(y_val.values, val_preds, w_val)\nprint(\"Weighted R²:\", score)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-02T15:17:49.325019Z","iopub.execute_input":"2026-01-02T15:17:49.325266Z","iopub.status.idle":"2026-01-02T15:17:49.346121Z","shell.execute_reply.started":"2026-01-02T15:17:49.325252Z","shell.execute_reply":"2026-01-02T15:17:49.345231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_path = \"/kaggle/input/csiro-biomass/test.csv\"\ntest_df = pd.read_csv(test_path)\n\nprint(test_df.shape)\ntest_df.head()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}