Skip to content

About

Classifying the water samples as potable or non-potable using physicochemical properties (pH, hardness, chloramines, sulfate, etc.) from the Water Potability dataset. Apply Random Forest, SVM, and XGBoost. Handled missing values, perform feature engineering, and use SMOTE and Interpreted the results for environmental monitoring applications

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

66 Commits

Folders and files

Repository files navigation

πŸ’§ Water Quality Classification for Safe Drinking Prediction

Python Streamlit Scikit-learn Status

Machine Learning-based Water Potability Prediction System
Predicts whether a water sample is safe to drink using advanced classification models


πŸ”— Live Demo

🌐 Open the Streamlit App β†’

To run locally: streamlit run app.py


πŸ‘₯ Team Members

Name GitHub Responsibilities
Bainty Kaur @BaintyKaur Model Building, Evaluation, Deployment, Feature Engineering
Asna Abbas @asnaabbas763-cyber Data Collection, Preprocessing, EDA, Explainability

πŸ“Œ Problem Statement

Access to safe drinking water is a critical global health issue. Over 2 billion people consume contaminated water worldwide, leading to diseases like cholera, typhoid, and dysentery.

Traditional water quality testing is expensive, slow, and requires laboratory equipment. This project builds a machine learning-based binary classifier that predicts whether a water sample is potable (safe to drink) or non-potable based on physicochemical properties β€” enabling fast, scalable water safety assessment.


πŸ“‚ Dataset

Property Detail
Name Water Potability Dataset
Source Kaggle β€” Aditya Kadiwal
Size 3,276 samples Γ— 10 features
Target Potability β€” 1 (Safe), 0 (Not Safe)
Class Distribution ~61% Non-Potable, ~39% Potable (imbalanced)
Train-Test Split 80% training (with SMOTE applied), 20% test

Features

Feature Description WHO Safe Limit
ph pH of water 6.5 – 8.5
Hardness Calcium/Magnesium content (mg/L) < 300
Solids Total dissolved solids (ppm) < 500
Chloramines Disinfectant concentration (ppm) < 4
Sulfate Sulfate ions (mg/L) < 250
Conductivity Electrical conductivity (ΞΌS/cm) < 400
Organic_carbon Organic matter (ppm) < 2
Trihalomethanes THM disinfection byproducts (ΞΌg/L) < 80
Turbidity Water clarity (NTU) < 5

πŸ”¬ Project Workflow β€” 8 Stages

This project follows a structured 8-stage machine learning pipeline:

Stage 1 β€” Problem Definition & EDA Preparation

  • File: 01_problem_definition.ipynb
  • Defined binary classification problem: potable (safe) vs. non-potable (unsafe)
  • Reviewed water quality standards and health implications
  • Set evaluation metrics: F1-score (primary, due to class imbalance), Accuracy, Precision, Recall, ROC-AUC

Stage 2 β€” Data Collection & Exploration

  • File: 02_data_collection.ipynb
  • Source: Water Potability Dataset from Kaggle
  • Loaded raw data: 3,276 samples Γ— 10 features
  • Identified missing values: ph (491), Sulfate (781), Trihalomethanes (162)
  • Class distribution: ~61% Non-Potable, ~39% Potable (class imbalance detected)

Stage 3 β€” Data Preprocessing & Cleaning

  • File: 03_preprocessing.ipynb
  • Missing values: Class-wise median imputation (preserves class statistics better than global median)
  • Outliers: IQR-based Winsorization (capping extreme values) to preserve data magnitude while reducing influence
  • Duplicates: Verified zero duplicate rows
  • Output: data/processed/water_potability_cleaned.csv

Stage 4 β€” Exploratory Data Analysis

  • File: 04_eda.ipynb
  • Distribution plots with KDE curves (overlaid by Potability class)
  • Correlation heatmap (Pearson correlation, visualized with clustermap)
  • Violin plots showing feature distributions per class
  • Statistical tests: Mann-Whitney U test for all features (identify significant predictors)
  • Finding: Features show significant overlap between classes β†’ non-linear models likely needed

