Skip to content

muhammad-rateeb/Global_air_quality_analysis_in_Python

Repository files navigation

🌍 Global Air Quality Dataset Analysis

Python Pandas Scikit-learn License

CSC380 - Introduction to Data Science | Semester Project
University of Engineering and Technology, Lahore | Fall 2025

A comprehensive data science project analyzing global air quality measurements from major cities worldwide, featuring exploratory data analysis, machine learning model building, and actionable environmental recommendations.


πŸ“‹ Table of Contents


🎯 Overview

Air pollution is a global concern affecting millions of people worldwide. This project analyzes the Global Air Quality Dataset containing 10,000 records of air quality measurements from various prominent cities worldwide. The analysis includes:

  • Data Preprocessing & Cleaning - Handling missing values, outliers, and feature engineering
  • Exploratory Data Analysis (EDA) - Comprehensive univariate, bivariate, and correlation analysis
  • Machine Learning - Building and comparing classification models for AQI prediction
  • Model Evaluation - Performance comparison with accuracy, precision, recall, and F1-score
  • Recommendations - Strategies for environmental improvement

🎯 Objectives

  1. Analyze air quality trends and patterns across different cities and countries
  2. Build predictive models for AQI (Air Quality Index) categories
  3. Understand the impact of pollution on health
  4. Suggest strategies for environmental improvement

πŸ“Š Dataset Description

Feature Description Data Type
City Name of the city Object
Country Country where city is located Object
Date Date of measurement Object
PM2.5 Fine particulate matter (Β΅g/mΒ³) Float64
PM10 Coarse particulate matter (Β΅g/mΒ³) Float64
NO2 Nitrogen dioxide (Β΅g/mΒ³) Float64
SO2 Sulfur dioxide (Β΅g/mΒ³) Float64
CO Carbon monoxide (mg/mΒ³) Float64
O3 Ozone (Β΅g/mΒ³) Float64
Temperature Temperature at measurement time (Β°C) Float64
Humidity Humidity level (%) Float64
Wind Speed Wind speed (m/s) Float64

Dataset Statistics

Feature Mean Std Dev Min Max
PM2.5 77.45 41.93 5.02 149.98
PM10 104.44 55.06 10.00 200.00
NO2 52.20 27.32 5.01 100.00
SO2 25.34 14.09 1.00 49.99
CO 5.05 2.85 0.10 10.00
O3 106.03 55.08 10.04 200.00
Temperature 14.90 14.44 -10.00 40.00
Humidity 55.08 25.98 10.01 99.99
Wind Speed 10.23 5.63 0.50 20.00
  • Total Records: 10,000
  • Cities Covered: 20
  • Countries Covered: 19
  • Date Range: January 1, 2023 to December 28, 2023

πŸ“ Project Structure

ids/
β”œβ”€β”€ air_quality_analysis.py      # Main analysis script (971 lines)
β”œβ”€β”€ global_air_quality_data_10000.csv  # Raw dataset
β”œβ”€β”€ cleaned_air_quality_data.csv # Preprocessed dataset
β”œβ”€β”€ model_results.csv            # ML model performance results
β”œβ”€β”€ REPORT_CONTENT.txt           # Detailed project report
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ requirements.txt             # Python dependencies
└── figures/                     # Generated visualizations
    β”œβ”€β”€ 01_univariate_pollutants.png
    β”œβ”€β”€ 02_univariate_meteorological.png
    β”œβ”€β”€ 03_aqi_distribution.png
    β”œβ”€β”€ 04_city_country_distribution.png
    β”œβ”€β”€ 05_bivariate_pollutants_aqi.png
    β”œβ”€β”€ 06_bivariate_meteo_aqi.png
    β”œβ”€β”€ 07_boxplot_by_category.png
    β”œβ”€β”€ 08_correlation_matrix.png
    β”œβ”€β”€ 09_aqi_by_city.png
    β”œβ”€β”€ 10_aqi_by_country.png
    β”œβ”€β”€ 11_temporal_analysis.png
    β”œβ”€β”€ 12_monthly_pollutant_trends.png
    β”œβ”€β”€ 13_seasonal_city_comparison.png
    β”œβ”€β”€ 14_pairplot.png
    β”œβ”€β”€ 15_confusion_matrices.png
    β”œβ”€β”€ 16_model_comparison.png
    β”œβ”€β”€ 17_feature_importance.png
    └── 18_cv_scores.png

