Flood prediction using Feed forward neural network | Python
In this video a detailed coding procedure for prediction of discharge is explained using Python. ** code** import numpy as np import pandas as pd import tensorflow as tf from tensorflow import keras from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_absolute_error, r2_score import matplotlib.pyplot as plt import random # Set random seeds for reproducibility np.random.seed(42) random.seed(42) tf.random.set_seed(42) # import data from excel file_path = "excel file path" # Replace with your actual file path df = pd.read_excel(file_path, engine='openpyxl') # Splitting features and target X = df[['R', 'QT']].values y = df['QT+1'].values.reshape(-1, 1) # Normalize Data feature_scaler = MinMaxScaler() target_scaler = MinMaxScaler() X = feature_scaler.fit_transform(X) y = target_scaler.fit_transform(y) # Train-Test Split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Build Simple Feedforward Neural Network model = keras.Sequential([ keras.layers.Dense(10, activation='relu', input_shape=(2,)), keras.layers.Dense(10, activation='relu'), keras.layers.Dense(1) ]) # Compile Model model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss='mse') # Early Stopping early_stopping = keras.callbacks.EarlyStopping( monitor='val_loss', patience=10, restore_best_weights=True ) # Train Model history = model.fit( X_train, y_train, epochs=100, verbose=1, validation_data=(X_test, y_test), callbacks=[early_stopping] ) # Make Predictions y_pred = model.predict(X_test) y_pred = target_scaler.inverse_transform(y_pred) y_test = target_scaler.inverse_transform(y_test) # Evaluate Model mae = mean_absolute_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print(f"Mean Absolute Error: {mae:.2f}") print(f"R² Score: {r2:.2f}") # Scatter Plot for Actual vs Predicted with Error and R² plt.figure(figsize=(6, 6)) plt.scatter(y_test, y_pred, alpha=0.5, label='Predictions') plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2, label='Ideal Fit') # Add R² and MAE to the plot plt.text( 0.05, 0.9, f'R² = {r2:.2f}\nMAE = {mae:.2f}', transform=plt.gca().transAxes, fontsize=12, bbox=dict(facecolor='white', alpha=0.8)) plt.xlabel('Actual Discharge') plt.ylabel('Predicted Discharge') plt.title('Actual vs Predicted Discharge (Scatter Plot)') plt.legend(loc ='upper right') plt.show() Previous videos: Gradient Descent algorithm in Python https://youtu.be/NAgMHF38v9w Rainfall-Runoff Modelling using ELM| Code https://youtu.be/5UuWHRW-wkg Runoff Forecasting using Narx Neural Network https://youtu.be/5ndlHlEYBj0 Hydrological modelling in Matlab | Part 1 https://youtu.be/b7P176RswnM
Download
0 formatsNo download links available.