Stage 5 β€” Feature Engineering & Selection

  • File: 05_feature_engineering.ipynb
  • Created 15 new engineered features:
    • WHO safety binary flags (9 features) β€” flag if each parameter meets WHO safe limits
    • total_who_compliant β€” count of WHO-compliant parameters
    • Ratio features: hardness_to_conductivity, chloramines_to_thm, organic_to_turbidity
    • ph_deviation β€” absolute distance from neutral pH 7.0
    • Log transforms: log_solids, log_conductivity
  • Feature selection: Random Forest Gini importance (threshold: 0.01) + Mutual Information ranking
  • Train-Test Split: 80-20 split with stratification applied here to maintain class balance
  • Standardization: StandardScaler applied to numeric features; saved to models/scaler.pkl
  • Output: data/processed/water_potability_engineered.csv, models/selected_features.pkl

Stage 6 β€” Model Building & Hyperparameter Tuning

Model Hyperparameters Tuned
Random Forest n_estimators: [100, 200, 300], max_depth: [None, 10, 20], min_samples_split: [2, 5], max_features: ["sqrt", "log2"]
SVM C: [0.1, 1, 10], kernel: [rbf, linear], gamma: [scale, auto]
XGBoost n_estimators: [100, 200], max_depth: [3, 5, 7], learning_rate: [0.05, 0.1, 0.2], subsample: [0.8, 1.0]
  • Class imbalance handled: SMOTE applied to training data only (prevents data leakage)
  • Models saved: Each trained model pickled to models/random_forest.pkl, models/svm.pkl, models/xgboost.pkl

Stage 7 β€” Model Evaluation & Comparison

  • File: 07_model_evaluation.ipynb
  • Evaluated all 3 models on held-out 20% test set:
    • Classification reports (Precision, Recall, F1-score per class)
    • Confusion matrices
    • ROC-AUC curves with AUC scores
    • Precision-Recall curves
    • Threshold optimization (F1-optimal, cost-optimal, balanced)
  • Best model selection: Automatically selected based on ROC-AUC
  • Output: models/best_model.pkl, models/best_model_name.pkl

Stage 8 β€” Model Interpretation & Explainability

  • File: 08_explainability.ipynb
  • SHAP (SHapley Additive exPlanations) analysis:
    • Global explanation: Summary plots showing feature importance across entire test set
    • Local explanation: Waterfall plots for individual predictions (potable vs. non-potable samples)
    • Dependence plots: Feature interaction insights for top 3 predictive features
    • Actionable insights: Specific environmental monitoring recommendations derived
  • Visualizations saved to data/processed/ for use in Streamlit app

πŸ“Έ Application Screenshots

The Streamlit web app provides an interactive interface with:

  • Real-time Predictions: Input water sample parameters via sliders
  • Multi-model Support: Compare predictions across Random Forest, SVM, and XGBoost
  • WHO Compliance Checker: Visualize which parameters meet WHO safe limits
  • Prediction Confidence: Display probabilities for both potable/non-potable classes
  • SHAP Explainability: Interactive plots explaining individual predictions
  • Evaluation Metrics: Confusion matrices, ROC curves, and performance dashboards

(Screenshots will be added after deployment)


πŸ—‚οΈ Repository Structure

Water-Quality-Classification/
β”œβ”€β”€ 01_problem_definition.ipynb          ← Stage 1: Problem definition
β”œβ”€β”€ 02_data_collection.ipynb             ← Stage 2: Data loading & exploration
β”œβ”€β”€ 03_preprocessing.ipynb               ← Stage 3: Cleaning & preprocessing
β”œβ”€β”€ 04_eda.ipynb                         ← Stage 4: Exploratory data analysis
β”œβ”€β”€ 05_feature_engineering.ipynb         ← Stage 5: Feature creation & selection
β”œβ”€β”€ 06_model_building.ipynb              ← Stage 6: Model training & tuning
β”œβ”€β”€ 07_model_evaluation.ipynb            ← Stage 7: Model evaluation & comparison
β”œβ”€β”€ 08_explainability.ipynb              ← Stage 8: SHAP analysis
β”œβ”€β”€ app.py                               ← Streamlit web application (run from root)
β”œβ”€β”€ requirements.txt                     ← Python dependencies
β”œβ”€β”€ README.md                            ← This file
β”œβ”€β”€ GIT_WORKFLOW.md                      ← Team collaboration guide
β”œβ”€β”€ .gitignore                           ← Git ignore rules
β”‚
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   └── water_potability.csv         ← Original dataset from Kaggle (download manually)
β”‚   └── processed/
β”‚       β”œβ”€β”€ water_potability_cleaned.csv         ← After preprocessing (Stage 3)
β”‚       β”œβ”€β”€ water_potability_engineered.csv      ← After feature engineering (Stage 5)
β”‚       β”œβ”€β”€ X_train.csv, X_train_scaled.csv      ← Training features (scaled)
β”‚       β”œβ”€β”€ X_test.csv, X_test_scaled.csv        ← Test features (scaled)
β”‚       β”œβ”€β”€ y_train.csv, y_test.csv              ← Training/test labels
β”‚       └── water_potability_raw_copy.csv        ← Backup copy
β”‚
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ random_forest.pkl                ← Trained Random Forest model
β”‚   β”œβ”€β”€ svm.pkl                          ← Trained SVM model
β”‚   β”œβ”€β”€ xgboost.pkl                      ← Trained XGBoost model
β”‚   β”œβ”€β”€ best_model.pkl                   ← Best performing model
β”‚   β”œβ”€β”€ best_model_name.pkl              ← Name of best model
β”‚   β”œβ”€β”€ scaler.pkl                       ← StandardScaler (fit on train data)
β”‚   └── selected_features.pkl            ← Selected feature names list
β”‚
β”œβ”€β”€ .venv/                               ← Python virtual environment (gitignored)
└── .git/                                ← Git repository metadata

πŸš€ Quick Start

⚠️ Important: The Streamlit app loads pre-trained models and processed data from disk. You must run notebooks 01–07 in order before launching app.py, otherwise the app will fail to load models and display errors.

Step 0 β€” Download the Dataset

  1. Go to Kaggle β€” Water Potability Dataset
  2. Sign in with a free Kaggle account (required to download)
  3. Click Download and extract the ZIP
  4. Rename the file to water_potability.csv if needed
  5. Place it at: data/raw/water_potability.csv

Option 1: Run the Full Pipeline (Recommended for First-Time Setup)

# 1. Clone the repository
git clone https://github.com/YOUR_ORG/water-quality-project.git
cd water-quality-project

# 2. Create and activate virtual environment
python -m venv venv
source venv/bin/activate              # Windows: venv\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Place the dataset (see Step 0 above)
#    β†’ data/raw/water_potability.csv

# 5. Run notebooks IN ORDER (01 β†’ 07)
jupyter notebook
# Open and run each notebook: 01 β†’ 02 β†’ 03 β†’ 04 β†’ 05 β†’ 06 β†’ 07 β†’ 08

# 6. Launch the Streamlit app
streamlit run app.py

The app will open at http://localhost:8501

Option 2: Run Streamlit App Only (If Models Are Already Trained)

# If models/ and data/processed/ directories are already populated:
streamlit run app.py

Option 3: Deploy to Streamlit Cloud (Production)

  1. Push code to GitHub (ensure models/ and data/processed/ are committed or use Git LFS)
  2. Go to Streamlit Cloud
  3. Click "New app" β†’ Select your repository, branch, and set main file to app.py
  4. Click Deploy β€” your app will be live at: https://your-username-projectname.streamlit.app
  5. Update the Live Demo link at the top of this README with the real URL

πŸ“‹ Prerequisites & Dependencies

  • Python: 3.10 or higher
  • Git: For version control
  • Kaggle Account: To download the dataset (free)

Key Python Packages:

  • pandas, numpy, scipy β€” Data manipulation & statistics
  • scikit-learn, xgboost, lightgbm β€” Machine learning models
  • imbalanced-learn β€” SMOTE for handling class imbalance
  • shap β€” Model explainability
  • matplotlib, seaborn, plotly β€” Data visualization
  • streamlit β€” Web app framework
  • joblib β€” Model serialization

All dependencies listed in requirements.txt


🀝 Git Collaboration & Team Workflow

See GIT_WORKFLOW.md for detailed instructions on:

  • Initial repository setup
  • Creating feature branches
  • Committing with meaningful messages
  • Opening and reviewing Pull Requests
  • Using GitHub Issues for task tracking

Quick Summary of Branch Strategy:

# Each team member creates their own feature branch
git checkout -b feature/stage-N-description

# Examples:
git checkout -b feature/stage-4-eda-analysis
git checkout -b feature/stage-6-model-training

# After finishing, create a PR for review before merging to main

