This project trains and evaluates multiple machine learning models for road network flood prediction, generates performance metrics, visualizes model performance with heatmaps, and provides SHAP explanations machine learning model. The code is modularized into separate Python files for training individual models, ensemble models (voting and stacking), plotting heatmaps, and explaining the AdaBoost model with SHAP.
The project consists of six Python files and expects a dataset (flood.csv) as input. The code automatically creates necessary directories for input and output files.
main.py: Orchestrates data loading, model training, evaluation, and visualization. It reads the input dataset, calls training functions, saves results, and generates visualizations.train_individual_models.py: Trains and evaluates individual machine learning models (Random Forest, CatBoost, AdaBoost, SVM, Bagging, Gradient Boosting).train_voting_model.py: Trains and evaluates a VotingClassifier using selected individual models.train_stacking_model.py: Trains and evaluates a StackingClassifier using selected individual models.plot_heatmaps.py: Generates two heatmaps: one for model performance rankings and another for relative improvement compared to Random Forest.explain_adaboost.py: Computes SHAP values for the AdaBoost model and generates a violin summary plot.
Database/: Stores the input dataset (flood.csv). Created automatically if it doesn't exist.Results/Tables/: Stores the output CSV file with model performance metrics (model_perform_new.csv).Results/Figure/: Stores visualization outputs (model_performance_heatmaps.pngandadaboost_shap_summary.png).
Install the required Python libraries using pip:
pip install numpy pandas matplotlib seaborn catboost scikit-learn shap- The project expects a dataset named
flood.csvin theDatabase/folder. - The dataset should have:
- A target column (first column) for flood prediction (binary classification).
- Seven feature columns (columns 1-9) named:
ELEVATION,STREAM,RAINFALL,STORMWATER,SLOPE,ROUGHNESS,CN.
- Ensure the dataset is clean (no missing values) or the code will automatically drop rows with missing values.
-
Clone or Download the Project:
- Save all six Python files (
main.py,train_individual_models.py,train_voting_model.py,train_stacking_model.py,plot_heatmaps.py,explain_adaboost.py) in a single directory.
- Save all six Python files (
-
Place the Dataset:
- Place
flood.csvin theDatabase/folder. If the folder doesn't exist, it will be created when you run the script.
- Place
-
Install Dependencies:
- Run the pip command above to install required libraries.
-
Navigate to the Project Directory:
cd /path/to/project -
Run the Main Script:
python main.py
-
Expected Output:
- Console Output:
- Displays a DataFrame with performance metrics (Accuracy, Kappa score, Precision, Recall, F1-score, ROC AUC) for all models.
- Confirms the SHAP summary plot has been saved.
- Files Generated:
Results/Tables/model_perform_new.csv: CSV file with model performance metrics.Results/Figure/model_performance_heatmaps.png: Heatmaps showing model rankings and relative improvement compared to Random Forest.Results/Figure/adaboost_shap_summary.png: SHAP violin summary plot for the AdaBoost model.
- Console Output:
- Directory Creation: The script automatically creates
Database/,Results/Tables/, andResults/Figure/if they don't exist. - Dataset Requirements: Ensure
flood.csvis in theDatabase/folder and matches the expected format. If the file is missing, aFileNotFoundErrorwill be raised. - SHAP Performance: Computing SHAP values for large datasets can be slow. Consider sampling the dataset (e.g., uncomment
shap.sample(X, 300, random_state=42)inexplain_adaboost.py) for faster computation. - Visualization: Plots are saved as PNG files but not displayed. To display plots, add
plt.show()beforeplt.close()inplot_heatmaps.pyandexplain_adaboost.py.
- Missing Dataset: If
flood.csvis not found, ensure it is placed in theDatabase/folder. - Library Errors: Verify all dependencies are installed correctly.
- Large Datasets: If SHAP computation is slow, reduce the dataset size by sampling in
explain_adaboost.py.
This project is provided as-is for educational purposes. Ensure you have the necessary permissions to use the dataset (flood.csv).
Developed by Yogesh Bhattarai(yogeshbhattarai.sb@gmail.com)