{"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":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport random\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\n\n# Reproducibility\nSEED = 42\nrandom.seed(SEED)\nnp.random.seed(SEED)\n\n# Automatically find the competition dataset folder\nINPUT_ROOT = Path(\"/kaggle/input\")\nOUTPUT_ROOT = Path(\"/kaggle/working/\")\n\n\n\ndef find_data_root():\n    for p in INPUT_ROOT.iterdir():\n        if p.is_dir():\n            train_file = p / \"train.csv\"\n            test_file = p / \"test.csv\"\n            if train_file.exists() and test_file.exists():\n                return p\n    raise FileNotFoundError(\"Could not find dataset with train.csv and test.csv under /kaggle/input\")\n\nDATA_DIR = find_data_root()\nprint(\"Using data directory:\", DATA_DIR)\n\ntrain_path = DATA_DIR / \"train.csv\"\ntest_path = DATA_DIR / \"test.csv\"\nsample_sub_path = DATA_DIR / \"sample_submission.csv\"\n\nprint(train_path)\nprint(test_path)\nprint(sample_sub_path)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-16T21:33:49.762127Z","iopub.execute_input":"2025-11-16T21:33:49.762467Z","iopub.status.idle":"2025-11-16T21:33:49.771406Z","shell.execute_reply.started":"2025-11-16T21:33:49.762443Z","shell.execute_reply":"2025-11-16T21:33:49.770225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(train_path)\ntest = pd.read_csv(test_path)\nsample_sub = pd.read_csv(sample_sub_path)\n\nprint(\"train shape:\", train.shape)\nprint(\"test shape:\", test.shape)\nprint(\"sample_sub shape:\", sample_sub.shape)\n\ndisplay(train.head())\ndisplay(test.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T21:33:49.773156Z","iopub.execute_input":"2025-11-16T21:33:49.773421Z","iopub.status.idle":"2025-11-16T21:33:49.822344Z","shell.execute_reply.started":"2025-11-16T21:33:49.773399Z","shell.execute_reply":"2025-11-16T21:33:49.821374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Target names and their weights (from competition description)\nTARGET_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\nTARGETS = list(TARGET_WEIGHTS.keys())\nprint(\"Targets:\", TARGETS)\nprint(\"Weights:\", TARGET_WEIGHTS)\n\n\ndef weighted_r2_score(df, target_col=\"target\", pred_col=\"pred\", name_col=\"target_name\"):\n    \"\"\"\n    Compute globally weighted R^2 over all rows.\n\n    df must have: columns [target_col, pred_col, name_col]\n    name_col values are used to look up weights in TARGET_WEIGHTS.\n    \"\"\"\n    y_true = df[target_col].astype(float).values\n    y_pred = df[pred_col].astype(float).values\n    names = df[name_col].values\n\n    # Row-wise weights based on target_name\n    w = np.array([TARGET_WEIGHTS[n] for n in names], dtype=float)\n\n    # Weighted mean of y_true\n    y_mean = np.sum(w * y_true) / np.sum(w)\n\n    # Weighted SSE and SST\n    sse = np.sum(w * (y_true - y_pred) ** 2)\n    sst = np.sum(w * (y_true - y_mean) ** 2)\n\n    if sst == 0:\n        return 0.0\n\n    return 1.0 - sse / sst\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T21:33:49.823349Z","iopub.execute_input":"2025-11-16T21:33:49.823676Z","iopub.status.idle":"2025-11-16T21:33:49.831733Z","shell.execute_reply.started":"2025-11-16T21:33:49.823650Z","shell.execute_reply":"2025-11-16T21:33:49.830616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compute mean biomass per target_name\ntarget_means = (\n    train.groupby(\"target_name\")[\"target\"]\n    .mean()\n    .to_dict()\n)\n\nprint(\"Per-target means:\")\nfor k, v in target_means.items():\n    print(f\"  {k}: {v:.3f}\")\n\n# In-sample evaluation using the weighted R^2\ntrain_eval = train.copy()\ntrain_eval[\"pred\"] = train_eval[\"target_name\"].map(target_means)\n\nbaseline_score = weighted_r2_score(train_eval, target_col=\"target\", pred_col=\"pred\", name_col=\"target_name\")\nprint(f\"\\nBaseline weighted R^2 on train: {baseline_score:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T21:33:49.832798Z","iopub.execute_input":"2025-11-16T21:33:49.833143Z","iopub.status.idle":"2025-11-16T21:33:49.864213Z","shell.execute_reply.started":"2025-11-16T21:33:49.833115Z","shell.execute_reply":"2025-11-16T21:33:49.863349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create submission by mapping target_name -> mean target\nsubmission = test.copy()\n\nsubmission[\"target\"] = submission[\"target_name\"].map(target_means)\n\n# Just in case (shouldn't happen): fill any missing with global mean\nglobal_mean = train[\"target\"].mean()\nsubmission[\"target\"] = submission[\"target\"].fillna(global_mean)\n\n# Keep required columns, ensure correct order\nsubmission = submission[[\"sample_id\", \"target\"]]\n\ndisplay(submission.head())\nprint(\"Submission shape:\", submission.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T21:33:49.865893Z","iopub.execute_input":"2025-11-16T21:33:49.866271Z","iopub.status.idle":"2025-11-16T21:33:49.888851Z","shell.execute_reply.started":"2025-11-16T21:33:49.866249Z","shell.execute_reply":"2025-11-16T21:33:49.887896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_path = \"submission.csv\"\nsubmission.to_csv(submission_path, index=False)\nprint(\"Saved submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T21:35:59.423548Z","iopub.execute_input":"2025-11-16T21:35:59.424374Z","iopub.status.idle":"2025-11-16T21:35:59.431148Z","shell.execute_reply.started":"2025-11-16T21:35:59.424345Z","shell.execute_reply":"2025-11-16T21:35:59.430202Z"}},"outputs":[],"execution_count":null}]}