Commit Message Convention:

git commit -m "feat(stage-N): Brief description of changes

- Detailed point 1
- Detailed point 2
- Files saved to: path/to/output"

πŸ“Š Key Results & Findings

After running all stages:

  1. Best Model: Random Forest (selected by ROC-AUC)

    • ROC-AUC, F1-score, Precision-Recall curves in 07_model_evaluation.ipynb
  2. Top Predictive Features (SHAP-verified):

    • Sulfate β€” most critical for potability determination
    • ph_deviation β€” distance from neutral pH (7.0) is a key signal
    • Chloramines β€” disinfection quality strongly influences prediction
  3. Class Imbalance Impact:

    • SMOTE improved recall for minority (potable) class
    • F1-score is a more reliable metric than accuracy for this dataset
  4. WHO Compliance Insights:

    • Engineered WHO-compliance flags are strong predictors
    • total_who_compliant score correlates positively with potability

πŸ” Model Explainability

The project uses SHAP (SHapley Additive exPlanations) to interpret model predictions:

  • Global Explanations: Which features matter most across all predictions
  • Local Explanations: Why a specific water sample was predicted potable/non-potable
  • Dependence Plots: How feature values affect predictions
  • Waterfall Plots: Breakdown of prediction components per sample

All SHAP visualizations are generated in 08_explainability.ipynb and displayed in the Streamlit app.


πŸ› Troubleshooting

Issue: Models not loading in Streamlit app

Solution: Run notebooks 01–07 in order first. All .pkl files must exist in models/ before launching the app. Required files: random_forest.pkl, svm.pkl, xgboost.pkl, best_model.pkl, best_model_name.pkl, scaler.pkl, selected_features.pkl

Issue: FileNotFoundError for test data

Solution: Run notebooks 03–05 to regenerate data/processed/ files. Required: X_test.csv, X_test_scaled.csv, y_test.csv

Issue: Missing water_potability.csv

Solution: Download from Kaggle and place at data/raw/water_potability.csv. A Kaggle account is required to download.

Issue: SMOTE or imbalanced-learn errors

Solution: pip install imbalanced-learn==0.12.3

Issue: Streamlit port already in use

Solution: streamlit run app.py --server.port 8502

Issue: SHAP errors on Stage 8

Solution: pip install shap==0.45.1 β€” ensure version matches requirements.txt


πŸ“š Technologies & Libraries

Category Tools
Languages Python 3.10+
Data Science pandas, numpy, scipy, scikit-learn
ML Models Random Forest, SVM, XGBoost
Imbalanced Data SMOTE (imbalanced-learn)
Explainability SHAP
Visualization matplotlib, seaborn, plotly
Web Framework Streamlit
Version Control Git, GitHub
Notebooks Jupyter

πŸŽ“ Learning Outcomes

By completing this project, you will learn:

βœ… End-to-end machine learning pipeline (data β†’ deployment)
βœ… Handling class imbalance with SMOTE
βœ… Hyperparameter tuning with GridSearchCV
βœ… Model evaluation beyond accuracy (F1, ROC-AUC, Precision-Recall)
βœ… Feature engineering and domain knowledge application (WHO standards)
βœ… Model interpretation with SHAP
βœ… Interactive web app deployment with Streamlit
βœ… Collaborative development with Git & GitHub
βœ… Real-world problem solving: predicting water safety


πŸš€ Future Enhancements

  • Add more classification models (Neural Networks, Gradient Boosting)
  • Implement real-time IoT sensor data integration
  • Add time-series analysis for temporal patterns
  • Incorporate microbiological indicators (bacteria, viruses)
  • Create REST API for model predictions (FastAPI)
  • Add automated retraining pipeline
  • Expand to multi-class classification (Excellent, Good, Fair, Poor water quality)

πŸ“ References & Resources


πŸ“„ License

This project is for educational and research purposes. The dataset is provided under the Open Data Commons Open Database License (ODbL).


Last Updated: May 2026
Project Status: βœ… Active Development

About

Classifying the water samples as potable or non-potable using physicochemical properties (pH, hardness, chloramines, sulfate, etc.) from the Water Potability dataset. Apply Random Forest, SVM, and XGBoost. Handled missing values, perform feature engineering, and use SMOTE and Interpreted the results for environmental monitoring applications

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages