{"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":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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,"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# use pasture images to predict biomass components' values (target):\n\n# (also target_name:)\n# Dry green vegetation (excluding clover) [Dry_Green_g]\n# Dry dead material [Dry_Dead_g] \n# Dry clover biomass [Dry_Clover_g]\n# Green dry matter (GDM) [GDM_g]\n# Total dry biomass [Dry_Total_g]\n\n# Pre_GSHH_NDVI — Normalized Difference Vegetation Index (GreenSeeker) reading.\n# Height_Ave_cm — Average pasture height measured by falling plate (cm).\n# target - Ground-truth biomass value (grams) corresponding to target_name\n\n# model should learn the relation between an (image,target_value) and target, pre_gshh_ndvi and height_ave","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Start Here**","metadata":{}},{"cell_type":"code","source":"import pandas\nimport numpy\nimport matplotlib.pyplot as plt\nimport seaborn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T12:43:57.025936Z","iopub.execute_input":"2025-11-14T12:43:57.026747Z","iopub.status.idle":"2025-11-14T12:43:58.322572Z","shell.execute_reply.started":"2025-11-14T12:43:57.026719Z","shell.execute_reply":"2025-11-14T12:43:58.321688Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pandas.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T12:44:01.472477Z","iopub.execute_input":"2025-11-14T12:44:01.473436Z","iopub.status.idle":"2025-11-14T12:44:01.499113Z","shell.execute_reply.started":"2025-11-14T12:44:01.473402Z","shell.execute_reply":"2025-11-14T12:44:01.498406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T06:30:14.203267Z","iopub.execute_input":"2025-11-10T06:30:14.203520Z","iopub.status.idle":"2025-11-10T06:30:14.214031Z","shell.execute_reply.started":"2025-11-10T06:30:14.203502Z","shell.execute_reply":"2025-11-10T06:30:14.213225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"1785/5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T12:59:56.688823Z","iopub.execute_input":"2025-11-14T12:59:56.689203Z","iopub.status.idle":"2025-11-14T12:59:56.695298Z","shell.execute_reply.started":"2025-11-14T12:59:56.689174Z","shell.execute_reply":"2025-11-14T12:59:56.694516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.dropna(inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T06:29:23.355753Z","iopub.execute_input":"2025-11-10T06:29:23.356385Z","iopub.status.idle":"2025-11-10T06:29:23.361832Z","shell.execute_reply.started":"2025-11-10T06:29:23.356360Z","shell.execute_reply":"2025-11-10T06:29:23.360625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.drop_duplicates(inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T06:30:10.416066Z","iopub.execute_input":"2025-11-10T06:30:10.416700Z","iopub.status.idle":"2025-11-10T06:30:10.423931Z","shell.execute_reply.started":"2025-11-10T06:30:10.416673Z","shell.execute_reply":"2025-11-10T06:30:10.423188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['image_path'][1]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# pip install opencv-python","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\n# Load the image\nimage = cv2.imread((\"/kaggle/input/csiro-biomass/\" + data['image_path'][1]))\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n# Display the image using matplotlib\nplt.imshow(image, origin='lower')\nplt.title(\"Image\")\nplt.show()\n# # Wait for a key press (0 means wait indefinitely)\n# cv2.waitKey(0)\n\n# # Destroy all OpenCV windows\n# cv2.destroyAllWindows()\n\n# image = open(\", \"rb\")\n# print(image.read())\n# image.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T12:44:37.434161Z","iopub.execute_input":"2025-11-14T12:44:37.434497Z","iopub.status.idle":"2025-11-14T12:44:37.938124Z","shell.execute_reply.started":"2025-11-14T12:44:37.434471Z","shell.execute_reply":"2025-11-14T12:44:37.937022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#grayscale image\ngray_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n\n# Displaying the results\nfig, axs = plt.subplots(1, 2, figsize=(10, 10))\naxs[0].imshow(image, origin='lower')\naxs[0].set_title('Original Image')\naxs[1].imshow(gray_img, cmap='gray', origin='lower')\naxs[1].set_title('Grayscale Image')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:13:52.292344Z","iopub.execute_input":"2025-11-14T13:13:52.292736Z","iopub.status.idle":"2025-11-14T13:13:52.966151Z","shell.execute_reply.started":"2025-11-14T13:13:52.292711Z","shell.execute_reply":"2025-11-14T13:13:52.964928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#plot a histogram for the grayscale image\nplt.hist(gray_img.ravel(), 256, [0, 256])\n# seaborn.kdeplot(gray_img.ravel())\nplt.xlabel(\"pixel intensity\")\nplt.ylabel(\"pixel count\")\nplt.title(\"Distribution of intensities in the grayscale image\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:28:05.056226Z","iopub.execute_input":"2025-11-14T13:28:05.056598Z","iopub.status.idle":"2025-11-14T13:28:13.879924Z","shell.execute_reply.started":"2025-11-14T13:28:05.056551Z","shell.execute_reply":"2025-11-14T13:28:13.878954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#using cv2.calcHist\nhist = cv2.calcHist([gray_img], [0], None, [256], [0, 256]) #much faster than plt.hist() and seaborn,histplot()\nplt.plot(hist, color='gray')\nplt.xlim([0, 256])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:24:50.871838Z","iopub.execute_input":"2025-11-14T13:24:50.872664Z","iopub.status.idle":"2025-11-14T13:24:51.059393Z","shell.execute_reply.started":"2025-11-14T13:24:50.872630Z","shell.execute_reply":"2025-11-14T13:24:51.058477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split the image into its color channels\nb, g, r = cv2.split(image)\n\n# Display each color channel as a separate image\nfig, ax = plt.subplots(1, 3, figsize=(15, 8))\nax[0].imshow(b, cmap='gray')\nax[0].set_title('Blue Channel')\nax[1].imshow(g, cmap='gray')\nax[1].set_title('Green Channel')\nax[2].imshow(r, cmap='gray')\nax[2].set_title('Red Channel')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:30:35.535048Z","iopub.execute_input":"2025-11-14T13:30:35.535375Z","iopub.status.idle":"2025-11-14T13:30:36.627439Z","shell.execute_reply.started":"2025-11-14T13:30:35.535350Z","shell.execute_reply":"2025-11-14T13:30:36.626402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# b.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:34:33.381404Z","iopub.execute_input":"2025-11-14T13:34:33.381740Z","iopub.status.idle":"2025-11-14T13:34:33.387400Z","shell.execute_reply.started":"2025-11-14T13:34:33.381717Z","shell.execute_reply":"2025-11-14T13:34:33.386060Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\"\"\" Inaccurate method \"\"\"\n#plot a histogram for the each color channel\n# seaborn.kdeplot(b.ravel(), color='blue')\n# seaborn.kdeplot(g.ravel(), color='green')\n# seaborn.kdeplot(r.ravel(), color='red')\n# plt.xlabel(\"pixel intensity\")\n# plt.ylabel(\"pixel count\")\n# plt.title(\"Distribution of intensities in the grayscale image\")\n# plt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#using cv2.calcHist\ncolor = ('r', 'g', 'b')\nfor i, col in enumerate(color):\n    hist = cv2.calcHist([image], [i], None, [256], [0, 256])\n    plt.plot(hist, color=col)\n    plt.xlim([0, 256])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:26:09.786857Z","iopub.execute_input":"2025-11-14T13:26:09.787854Z","iopub.status.idle":"2025-11-14T13:26:09.951278Z","shell.execute_reply.started":"2025-11-14T13:26:09.787822Z","shell.execute_reply":"2025-11-14T13:26:09.950162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# merge the color channels back into an image\nmerged_img = cv2.merge([r, g, b])\nplt.imshow(cv2.cvtColor(merged_img,cv2.COLOR_BGR2RGB))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T12:55:00.489962Z","iopub.execute_input":"2025-11-14T12:55:00.490313Z","iopub.status.idle":"2025-11-14T12:55:01.055127Z","shell.execute_reply.started":"2025-11-14T12:55:00.490291Z","shell.execute_reply":"2025-11-14T12:55:01.053795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:07:18.824325Z","iopub.execute_input":"2025-11-14T13:07:18.824759Z","iopub.status.idle":"2025-11-14T13:07:18.831342Z","shell.execute_reply.started":"2025-11-14T13:07:18.824732Z","shell.execute_reply":"2025-11-14T13:07:18.830239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# data.iloc[1]\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T06:26:10.624976Z","iopub.execute_input":"2025-11-10T06:26:10.625489Z","iopub.status.idle":"2025-11-10T06:26:10.649438Z","shell.execute_reply.started":"2025-11-10T06:26:10.625455Z","shell.execute_reply":"2025-11-10T06:26:10.648732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(data['image_path'].unique())\n#for each image there are 5 entries, one for each tyoe of target_name","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-14T13:00:41.369995Z","iopub.execute_input":"2025-11-14T13:00:41.370329Z","iopub.status.idle":"2025-11-14T13:00:41.376926Z","shell.execute_reply.started":"2025-11-14T13:00:41.370305Z","shell.execute_reply":"2025-11-14T13:00:41.376049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# data.corr()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\ndata['Height_Ave_cm'].plot()\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['Species'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T06:31:08.621007Z","iopub.execute_input":"2025-11-10T06:31:08.621288Z","iopub.status.idle":"2025-11-10T06:31:08.627275Z","shell.execute_reply.started":"2025-11-10T06:31:08.621268Z","shell.execute_reply":"2025-11-10T06:31:08.626527Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for s in data['Species'].unique():\n    print(data[data['Species']==s].shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T06:36:52.911365Z","iopub.execute_input":"2025-11-10T06:36:52.912196Z","iopub.status.idle":"2025-11-10T06:36:52.926853Z","shell.execute_reply.started":"2025-11-10T06:36:52.912165Z","shell.execute_reply":"2025-11-10T06:36:52.925917Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#'Ryegrass_Clover' = 490 rows\n# Lucerne = 110 rows\n# SubcloverDalkeith = 15 rows\n# Ryegrass = 310 rows\n# Phalaris_Clover = 210\n# 'SubcloverLosa = 25\n# 'Clover' = 205\n#'Fescue_CrumbWeed' = 50\n# 'Phalaris_Ryegrass_Clover' = 40\n# 'Phalaris' = 40\n# 'WhiteClover' = 50\n# 'Fescue' = 140\n# 'Phalaris_BarleyGrass_SilverGrass_SpearGrass_Clover_Capeweed' = 55\n# 'Phalaris_Clover_Ryegrass_Barleygrass_Bromegrass' = 35\n# 'Mixed' = 10","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ryegrass_clover = data[(data['Species']=='Ryegrass_Clover')]\nryegrass_clover","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T12:24:40.807764Z","iopub.execute_input":"2025-11-12T12:24:40.808619Z","iopub.status.idle":"2025-11-12T12:24:40.823636Z","shell.execute_reply.started":"2025-11-12T12:24:40.808590Z","shell.execute_reply":"2025-11-12T12:24:40.822825Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ryegrass_clover.groupby('State').count()\n# len(ryegrass_clover['image_path'].unique()) #98 different images used for ryegrass_clover\n# plt.figure(figsize=(20, 20))\n# seaborn.barplot(x=ryegrass_clover['Sampling_Date'], y=ryegrass_clover.count())\n# plt.show()\n# ryegrass_clover.groupby('Sampling_Date').count()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T12:49:38.281718Z","iopub.execute_input":"2025-11-12T12:49:38.282066Z","iopub.status.idle":"2025-11-12T12:49:38.296950Z","shell.execute_reply.started":"2025-11-12T12:49:38.282043Z","shell.execute_reply":"2025-11-12T12:49:38.295967Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#encoding target_name\nfrom sklearn.preprocessing import OneHotEncoder\n\nencoder = OneHotEncoder(handle_unknown='ignore', sparse_output=False)\ndata_cat = data.loc[:, ['State', 'target_name']]\nencoded_cat = pandas.DataFrame(encoder.fit_transform(data_cat))\nencoded_cat.columns = ['state_NSW', 'state_Tas', 'state_Vic', 'state_WA', 'Dry_Clover_g', 'Dry_Dead_g', 'Dry_Green_g', 'Dry_Total_g', 'GDM_g']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T13:21:08.150598Z","iopub.execute_input":"2025-11-12T13:21:08.151449Z","iopub.status.idle":"2025-11-12T13:21:08.163076Z","shell.execute_reply.started":"2025-11-12T13:21:08.151418Z","shell.execute_reply":"2025-11-12T13:21:08.161888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoded_cat.iloc[10:20]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T13:21:48.735619Z","iopub.execute_input":"2025-11-12T13:21:48.735936Z","iopub.status.idle":"2025-11-12T13:21:48.753144Z","shell.execute_reply.started":"2025-11-12T13:21:48.735891Z","shell.execute_reply":"2025-11-12T13:21:48.752138Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.iloc[10:20]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T13:21:33.357608Z","iopub.execute_input":"2025-11-12T13:21:33.357962Z","iopub.status.idle":"2025-11-12T13:21:33.372580Z","shell.execute_reply.started":"2025-11-12T13:21:33.357936Z","shell.execute_reply":"2025-11-12T13:21:33.371783Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['State'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T12:26:45.779120Z","iopub.execute_input":"2025-11-12T12:26:45.780139Z","iopub.status.idle":"2025-11-12T12:26:45.786607Z","shell.execute_reply.started":"2025-11-12T12:26:45.780106Z","shell.execute_reply":"2025-11-12T12:26:45.785734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (20, 20))\nseaborn.lineplot(x = data['Species'], y = data.index)\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[['State','Height_Ave_cm']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T13:24:58.516620Z","iopub.execute_input":"2025-11-12T13:24:58.517008Z","iopub.status.idle":"2025-11-12T13:24:58.529079Z","shell.execute_reply.started":"2025-11-12T13:24:58.516982Z","shell.execute_reply":"2025-11-12T13:24:58.528175Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = data[['image_path', 'Pre_GSHH_NDVI', 'Height_Ave_cm']].join(encoded_cat[['Dry_Clover_g', 'Dry_Dead_g', 'Dry_Green_g', 'Dry_Total_g', 'GDM_g']])\ny = data['target']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T13:26:11.536444Z","iopub.execute_input":"2025-11-12T13:26:11.537548Z","iopub.status.idle":"2025-11-12T13:26:11.546765Z","shell.execute_reply.started":"2025-11-12T13:26:11.537517Z","shell.execute_reply":"2025-11-12T13:26:11.545972Z"}},"outputs":[],"execution_count":null}]}