1

I have yield data and multiple vegetation indices values in a tabulated form for multiple years. I applied machine learning regressors using the .csv file and tested the performance. The random forest regressor performed the best. Now I want to use this trained model to predict the yield for a separate year. But this time, I want to give raster images of the vegetation indices in .tif format as the explanatory variables, and want to generate the response variable, i.e., the yield in raster (.tif) format. How do I do that? I am providing the code below, which I have used to train the model, and test the model performance.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from xgboost import XGBRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error

# Load the dataset

df = pd.read_csv('J:/Pratibha/VI_Yield.csv')
print(df.head())

# Separate the target and the explanatory variables

X = df.iloc[:, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
y = df.iloc[:, 11]
print(X)

# Compute the mean of the target variable (baseline prediction)
baseline_prediction = np.mean(y)

# Create an array of baseline predictions with the same length as y_test
baseline_predictions = np.full_like(y, baseline_prediction)

# Evaluate the baseline model using common regression metrics
mse_baseline = mean_squared_error(y, baseline_predictions)
rmse_baseline = np.sqrt(mse_baseline)
mae_baseline = mean_absolute_error(y, baseline_predictions)
r2_baseline = r2_score(y, baseline_predictions)

# Display the baseline model's performance metrics
print("Baseline Model Performance:")
print(f"Baseline Prediction: {baseline_prediction:.2f}")
print(f"Mean Squared Error (MSE): {mse_baseline:.2f}")
print(f"Root Mean Squared Error (RMSE): {rmse_baseline:.2f}")
print(f"Mean Absolute Error (MAE): {mae_baseline:.2f}")
print(f"R-squared (R^2): {r2_baseline:.2f}")

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Fitting Random Forest Regression to the dataset

from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators = 75, random_state = 0)
rf.fit(X, y) 
rf.score(X_train, y_train)
rft_predictions = rf.predict(X_train)
rf_predictions = rf.predict(X_test)

# Support Vector Regression (SVR)
svr = SVR(kernel='linear', C=1.0)
svr.fit(X_train, y_train)
svrt_predictions = svr.predict(X_train)
svr_predictions = svr.predict(X_test)

# K-Nearest Neighbors (KNN) Regression
knn = KNeighborsRegressor(n_neighbors=5)
knn.fit(X_train, y_train)
knnt_predictions = knn.predict(X_train)
knn_predictions = knn.predict(X_test)

# Decision Tree Regression
tree_regressor = DecisionTreeRegressor(random_state=12, max_depth=15, min_samples_split = 2)
tree_regressor.fit(X_train, y_train)
treet_predictions = tree_regressor.predict(X_train)
tree_predictions = tree_regressor.predict(X_test)

# XGBoost Regression
xgb_regressor = XGBRegressor(n_estimators=25, learning_rate=0.1, max_depth=5, random_state=5)
xgb_regressor.fit(X_train, y_train)
xgbt_predictions = xgb_regressor.predict(X_train)
xgb_predictions = xgb_regressor.predict(X_test)

# Evaluate the models (you can use different evaluation metrics)
rft_rmse = np.sqrt(mean_squared_error(y_train, rft_predictions))
rft_r2 = r2_score(y_train, rft_predictions)
rft_mae = mean_absolute_error(y_train, rft_predictions)

rf_rmse = np.sqrt(mean_squared_error(y_test, rf_predictions))
rf_r2 = r2_score(y_test, rf_predictions)
rf_mae = mean_absolute_error(y_test, rf_predictions)

svrt_rmse = np.sqrt(mean_squared_error(y_train, svrt_predictions))
svrt_r2 = r2_score(y_train, svrt_predictions)
svrt_mae = mean_absolute_error(y_train, svrt_predictions)

svr_rmse = np.sqrt(mean_squared_error(y_test, svr_predictions))
svr_r2 = r2_score(y_test, svr_predictions)
svr_mae = mean_absolute_error(y_test, svr_predictions)

knnt_rmse = np.sqrt(mean_squared_error(y_train, knnt_predictions))
knnt_r2 = r2_score(y_train, knnt_predictions)
knnt_mae = mean_absolute_error(y_train, knnt_predictions)

knn_rmse = np.sqrt(mean_squared_error(y_test, knn_predictions))
knn_r2 = r2_score(y_test, knn_predictions)
knn_mae = mean_absolute_error(y_test, knn_predictions)

treet_rmse = np.sqrt(mean_squared_error(y_train, treet_predictions))
treet_r2 = r2_score(y_train, treet_predictions)
treet_mae = mean_absolute_error(y_train, treet_predictions)

tree_rmse = np.sqrt(mean_squared_error(y_test, tree_predictions))
tree_r2 = r2_score(y_test, tree_predictions)
tree_mae = mean_absolute_error(y_test, tree_predictions)

xgbt_rmse = np.sqrt(mean_squared_error(y_train, xgbt_predictions))
xgbt_r2 = r2_score(y_train, xgbt_predictions)
xgbt_mae = mean_absolute_error(y_train, xgbt_predictions)

xgb_rmse = np.sqrt(mean_squared_error(y_test, xgb_predictions))
xgb_r2 = r2_score(y_test, xgb_predictions)
xgb_mae = mean_absolute_error(y_test, xgb_predictions)

# Print the evaluation results
print("Random Forest Regression:")
print(f"RMSET: {rft_rmse:.2f}")
print(f"R-squaredT: {rft_r2:.2f}")
print(f"MAET: {rft_mae:.2f}")
print(f"RMSE: {rf_rmse:.2f}")
print(f"R-squared: {rf_r2:.2f}")
print(f"MAE: {rf_mae:.2f}")

print("\nSupport Vector Regression:")
print(f"RMSET: {svrt_rmse:.2f}")
print(f"R-squaredT: {svrt_r2:.2f}")
print(f"MAET: {svrt_mae:.2f}")
print(f"RMSE: {svr_rmse:.2f}")
print(f"R-squared: {svr_r2:.2f}")
print(f"MAE: {svr_mae:.2f}")

print("\nK-Nearest Neighbors Regression:")
print(f"RMSET: {knnt_rmse:.2f}")
print(f"R-squaredT: {knnt_r2:.2f}")
print(f"MAET: {knnt_mae:.2f}")
print(f"RMSE: {knn_rmse:.2f}")
print(f"R-squared: {knn_r2:.2f}")
print(f"MAE: {knn_mae:.2f}")

print("\nDecision Tree Regression:")
print(f"RMSET: {treet_rmse:.2f}")
print(f"R-squaredT: {treet_r2:.2f}")
print(f"MAET: {treet_mae:.2f}")
print(f"RMSE: {tree_rmse:.2f}")
print(f"R-squared: {tree_r2:.2f}")
print(f"MAE: {tree_mae:.2f}")

print("\nXGBoost Regression:")
print(f"RMSET: {xgbt_rmse:.2f}")
print(f"R-squaredT: {xgbt_r2:.2f}")
print(f"MAET: {xgbt_mae:.2f}")
print(f"RMSE: {xgb_rmse:.2f}")
print(f"R-squared: {xgb_r2:.2f}")
print(f"MAE: {xgb_mae:.2f}")

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.