{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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":31260,"isInternetEnabled":true,"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\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":"2026-01-19T16:25:28.692156Z","iopub.execute_input":"2026-01-19T16:25:28.692482Z","iopub.status.idle":"2026-01-19T16:25:31.328906Z","shell.execute_reply.started":"2026-01-19T16:25:28.692453Z","shell.execute_reply":"2026-01-19T16:25:31.326924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ===============================================================\n#  CSIRO Biomass \n# ===============================================================\n\n# Ignore warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# Imports\nimport os\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.metrics import r2_score\nimport tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\nfrom tensorflow.keras.models import Model\n\n# ===============================================================\n# Load CSVs\n# ===============================================================\ntrain = pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/csiro-biomass/test.csv\")\n\n# ===============================================================\n# Simple Image Feature Extractor (EfficientNet)\n# ===============================================================\nbase_model = EfficientNetB0(weights=\"imagenet\", include_top=False, pooling=\"avg\")\ninp = tf.keras.Input(shape=(224,224,3))\nx = preprocess_input(inp)\nout = base_model(x)\nimg_model = Model(inp, out)\n\ndef extract_features(img_path):\n    try:\n        img = Image.open(f\"/kaggle/input/csiro-biomass/{img_path}\").convert(\"RGB\")\n        img = img.resize((224,224))\n        arr = np.expand_dims(np.array(img),0)\n        return img_model.predict(arr)[0]\n    except:\n        return np.zeros((img_model.output_shape[-1],))\n\n# Precompute image features\ntrain_imgs = np.vstack(train[\"image_path\"].apply(extract_features).values)\ntest_imgs  = np.vstack(test[\"image_path\"].apply(extract_features).values)\n\n# ===============================================================\n# Combine with Tabular Features\n# ===============================================================\n# Drop target columns in train (we predict them later)\ntargets = train[[\"Dry_Green_g\",\"Dry_Dead_g\",\"Dry_Clover_g\",\"GDM_g\",\"Dry_Total_g\"]]\ntrain = train.drop([\"Dry_Green_g\",\"Dry_Dead_g\",\"Dry_Clover_g\",\"GDM_g\",\"Dry_Total_g\"],axis=1)\n\n# Numeric\nnum_cols = [\"Height_Ave_cm\",\"Pre_GSHH_NDVI\"]\n# Categorical\ncat_cols = [\"State\",\"Species\"]\n\n# Preprocess\npreprocessor = ColumnTransformer([\n    (\"num\", \"passthrough\", num_cols),\n    (\"cat\", OneHotEncoder(handle_unknown=\"ignore\"), cat_cols)\n])\n\n# Augment with image features\nX_train = np.hstack([train_imgs, train[num_cols].values])\nX_test  = np.hstack([test_imgs, test[num_cols].values])\n\n# ===============================================================\n# Train Test Split\n# ===============================================================\nX_tr, X_val, y_tr, y_val = train_test_split(X_train, targets.values, test_size=0.15, random_state=42)\n\n# ===============================================================\n# Model per target\n# ===============================================================\npreds = {}\nfor i,col in enumerate(targets.columns):\n    model = GradientBoostingRegressor(random_state=42)\n    model.fit(X_tr, y_tr[:,i])\n    vpred = model.predict(X_val)\n    print(f\"{col} R2:\", r2_score(y_val[:,i], vpred))\n    preds[col] = model\n\n# ===============================================================\n# Train on full train and predict test\n# ===============================================================\nsubmission = pd.DataFrame({\"sample_id\": test[\"sample_id\"]})\n\nfor col,model in preds.items():\n    submission[col] = model.fit(X_train, targets[col].values).predict(X_test)\n\n# Melt to long format\nsub_long = submission.melt(id_vars=\"sample_id\",var_name=\"target_name\",value_name=\"target\")\nsub_long.to_csv(\"submission.csv\",index=False)\n\nprint(\"✅ submission.csv created\")\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T16:35:01.464985Z","iopub.execute_input":"2026-01-19T16:35:01.465236Z","iopub.status.idle":"2026-01-19T16:39:55.368313Z","shell.execute_reply.started":"2026-01-19T16:35:01.465205Z","shell.execute_reply":"2026-01-19T16:39:55.367337Z"}},"outputs":[],"execution_count":null}]}