✨ Features

Data Preprocessing

  • βœ… Missing value detection and handling (interpolation & mode imputation)
  • βœ… Outlier detection using IQR method
  • βœ… Outlier handling using Winsorization
  • βœ… Date parsing and temporal feature extraction (Year, Month, Day, Season, Quarter)
  • βœ… AQI calculation based on US EPA standards
  • βœ… Feature scaling (StandardScaler & MinMaxScaler)
  • βœ… Label encoding for categorical target variable

Exploratory Data Analysis

  • πŸ“Š Univariate analysis of pollutants and meteorological variables
  • πŸ“ˆ Bivariate analysis with scatter plots and trend lines
  • πŸ”₯ Correlation matrix heatmap
  • πŸ™οΈ Comparative analysis across cities and countries
  • πŸ“… Temporal/Seasonal trend analysis

Machine Learning

  • 🌲 Random Forest Classifier
  • πŸš€ Gradient Boosting Classifier
  • πŸ“ˆ Logistic Regression
  • 🎯 K-Nearest Neighbors (KNN)
  • ⚑ Support Vector Machine (SVM)

πŸ”§ Installation

Prerequisites

  • Python 3.8 or higher
  • pip (Python package installer)

Setup

  1. Clone the repository
git clone https://github.com/muhammad-rateeb/global-air-quality-analysis.git
cd global-air-quality-analysis
  1. Create a virtual environment (recommended)
python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux/Mac
source .venv/bin/activate
  1. Install dependencies
pip install -r requirements.txt

πŸš€ Usage

Run the complete analysis

python air_quality_analysis.py

This will:

  1. Load and preprocess the dataset
  2. Perform comprehensive EDA
  3. Generate all visualizations in the figures/ folder
  4. Train and evaluate 5 ML models
  5. Save results to model_results.csv
  6. Save cleaned data to cleaned_air_quality_data.csv

πŸ”„ Analysis Pipeline

graph TD
    A[Raw Data] --> B[Data Loading]
    B --> C[Missing Value Analysis]
    C --> D[Outlier Detection & Handling]
    D --> E[Feature Engineering]
    E --> F[AQI Calculation]
    F --> G[Feature Scaling]
    G --> H[Train-Test Split 80:20]
    H --> I[Model Training]
    I --> J[Model Evaluation]
    J --> K[Results & Recommendations]
Loading

Preprocessing Steps

Step Method Description
Missing Values Interpolation + Mode Linear interpolation for numerical, mode for categorical
Outliers IQR Method Detection using Q1-1.5Γ—IQR to Q3+1.5Γ—IQR
Outlier Handling Winsorization Capping values at IQR bounds
Feature Scaling StandardScaler Z-score normalization
Train-Test Split Stratified 80% training, 20% testing

AQI Categories

AQI Range Category Health Implication
0 - 50 Good Air quality is satisfactory
51 - 100 Moderate Acceptable; moderate concern for some
101 - 200 Unhealthy Everyone may experience health effects
201+ Hazardous Health alert; everyone affected

πŸ€– Machine Learning Models

Models Implemented

  1. Random Forest Classifier - Ensemble of decision trees with bagging
  2. Gradient Boosting Classifier - Sequential ensemble with boosting
  3. Logistic Regression - Linear classification with regularization
  4. K-Nearest Neighbors - Instance-based learning
  5. Support Vector Machine - Kernel-based classification

Hyperparameters

Model Key Parameters
Random Forest n_estimators=100, max_depth=None, random_state=42
Gradient Boosting n_estimators=100, learning_rate=0.1
Logistic Regression max_iter=1000, multi_class='multinomial'
KNN n_neighbors=5, metric='minkowski'
SVM kernel='rbf', C=1.0

πŸ“ˆ Results

Model Performance Comparison

Model Train Accuracy Test Accuracy Precision Recall F1-Score CV Mean CV Std
Random Forest 100% 99.9% 99.85% 99.9% 99.87% 99.89% 0.07%
Gradient Boosting 100% 99.9% 99.85% 99.9% 99.87% 99.93% 0.05%
Logistic Regression 95.61% 96.15% 95.84% 96.15% 95.91% 95.43% 0.41%
KNN 94.0% 89.1% 88.87% 89.1% 88.93% 89.44% 0.49%
SVM 97.51% 97.15% 97.06% 97.15% 97.09% 96.11% 0.39%

πŸ† Best Models

  • Random Forest and Gradient Boosting achieved the highest accuracy (99.9%)
  • Both models showed excellent generalization with minimal overfitting
  • Cross-validation confirmed consistent performance

πŸ“Š Visualizations

The project generates 18 comprehensive visualizations:

Sample Visualizations

πŸ“ˆ Click to see visualization descriptions
Figure Description
01_univariate_pollutants.png Distribution histograms of all 6 pollutants
02_univariate_meteorological.png Distribution of temperature, humidity, wind speed
03_aqi_distribution.png AQI histogram and category bar chart
04_city_country_distribution.png Record counts by city and country
05_bivariate_pollutants_aqi.png Scatter plots: pollutants vs AQI with trend lines
06_bivariate_meteo_aqi.png Scatter plots: meteorological variables vs AQI
07_boxplot_by_category.png Box plots of pollutants by AQI category
08_correlation_matrix.png Heatmap of feature correlations
09_aqi_by_city.png Average AQI comparison across cities
10_aqi_by_country.png Average AQI comparison across countries
11_temporal_analysis.png Monthly AQI trends over time
12_monthly_pollutant_trends.png Monthly pollutant concentration patterns
13_seasonal_city_comparison.png Seasonal AQI variations by city
14_pairplot.png Pairwise relationships between features
15_confusion_matrices.png Confusion matrices for all 5 models
16_model_comparison.png Performance metrics comparison chart
17_feature_importance.png Random Forest feature importance
18_cv_scores.png Cross-validation score comparison

πŸ” Key Findings

Pollutant Analysis

  • PM2.5 shows the strongest correlation with AQI
  • Ozone (O3) levels vary significantly with temperature
  • CO concentrations are relatively stable across regions

Geographic Insights

  • Major industrial cities show consistently higher AQI values
  • Coastal cities benefit from better air dispersion
  • Seasonal patterns differ between hemispheres

Model Insights

  • Ensemble methods (Random Forest, Gradient Boosting) significantly outperform other models
  • Feature importance reveals PM2.5, PM10, and O3 as top predictors
  • All models show good generalization with stratified sampling

Health Recommendations

  1. πŸš— Reduce vehicle emissions in urban areas
  2. 🏭 Implement stricter industrial emission controls
  3. 🌳 Increase green spaces and urban forests
  4. πŸ“Š Establish real-time air quality monitoring systems
  5. πŸŽ“ Public awareness campaigns on air quality impacts

πŸ› οΈ Technologies Used

Category Technologies
Language Python 3.x
Data Processing Pandas, NumPy
Visualization Matplotlib, Seaborn
Machine Learning Scikit-learn
IDE VS Code / Jupyter Notebook

Python Libraries

pandas          # Data manipulation and analysis
numpy           # Numerical computing
matplotlib      # Static visualizations
seaborn         # Statistical visualizations
scikit-learn    # Machine learning algorithms

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Muhammad Rateeb

  • GitHub: @muhammad-rateeb
  • University: University of Engineering and Technology, Lahore
  • Course: CSC380 - Introduction to Data Science

πŸ™ Acknowledgments


Made with ❀️ for Data Science

About

CSC384 - Introduction to Data Science Semester Project: Global Air Quality Dataset Analysis with ML Models

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages