In continuation with ETo Prediction Blog ..........
Prompt:
Can you give me code for the above latest ML algorithms suitable for the above prediction ETo
Here is Python code for Extreme Learning Machine (ELM), a recent and efficient ML algorithm, applied to the prediction of reference evapotranspiration (ETo) based on input parameters.
Extreme Learning Machine (ELM) Code for ETo Prediction
Steps:
- Install required libraries.
- Load and preprocess the dataset.
- Split the dataset into training and testing sets.
- Build the ELM model using a simple feedforward neural network.
- Train and test the model for ETo prediction.
import numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import mean_squared_error, r2_scorefrom sklearn.preprocessing import StandardScalerimport tensorflow as tf
# Step 1: Load the datasetfile_path = "Sample.xlsx" # Replace with your file pathdata = pd.read_excel(file_path)
print(data)
# Step 2: Data Preprocessing# Assuming 'ETo' is the target columnX = data.drop("ET0", axis=1) # Featuresy = data["ET0"] # Target
# Standardize the featuresscaler = StandardScaler()X_scaled = scaler.fit_transform(X)
# Split data into train and test setsX_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.25, random_state=42)
# Step 3: Define the Extreme Learning Machine (ELM) modelclass ELM: def __init__(self, input_size, hidden_size, output_size): # Initialize random weights and biases self.input_size = input_size self.hidden_size = hidden_size self.output_size = output_size self.input_weights = np.random.uniform(-1, 1, (self.hidden_size, self.input_size)) self.bias = np.random.uniform(-1, 1, (self.hidden_size, 1)) self.output_weights = None
def sigmoid(self, x): return 1 / (1 + np.exp(-x))
def fit(self, X, y): # Compute hidden layer output H = self.sigmoid(np.dot(self.input_weights, X.T) + self.bias) H = H.T
# Compute output weights using Moore-Penrose pseudoinverse self.output_weights = np.dot(np.linalg.pinv(H), y)
def predict(self, X): # Compute hidden layer output H = self.sigmoid(np.dot(self.input_weights, X.T) + self.bias) H = H.T # Compute predictions return np.dot(H, self.output_weights)
# Step 4: Train the ELM modelinput_size = X_train.shape[1]hidden_size = 50 # Number of hidden neurons (adjustable)output_size = 1
elm = ELM(input_size, hidden_size, output_size)elm.fit(X_train, y_train)
# Step 5: Predict and evaluatey_pred_train = elm.predict(X_train)y_pred_test = elm.predict(X_test)
# Metricstrain_mse = mean_squared_error(y_train, y_pred_train)test_mse = mean_squared_error(y_test, y_pred_test)test_r2 = r2_score(y_test, y_pred_test)
print("Training MSE:", train_mse)print("Testing MSE:", test_mse)print("Testing R2 Score:", test_r2)
# Predict for a new samplesample = np.array([[25, 50, 3, 20, 10, 5]]) # Replace with new input valuessample_scaled = scaler.transform(sample)eto_prediction = elm.predict(sample_scaled)print("Predicted ETo for the sample:", eto_prediction[0])
Results
Explanation:
-
Model Architecture:
- Input Layer: Takes the input features (temperature, humidity, solar radiation, etc.).
- Hidden Layer: Randomly initialized weights and biases, with a non-trainable hidden layer using the sigmoid activation function.
- Output Layer: Weights are calculated using the Moore-Penrose pseudoinverse of the hidden layer outputs.
-
Advantages of ELM:
- Very fast training due to single-layer optimization.
- Efficient for large datasets.
-
Metrics Evaluated:
- Mean Squared Error (MSE): Measures the prediction error.
- R² Score: Measures how well the predictions fit the data.
-
Sample Input: Replace with your data's actual features and test its predictions.
Additional Algorithms for Comparison
If you're interested in trying Gradient Boosting Machines (GBMs) let me know, and I can provide additional implementations. Would you like me to extend the solution?
Prompt:
yes extend code so that i compare the performance of the above algorithm for the prediction of ET0
Here is an extended version of the code to compare the performance of Extreme Learning Machine (ELM) with Gradient Boosting Machine (GBM), XGBoost, and Random Forest for predicting reference evapotranspiration (ETo).
Full Comparison Code
Explanation:
-
Models Included:
- ELM: Efficient neural network-based approach.
- Gradient Boosting Machine (GBM): Ensemble method focused on reducing errors iteratively.
- Random Forest (RF): Ensemble of decision trees for robust predictions.
- XGBoost: Optimized gradient boosting algorithm known for high performance.
-
Steps:
- Models are trained and evaluated on the same train-test split for a fair comparison.
- Performance metrics include Mean Squared Error (MSE) and R² Score.
- Predicted ETo values for a new sample are displayed for each model.
-
Results Comparison:
- Look for the model with the lowest MSE and highest R² for optimal predictions.
Next Steps:
- If you want detailed visualizations (e.g., bar charts for comparison), we can use
matplotlib
orseaborn
. - Let me know if you want assistance with hyperparameter tuning or extending to other algorithms like CatBoost or LightGBM!
Below is the extended code for including hyperparameter tuning for all models using their respective built-in tuners.
Extended Code for Additional Models and Hyperparameter Tuning
# For Visualization of the above two models
Results
Explanation:
-
Hyperparameter Tuning:
GridSearchCV
is used to find the optimal hyperparameters for each model.- Hyperparameter grids are defined for Gradient Boosting (GBM), Random Forest (RF),
-
Comparison Metrics:
- Models are evaluated using Mean Squared Error (MSE) and R² Score on the test set.
-
Prediction for a New Sample:
- Predicts ETo using the best model for a hypothetical new input.(left as Student Exercise)
No comments:
Post a Comment