{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},{"sourceId":14155999,"sourceType":"datasetVersion","datasetId":9022666},{"sourceId":14156032,"sourceType":"datasetVersion","datasetId":9022690}],"dockerImageVersionId":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:21:35.910918Z","iopub.execute_input":"2025-12-27T12:21:35.911248Z","iopub.status.idle":"2025-12-27T12:21:35.916487Z","shell.execute_reply.started":"2025-12-27T12:21:35.911226Z","shell.execute_reply":"2025-12-27T12:21:35.915307Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/csiro-biomass/train.csv')\ntest_data = pd.read_csv('/kaggle/input/csiro-biomass/test.csv')","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:21:37.157364Z","iopub.execute_input":"2025-12-27T12:21:37.157699Z","iopub.status.idle":"2025-12-27T12:21:37.171665Z","shell.execute_reply.started":"2025-12-27T12:21:37.157673Z","shell.execute_reply":"2025-12-27T12:21:37.170634Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:38.298945Z","iopub.execute_input":"2025-12-27T12:21:38.299266Z","iopub.status.idle":"2025-12-27T12:21:38.308921Z","shell.execute_reply.started":"2025-12-27T12:21:38.299246Z","shell.execute_reply":"2025-12-27T12:21:38.307907Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Test Image embedding ","metadata":{}},{"cell_type":"code","source":"# import torch\n# import pandas as pd\n# from PIL import Image\n# from transformers import CLIPModel, CLIPProcessor\n# import os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:42.001188Z","iopub.execute_input":"2025-12-27T12:21:42.001506Z","iopub.status.idle":"2025-12-27T12:21:42.006176Z","shell.execute_reply.started":"2025-12-27T12:21:42.001483Z","shell.execute_reply":"2025-12-27T12:21:42.005081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # -------- CONFIG --------\n# image_path = '/Users/abdul/Desktop/CSIRO - Image2Biomass Prediction/csiro-biomass/test/ID1001187975.jpg'   # path to image\n# output_csv = \"image_embedding.csv\"\n\n\n# # -------- FORCE CPU --------\n# device = torch.device(\"cpu\")\n\n# # -------- LOAD MODEL --------\n# model = CLIPModel.from_pretrained(\"openai/clip-vit-base-patch32\")\n# model.to(device)\n# model.eval()\n\n# processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-base-patch32\")\n\n\n# # -------- LOAD IMAGE --------\n# image = Image.open(image_path).convert(\"RGB\")\n# inputs = processor(images=image, return_tensors=\"pt\")\n# inputs = {k: v.to(device) for k, v in inputs.items()}\n\n\n# # -------- EMBEDDING --------\n# with torch.no_grad():\n#     embedding = model.get_image_features(**inputs)\n\n# # -------- TO DATAFRAME --------\n# embedding = embedding.cpu().numpy()  # shape: (1, 512)\n\n# df = pd.DataFrame(embedding, columns=[str(i) for i in range(512)])\n\n\n# # -------- SAVE --------\n# df.to_csv(output_csv, index=False)\n\n# print(\"Saved 512-dim embedding to:\", output_csv)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:42.430635Z","iopub.execute_input":"2025-12-27T12:21:42.430926Z","iopub.status.idle":"2025-12-27T12:21:42.436052Z","shell.execute_reply.started":"2025-12-27T12:21:42.430907Z","shell.execute_reply":"2025-12-27T12:21:42.434864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_embedding = pd.read_csv('/kaggle/input/test-img-embedding/test_image_embedding.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:46.920356Z","iopub.execute_input":"2025-12-27T12:21:46.920677Z","iopub.status.idle":"2025-12-27T12:21:46.940999Z","shell.execute_reply.started":"2025-12-27T12:21:46.920653Z","shell.execute_reply":"2025-12-27T12:21:46.939985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_embedding","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:48.449823Z","iopub.execute_input":"2025-12-27T12:21:48.450182Z","iopub.status.idle":"2025-12-27T12:21:48.468584Z","shell.execute_reply.started":"2025-12-27T12:21:48.450158Z","shell.execute_reply":"2025-12-27T12:21:48.467795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nimg_id = test_data.loc[0, \"sample_id\"].split(\"__\")[0]  # or from image_path\ntest_image_embedding = test_image_embedding.copy()\ntest_image_embedding.insert(0, \"img_id\", img_id)\n\nembed_cols = [str(i) for i in range(512)]\ntest_image_embedding.columns = [\"img_id\"] + embed_cols  # ensure correct names\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:50.193120Z","iopub.execute_input":"2025-12-27T12:21:50.194261Z","iopub.status.idle":"2025-12-27T12:21:50.201788Z","shell.execute_reply.started":"2025-12-27T12:21:50.194220Z","shell.execute_reply":"2025-12-27T12:21:50.200895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_embedding","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:21:54.810289Z","iopub.execute_input":"2025-12-27T12:21:54.810599Z","iopub.status.idle":"2025-12-27T12:21:54.830223Z","shell.execute_reply.started":"2025-12-27T12:21:54.810577Z","shell.execute_reply":"2025-12-27T12:21:54.829148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create img_id in test_data\ntest_data[\"img_id\"] = test_data[\"sample_id\"].str.split(\"__\").str[0]\n\n# Merge\nnew_test_df = test_data.merge(\n    test_image_embedding,\n    on=\"img_id\",\n    how=\"left\"\n)\n\nnew_test_df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:22:00.530503Z","iopub.execute_input":"2025-12-27T12:22:00.530902Z","iopub.status.idle":"2025-12-27T12:22:00.559213Z","shell.execute_reply.started":"2025-12-27T12:22:00.530877Z","shell.execute_reply":"2025-12-27T12:22:00.557928Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Test data pivoting and merging","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# 1. Pivot targets\ntargets_wide = (\n    test_data\n    .assign(value=0.0)  # placeholder for test set\n    .pivot_table(\n        index=\"img_id\",\n        columns=\"target_name\",\n        values=\"value\",\n        fill_value=0.0\n    )\n    .reset_index()\n)\n\n# 2. Attach img_id to embeddings\ntest_image_embedding = test_image_embedding.copy()\ntest_image_embedding[\"img_id\"] = test_data[\"img_id\"].unique()\n\n# 3. Merge\nmerged = targets_wide.merge(\n    test_image_embedding,\n    on=\"img_id\",\n    how=\"left\"\n)\n\n# 4. Reorder: targets first, embeddings after\ntarget_cols = [\n    \"Dry_Clover_g\",\n    \"Dry_Dead_g\",\n    \"Dry_Green_g\",\n    \"Dry_Total_g\",\n    \"GDM_g\"\n]\n\nembedding_cols = [c for c in merged.columns if c.isdigit()]\n\nfinal_test_df = merged[\n    [\"img_id\"] + target_cols + embedding_cols\n]\n\nfinal_test_df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:22:05.463474Z","iopub.execute_input":"2025-12-27T12:22:05.463805Z","iopub.status.idle":"2025-12-27T12:22:05.501898Z","shell.execute_reply.started":"2025-12-27T12:22:05.463780Z","shell.execute_reply":"2025-12-27T12:22:05.500735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Used kaggle notebook to do img embeddings for train data","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Merging of Train_data and Embedded Img data","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"image_embedding = pd.read_csv('/kaggle/input/train-embedding/train_embeddings.csv')","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:22:13.107188Z","iopub.execute_input":"2025-12-27T12:22:13.107551Z","iopub.status.idle":"2025-12-27T12:22:13.166255Z","shell.execute_reply.started":"2025-12-27T12:22:13.107528Z","shell.execute_reply":"2025-12-27T12:22:13.165219Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['img_id'] = train_data['sample_id'].str.split('__').str[0]\nimage_embedding['img_id'] = image_embedding['filename'].str.replace('.jpg', '', regex=False)\n\ntrain_needed = train_data[['img_id', 'Pre_GSHH_NDVI', 'Height_Ave_cm',\n                         'target_name', 'target']]\n\nembed_cols = [str(i) for i in range(504)]  # embedding dims\nemb_needed = image_embedding[['img_id'] + embed_cols]\n\nnew_df = train_needed.merge(emb_needed, on='img_id', how='left')\n","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:22:14.204860Z","iopub.execute_input":"2025-12-27T12:22:14.205266Z","iopub.status.idle":"2025-12-27T12:22:14.232878Z","shell.execute_reply.started":"2025-12-27T12:22:14.205241Z","shell.execute_reply":"2025-12-27T12:22:14.231287Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_df.shape","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:22:17.084910Z","iopub.execute_input":"2025-12-27T12:22:17.085888Z","iopub.status.idle":"2025-12-27T12:22:17.092278Z","shell.execute_reply.started":"2025-12-27T12:22:17.085850Z","shell.execute_reply":"2025-12-27T12:22:17.091380Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_df\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:22:17.612450Z","iopub.execute_input":"2025-12-27T12:22:17.612874Z","iopub.status.idle":"2025-12-27T12:22:17.641880Z","shell.execute_reply.started":"2025-12-27T12:22:17.612849Z","shell.execute_reply":"2025-12-27T12:22:17.640976Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Pivoting the target_name column","metadata":{}},{"cell_type":"code","source":"pivot_df = new_df.pivot_table(\n    index='img_id',\n    columns='target_name',\n    values='target'\n).reset_index()","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:22:30.935716Z","iopub.execute_input":"2025-12-27T12:22:30.936096Z","iopub.status.idle":"2025-12-27T12:22:30.951001Z","shell.execute_reply.started":"2025-12-27T12:22:30.936066Z","shell.execute_reply":"2025-12-27T12:22:30.950002Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pivot_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:22:34.765527Z","iopub.execute_input":"2025-12-27T12:22:34.765885Z","iopub.status.idle":"2025-12-27T12:22:34.778731Z","shell.execute_reply.started":"2025-12-27T12:22:34.765859Z","shell.execute_reply":"2025-12-27T12:22:34.777692Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# merging the pivot df and embedded_img_df","metadata":{}},{"cell_type":"code","source":"features_df = new_df.drop(columns=['target', 'target_name']).drop_duplicates('img_id') # Because we will be using pivot_df\n\nfinal_df = pivot_df.merge(features_df, on='img_id', how='left') # | target → becomes column values |\nfinal_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2025-12-27T12:23:09.350816Z","iopub.execute_input":"2025-12-27T12:23:09.351162Z","iopub.status.idle":"2025-12-27T12:23:09.382385Z","shell.execute_reply.started":"2025-12-27T12:23:09.351139Z","shell.execute_reply":"2025-12-27T12:23:09.381293Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:14:44.050865Z","iopub.execute_input":"2025-12-27T12:14:44.051206Z","iopub.status.idle":"2025-12-27T12:14:44.060974Z","shell.execute_reply.started":"2025-12-27T12:14:44.051181Z","shell.execute_reply":"2025-12-27T12:14:44.059644Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Generating NDVI value for test data","metadata":{}},{"cell_type":"code","source":"final_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:23:35.878404Z","iopub.execute_input":"2025-12-27T12:23:35.878858Z","iopub.status.idle":"2025-12-27T12:23:35.909557Z","shell.execute_reply.started":"2025-12-27T12:23:35.878833Z","shell.execute_reply":"2025-12-27T12:23:35.908117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# One-sentence takeaway (lock this in)\n\n# NDVI is used as the label to train a model that learns how to infer NDVI from image embeddings; it must never be used as an input feature in that stage.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:09.036917Z","iopub.execute_input":"2025-12-14T17:51:09.037247Z","iopub.status.idle":"2025-12-14T17:51:09.042185Z","shell.execute_reply.started":"2025-12-14T17:51:09.037212Z","shell.execute_reply":"2025-12-14T17:51:09.040966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# The rule (lock this in)\n\n# •If a variable exists in training does not exist in test but is correlated with the image \n# •Then you learn it from embeddings and predict it for test.","metadata":{}},{"cell_type":"code","source":"# --------------------------NDVI Predicted value--------------------------","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:09.043513Z","iopub.execute_input":"2025-12-14T17:51:09.043874Z","iopub.status.idle":"2025-12-14T17:51:09.069385Z","shell.execute_reply.started":"2025-12-14T17:51:09.043842Z","shell.execute_reply":"2025-12-14T17:51:09.068145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # -----Preparing Features------\n\n# import lightgbm as lgb\n# import numpy as np\n\n# # embedding columns only\n# EMBED_COLS = [c for c in final_df.columns if c.isdigit()]\n\n# X_train = final_df[EMBED_COLS].values\n# X_train = np.asarray(X_train, dtype=np.float32, order=\"C\")\n\n\n# X_test  = final_test_df[EMBED_COLS].values\n# X_test = np.asarray(X_test, dtype=np.float32, order=\"C\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:15:29.584851Z","iopub.execute_input":"2025-12-27T12:15:29.585195Z","iopub.status.idle":"2025-12-27T12:15:37.342152Z","shell.execute_reply.started":"2025-12-27T12:15:29.585170Z","shell.execute_reply":"2025-12-27T12:15:37.341064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Train NDVI model (from embeddings)\n\n# ndvi_model = lgb.LGBMRegressor(\n#     n_estimators=300,\n#     learning_rate=0.05,\n#     num_leaves=31,\n#     n_jobs=2\n# )\n\n# ndvi_model.fit(\n#     X_train,\n#     final_df[\"Pre_GSHH_NDVI\"].values\n# )\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:15:49.891281Z","iopub.execute_input":"2025-12-27T12:15:49.891618Z","iopub.status.idle":"2025-12-27T12:15:49.896548Z","shell.execute_reply.started":"2025-12-27T12:15:49.891594Z","shell.execute_reply":"2025-12-27T12:15:49.895395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ndvi_test_hat = ndvi_model.predict(X_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:15:53.781707Z","iopub.execute_input":"2025-12-27T12:15:53.782098Z","iopub.status.idle":"2025-12-27T12:15:53.787999Z","shell.execute_reply.started":"2025-12-27T12:15:53.782068Z","shell.execute_reply":"2025-12-27T12:15:53.786789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ndvi_test_hat","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:15:59.401915Z","iopub.execute_input":"2025-12-27T12:15:59.402548Z","iopub.status.idle":"2025-12-27T12:15:59.409165Z","shell.execute_reply.started":"2025-12-27T12:15:59.402520Z","shell.execute_reply":"2025-12-27T12:15:59.408308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #--------------------------Height_Ave_cm Predicted value--------------------------","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:18.541668Z","iopub.execute_input":"2025-12-14T17:51:18.542137Z","iopub.status.idle":"2025-12-14T17:51:18.562903Z","shell.execute_reply.started":"2025-12-14T17:51:18.542110Z","shell.execute_reply":"2025-12-14T17:51:18.561764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# height_model = lgb.LGBMRegressor(\n#     n_estimators=300,\n#     learning_rate=0.05,\n#     num_leaves=31,\n#     n_jobs=2\n# )\n\n# height_model.fit(\n#     X_train,                         # embeddings only\n#     final_df[\"Height_Ave_cm\"].values\n# )\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:18.563892Z","iopub.execute_input":"2025-12-14T17:51:18.564344Z","iopub.status.idle":"2025-12-14T17:51:20.932899Z","shell.execute_reply.started":"2025-12-14T17:51:18.564311Z","shell.execute_reply":"2025-12-14T17:51:20.931983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# height_test_hat = height_model.predict(X_test)\n# height_test_hat","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:20.934139Z","iopub.execute_input":"2025-12-14T17:51:20.934562Z","iopub.status.idle":"2025-12-14T17:51:20.942531Z","shell.execute_reply.started":"2025-12-14T17:51:20.934532Z","shell.execute_reply":"2025-12-14T17:51:20.941510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Adding two coulmns after predicting in test dataset","metadata":{}},{"cell_type":"code","source":"# final_test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:20.943536Z","iopub.execute_input":"2025-12-14T17:51:20.943873Z","iopub.status.idle":"2025-12-14T17:51:20.986480Z","shell.execute_reply.started":"2025-12-14T17:51:20.943846Z","shell.execute_reply":"2025-12-14T17:51:20.985478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# final_test_df[\"ndvi_test_hat\"] = ndvi_test_hat\n# final_test_df[\"height_test_hat\"] = height_test_hat\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:20.987588Z","iopub.execute_input":"2025-12-14T17:51:20.988080Z","iopub.status.idle":"2025-12-14T17:51:21.009700Z","shell.execute_reply.started":"2025-12-14T17:51:20.988053Z","shell.execute_reply":"2025-12-14T17:51:21.008361Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" # #Reordering columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:21.010957Z","iopub.execute_input":"2025-12-14T17:51:21.011242Z","iopub.status.idle":"2025-12-14T17:51:21.034187Z","shell.execute_reply.started":"2025-12-14T17:51:21.011220Z","shell.execute_reply":"2025-12-14T17:51:21.033038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cols = (\n#     [\"img_id\", \"ndvi_test_hat\", \"height_test_hat\"]\n#     + [\n#         c for c in final_test_df.columns\n#         if c not in [\"img_id\", \"ndvi_test_hat\", \"height_test_hat\"]\n#     ]\n# )\n\n# final_test_df = final_test_df[cols]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:21.035191Z","iopub.execute_input":"2025-12-14T17:51:21.035498Z","iopub.status.idle":"2025-12-14T17:51:21.057876Z","shell.execute_reply.started":"2025-12-14T17:51:21.035472Z","shell.execute_reply":"2025-12-14T17:51:21.056804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:21.059026Z","iopub.execute_input":"2025-12-14T17:51:21.059316Z","iopub.status.idle":"2025-12-14T17:51:21.080844Z","shell.execute_reply.started":"2025-12-14T17:51:21.059288Z","shell.execute_reply":"2025-12-14T17:51:21.079531Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Instantiaing LightGBM","metadata":{}},{"cell_type":"code","source":"final_df.head(1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:24:09.553203Z","iopub.execute_input":"2025-12-27T12:24:09.553578Z","iopub.status.idle":"2025-12-27T12:24:09.575002Z","shell.execute_reply.started":"2025-12-27T12:24:09.553548Z","shell.execute_reply":"2025-12-27T12:24:09.574102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:24:33.419341Z","iopub.execute_input":"2025-12-27T12:24:33.420266Z","iopub.status.idle":"2025-12-27T12:24:33.429970Z","shell.execute_reply.started":"2025-12-27T12:24:33.420225Z","shell.execute_reply":"2025-12-27T12:24:33.428936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:24:38.936486Z","iopub.execute_input":"2025-12-27T12:24:38.936804Z","iopub.status.idle":"2025-12-27T12:24:38.958904Z","shell.execute_reply.started":"2025-12-27T12:24:38.936775Z","shell.execute_reply":"2025-12-27T12:24:38.957572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nimport numpy as np\n\nTARGETS = [\n    \"Dry_Clover_g\",\n    \"Dry_Dead_g\",\n    \"Dry_Green_g\",\n    \"Dry_Total_g\",\n    \"GDM_g\"\n]\n\nEMBED_COLS = [c for c in final_df.columns if c.isdigit()]\n\nFEATURE_COLS = EMBED_COLS + [\"Pre_GSHH_NDVI\", \"Height_Ave_cm\"]\n\n\nX_train = final_df.drop(columns=TARGETS + [\"img_id\"])\n\nX_test = final_test_df.reindex(\n    columns=X_train.columns,\n    fill_value=0\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:45:35.970903Z","iopub.execute_input":"2025-12-27T12:45:35.971735Z","iopub.status.idle":"2025-12-27T12:45:35.979984Z","shell.execute_reply.started":"2025-12-27T12:45:35.971696Z","shell.execute_reply":"2025-12-27T12:45:35.978912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:24:49.832309Z","iopub.execute_input":"2025-12-27T12:24:49.832644Z","iopub.status.idle":"2025-12-27T12:24:49.856115Z","shell.execute_reply.started":"2025-12-27T12:24:49.832611Z","shell.execute_reply":"2025-12-27T12:24:49.854861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_df.head(1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:27:37.686979Z","iopub.execute_input":"2025-12-27T12:27:37.687317Z","iopub.status.idle":"2025-12-27T12:27:37.712156Z","shell.execute_reply.started":"2025-12-27T12:27:37.687296Z","shell.execute_reply":"2025-12-27T12:27:37.710883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.head(1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:28:16.168137Z","iopub.execute_input":"2025-12-27T12:28:16.168634Z","iopub.status.idle":"2025-12-27T12:28:16.179543Z","shell.execute_reply.started":"2025-12-27T12:28:16.168605Z","shell.execute_reply":"2025-12-27T12:28:16.178183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:30:36.836861Z","iopub.execute_input":"2025-12-27T12:30:36.837231Z","iopub.status.idle":"2025-12-27T12:30:36.852248Z","shell.execute_reply.started":"2025-12-27T12:30:36.837209Z","shell.execute_reply":"2025-12-27T12:30:36.850972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:39:13.050567Z","iopub.execute_input":"2025-12-27T12:39:13.050921Z","iopub.status.idle":"2025-12-27T12:39:13.067597Z","shell.execute_reply.started":"2025-12-27T12:39:13.050899Z","shell.execute_reply":"2025-12-27T12:39:13.065945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train.drop(columns = ['Pre_GSHH_NDVI', 'Height_Ave_cm'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:46:09.045822Z","iopub.execute_input":"2025-12-27T12:46:09.046212Z","iopub.status.idle":"2025-12-27T12:46:09.053952Z","shell.execute_reply.started":"2025-12-27T12:46:09.046185Z","shell.execute_reply":"2025-12-27T12:46:09.053074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train= X_train.assign(\n    sample_id=train_data['sample_id'],\n    image_path=train_data['image_path'],\n    target_name=train_data['target_name'],\n    target=train_data['target']\n)\n\nfront_cols = [\"sample_id\", \"image_path\", \"target_name\", \"target\"]\nX_train = X_train[front_cols + [c for c in X_train.columns if c not in front_cols]]\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:52:21.688794Z","iopub.execute_input":"2025-12-27T12:52:21.689140Z","iopub.status.idle":"2025-12-27T12:52:21.701493Z","shell.execute_reply.started":"2025-12-27T12:52:21.689115Z","shell.execute_reply":"2025-12-27T12:52:21.700043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:52:25.381380Z","iopub.execute_input":"2025-12-27T12:52:25.381728Z","iopub.status.idle":"2025-12-27T12:52:25.411127Z","shell.execute_reply.started":"2025-12-27T12:52:25.381690Z","shell.execute_reply":"2025-12-27T12:52:25.409956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = X_test.drop(columns = ['Pre_GSHH_NDVI', 'Height_Ave_cm'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:49:08.999509Z","iopub.execute_input":"2025-12-27T12:49:08.999864Z","iopub.status.idle":"2025-12-27T12:49:09.007632Z","shell.execute_reply.started":"2025-12-27T12:49:08.999839Z","shell.execute_reply":"2025-12-27T12:49:09.006506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T12:59:39.151608Z","iopub.execute_input":"2025-12-27T12:59:39.152705Z","iopub.status.idle":"2025-12-27T12:59:39.162769Z","shell.execute_reply.started":"2025-12-27T12:59:39.152673Z","shell.execute_reply":"2025-12-27T12:59:39.161290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = X_test.assign(sample_id=test_data['sample_id'],\n              image_path=test_data['image_path'],\n              target_name=test_data['target_name'],\n              img_id=test_data['img_id'],\n)\n\nfront_cols = [\"sample_id\", \"image_path\", \"target_name\", \"img_id\"]\nX_test = X_test[front_cols + [c for c in X_test.columns if c not in front_cols]]\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:02:29.722809Z","iopub.execute_input":"2025-12-27T13:02:29.723166Z","iopub.status.idle":"2025-12-27T13:02:29.734577Z","shell.execute_reply.started":"2025-12-27T13:02:29.723142Z","shell.execute_reply":"2025-12-27T13:02:29.733681Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:02:30.695258Z","iopub.execute_input":"2025-12-27T13:02:30.695576Z","iopub.status.idle":"2025-12-27T13:02:30.718377Z","shell.execute_reply.started":"2025-12-27T13:02:30.695556Z","shell.execute_reply":"2025-12-27T13:02:30.717106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_embeddings = pd.read_csv('/kaggle/input/train-embedding/train_embeddings.csv')\ntrain_embeddings = train_embeddings.drop(columns = 'filename')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:10:01.809446Z","iopub.execute_input":"2025-12-27T13:10:01.809789Z","iopub.status.idle":"2025-12-27T13:10:01.872262Z","shell.execute_reply.started":"2025-12-27T13:10:01.809765Z","shell.execute_reply":"2025-12-27T13:10:01.871317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_embedding = test_image_embedding.drop(columns = 'img_id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:11:08.872704Z","iopub.execute_input":"2025-12-27T13:11:08.873083Z","iopub.status.idle":"2025-12-27T13:11:08.878704Z","shell.execute_reply.started":"2025-12-27T13:11:08.873059Z","shell.execute_reply":"2025-12-27T13:11:08.877780Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_embeddings.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:13:56.627073Z","iopub.execute_input":"2025-12-27T13:13:56.627377Z","iopub.status.idle":"2025-12-27T13:13:56.634129Z","shell.execute_reply.started":"2025-12-27T13:13:56.627357Z","shell.execute_reply":"2025-12-27T13:13:56.633081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_embedding.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:14:32.553251Z","iopub.execute_input":"2025-12-27T13:14:32.553584Z","iopub.status.idle":"2025-12-27T13:14:32.560446Z","shell.execute_reply.started":"2025-12-27T13:14:32.553560Z","shell.execute_reply":"2025-12-27T13:14:32.559126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TARGETS = [\n    \"Dry_Clover_g\",\n    \"Dry_Dead_g\",\n    \"Dry_Green_g\",\n    \"Dry_Total_g\",\n    \"GDM_g\"\n]\n\n\n\npredictions = {}\n\nfor target in TARGETS:\n    print(f\"Training model for {target}\")\n\n    y_train = final_df[target].values   # ✅ target\n\n    model = lgb.LGBMRegressor(\n        n_estimators=300,\n        learning_rate=0.05,\n        num_leaves=31,\n        n_jobs=2\n    )\n\n    model.fit(train_embeddings, y_train)\n\n    X_test = final_test_df.reindex(columns=X_train.columns, fill_value=0)\n\n\n    predictions[target] = model.predict(test_image_embedding)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:14:44.451524Z","iopub.execute_input":"2025-12-27T13:14:44.452073Z","iopub.status.idle":"2025-12-27T13:14:55.692368Z","shell.execute_reply.started":"2025-12-27T13:14:44.452037Z","shell.execute_reply":"2025-12-27T13:14:55.690977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train.shape, X_test.shape)\nprint(list(X_train.columns) == list(X_test.columns))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:15:02.386363Z","iopub.execute_input":"2025-12-27T13:15:02.386705Z","iopub.status.idle":"2025-12-27T13:15:02.391964Z","shell.execute_reply.started":"2025-12-27T13:15:02.386676Z","shell.execute_reply":"2025-12-27T13:15:02.390865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for target in TARGETS:\n    final_test_df[target + \"_pred\"] = predictions[target]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:15:06.169305Z","iopub.execute_input":"2025-12-27T13:15:06.169596Z","iopub.status.idle":"2025-12-27T13:15:06.175786Z","shell.execute_reply.started":"2025-12-27T13:15:06.169577Z","shell.execute_reply":"2025-12-27T13:15:06.174450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# submission = pd.read_csv('/Users/abdul/Desktop/CSIRO - Image2Biomass Prediction/csiro-biomass/sample_submission.csv')\n# submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T17:51:31.896409Z","iopub.execute_input":"2025-12-14T17:51:31.896846Z","iopub.status.idle":"2025-12-14T17:51:31.914707Z","shell.execute_reply.started":"2025-12-14T17:51:31.896815Z","shell.execute_reply":"2025-12-14T17:51:31.913598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:15:12.931395Z","iopub.execute_input":"2025-12-27T13:15:12.931706Z","iopub.status.idle":"2025-12-27T13:15:12.939640Z","shell.execute_reply.started":"2025-12-27T13:15:12.931685Z","shell.execute_reply":"2025-12-27T13:15:12.938499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rows = []\n\nfor i, img_id in enumerate(final_test_df[\"img_id\"]):\n    for target in TARGETS:\n        rows.append({\n            \"sample_id\": f\"{img_id}__{target}\",\n            \"target\": predictions[target][i]\n        })\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:15:21.583056Z","iopub.execute_input":"2025-12-27T13:15:21.583349Z","iopub.status.idle":"2025-12-27T13:15:21.588925Z","shell.execute_reply.started":"2025-12-27T13:15:21.583329Z","shell.execute_reply":"2025-12-27T13:15:21.587724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame(rows)\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:15:28.789196Z","iopub.execute_input":"2025-12-27T13:15:28.789482Z","iopub.status.idle":"2025-12-27T13:15:28.798900Z","shell.execute_reply.started":"2025-12-27T13:15:28.789462Z","shell.execute_reply":"2025-12-27T13:15:28.798102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-27T13:15:50.280895Z","iopub.execute_input":"2025-12-27T13:15:50.281231Z","iopub.status.idle":"2025-12-27T13:15:50.294317Z","shell.execute_reply.started":"2025-12-27T13:15:50.281210Z","shell.execute_reply":"2025-12-27T13:15:50.293134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}