HANDBOOK_GUIDE2026-06-29
Rising Waters: Machine Learning Approach to Flood Prediction System ML Guide
By Rayan Syed
45 min read
Part 6: Production Script - Model Comparison & Training (train.py)
We now transition from interactive notebook exploration to writing a production-grade Python script named train.py. While Jupyter is excellent for visual exploration (EDA) and prototype cleaning, standard Python scripts are preferred in production environments to run training pipelines from end to end and save the resulting model files.
6.1 Understanding Machine Learning Models & Algorithms
In this script, we train and compare three distinct classification models to find the one that balances accuracy with minority class detection:
- Logistic Regression: A linear model that estimates the probability of a binary event. It is fast, simple, and serves as our baseline.
- Random Forest Classifier: An ensemble model that builds multiple decision trees and averages their outputs. It is highly robust, handles non-linear relationships, and reduces variance.
- XGBoost Classifier: An advanced gradient boosting model that trains trees sequentially, where each new tree focuses on correcting the errors made by the previous ones. It is highly performant and often yields the highest accuracy.
Model Comparison Training Script (train.py)
Save this complete script as train.py in your project root folder:
import os
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, classification_report
import joblib
def run_training_pipeline():
print("Step 1: Loading weather dataset...")
df = pd.read_csv('data/flood_prediction.csv')
print("Step 2: Resolving missing values...")
# Fill any empty cells with median values
for col in df.columns:
if df[col].isnull().sum() > 0:
df[col] = df[col].fillna(df[col].median())
# Split features and target
X = df.drop(columns=['flood'])
y = df['flood']
# Stratified split to keep label proportions identical
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
print("Step 3: Applying Standard Scaling to features...")
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
print("\nStep 4: Training and comparing classification models...")
models = {
"Logistic Regression": LogisticRegression(max_iter=1000, class_weight='balanced'),
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42, class_weight='balanced'),
"XGBoost": XGBClassifier(use_label_encoder=False, eval_metric='logloss')
}
best_acc = 0.0
best_model = None
best_model_name = ""
for name, clf in models.items():
# Fit classifier
clf.fit(X_train_scaled, y_train)
y_pred = clf.predict(X_test_scaled)
acc = accuracy_score(y_test, y_pred)
print(f"-> {name} Test Accuracy: {acc * 100:.2f}%")
if acc > best_acc:
best_acc = acc
best_model = clf
best_model_name = name
print(f"\nWinner Selected: {best_model_name} with {best_acc * 100:.2f}% accuracy.")
# Print metrics report for the winner
winner_preds = best_model.predict(X_test_scaled)
print("\nWinner Performance Report:")
print(classification_report(y_test, winner_preds))
print("Step 5: Serializing and saving best model & scaler...")
os.makedirs('models', exist_ok=True)
joblib.dump(best_model, 'models/flood_model.joblib')
joblib.dump(scaler, 'models/scaler.joblib')
print("✓ Output model: models/flood_model.joblib")
print("✓ Output scaler: models/scaler.joblib")
if __name__ == '__main__':
run_training_pipeline()
6.2 Executing the Training Script
- Open your project terminal and run:
python train.py - Expected Output: The terminal will print out step-by-step progress, output accuracy percentages for each model, print the final classification metrics report, and confirm the serialization of the winning model.