├── synthetic_environmental_data.xlsx ├── file └── README.md /synthetic_environmental_data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nelvinebi/AI-Powered-Early-Warning-System-for-Environmental-Hazards/HEAD/synthetic_environmental_data.xlsx -------------------------------------------------------------------------------- /file: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | import matplotlib.pyplot as plt 4 | from sklearn.ensemble import RandomForestClassifier 5 | from sklearn.metrics import classification_report, confusion_matrix 6 | from sklearn.model_selection import train_test_split 7 | from sklearn.preprocessing import StandardScaler 8 | import seaborn as sns 9 | 10 | # Step 1: Synthetic Data Generation 11 | np.random.seed(42) 12 | n_samples = 5000 13 | 14 | data = { 15 | 'temperature': np.random.normal(loc=30, scale=5, size=n_samples), # °C 16 | 'humidity': np.random.uniform(30, 100, n_samples), # % 17 | 'co2_level': np.random.normal(loc=400, scale=100, size=n_samples), # ppm 18 | 'pm25': np.random.exponential(scale=25, size=n_samples), # μg/m³ 19 | 'rainfall': np.random.exponential(scale=5, size=n_samples), # mm/hr 20 | 'seismic_activity': np.random.uniform(0, 5, n_samples), # Richter scale 21 | } 22 | 23 | df = pd.DataFrame(data) 24 | 25 | # Step 2: Generate Labels (0: No Hazard, 1: Hazard) 26 | def label_hazard(row): 27 | if ( 28 | row['temperature'] > 40 or 29 | row['humidity'] < 35 or 30 | row['co2_level'] > 600 or 31 | row['pm25'] > 100 or 32 | row['rainfall'] > 50 or 33 | row['seismic_activity'] > 3.5 34 | ): 35 | return 1 # Hazard 36 | return 0 # No Hazard 37 | 38 | df['hazard'] = df.apply(label_hazard, axis=1) 39 | 40 | # Step 3: Feature Engineering 41 | X = df.drop('hazard', axis=1) 42 | y = df['hazard'] 43 | 44 | scaler = StandardScaler() 45 | X_scaled = scaler.fit_transform(X) 46 | 47 | # Step 4: Train/Test Split 48 | X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42) 49 | 50 | # Step 5: Model Training 51 | model = RandomForestClassifier(n_estimators=100, random_state=42) 52 | model.fit(X_train, y_train) 53 | 54 | # Step 6: Prediction and Evaluation 55 | y_pred = model.predict(X_test) 56 | 57 | print("Classification Report:") 58 | print(classification_report(y_test, y_pred)) 59 | 60 | print("Confusion Matrix:") 61 | sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues') 62 | plt.xlabel("Predicted") 63 | plt.ylabel("Actual") 64 | plt.title("Confusion Matrix for Environmental Hazard Detection") 65 | plt.show() 66 | 67 | # Feature Importance Plot 68 | feature_importances = pd.Series(model.feature_importances_, index=X.columns) 69 | feature_importances.nlargest(6).plot(kind='barh') 70 | plt.title("Feature Importance") 71 | plt.xlabel("Importance Score") 72 | plt.show() 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🌍 AI-Powered Early Warning System for Environmental Hazards 2 | 3 | This project is a demonstration of how artificial intelligence can be used to detect and predict environmental hazards using synthetic sensor data. The system uses a machine learning model (Random Forest) to classify environmental conditions as either hazardous or safe based on simulated readings from environmental sensors. 4 | 5 | ## 📦 Features 6 | 7 | - **Synthetic data generation**: Simulates real-world environmental data including temperature, humidity, CO₂, PM2.5, rainfall, and seismic activity. 8 | - **Hazard classification model**: Uses Random Forest classifier for hazard prediction. 9 | - **Streamlit dashboard**: Interactive interface to visualize data, train the model, and make real-time hazard predictions. 10 | - **Visual insights**: Confusion matrix and feature importance chart. 11 | - **Live prediction**: Users can input sensor values to get immediate hazard detection feedback. 12 | 13 | --- 14 | 15 | ## 📁 Project Structure 16 | 17 | ├── app.py # Streamlit dashboard application 18 | ├── synthetic_environmental_data.xlsx # Generated sample dataset 19 | ├── README.md # Project documentation 20 | 21 | yaml 22 | Copy 23 | Edit 24 | 25 | --- 26 | 27 | ## 🚀 How to Run the Project 28 | 29 | ### 1. Clone the Repository 30 | 31 | ```bash 32 | git clone https://github.com/your-username/environmental-hazard-warning-system.git 33 | cd environmental-hazard-warning-system 34 | 2. Install Dependencies 35 | Make sure you have Python 3.7+ installed, then run: 36 | 37 | bash 38 | Copy 39 | Edit 40 | pip install -r requirements.txt 41 | Or manually install: 42 | 43 | bash 44 | Copy 45 | Edit 46 | pip install streamlit pandas numpy scikit-learn matplotlib seaborn 47 | 3. Run the Streamlit App 48 | bash 49 | Copy 50 | Edit 51 | streamlit run app.py 52 | This will open an interactive dashboard in your web browser. 53 | 54 | 📊 Dataset Description 55 | The dataset is synthetically generated to simulate environmental sensor readings. It includes: 56 | 57 | temperature (°C) 58 | 59 | humidity (%) 60 | 61 | co2_level (ppm) 62 | 63 | pm25 (µg/m³) 64 | 65 | rainfall (mm/hr) 66 | 67 | seismic_activity (Richter scale) 68 | 69 | hazard (0: No Hazard, 1: Hazard) 70 | 71 | 🧠 Model Logic 72 | A hazard is flagged based on the following thresholds: 73 | 74 | temperature > 40°C 75 | 76 | humidity < 35% 77 | 78 | co2_level > 600 ppm 79 | 80 | pm25 > 100 µg/m³ 81 | 82 | rainfall > 50 mm/hr 83 | 84 | seismic_activity > 3.5 85 | 86 | The model is trained using a RandomForestClassifier from scikit-learn. 87 | 88 | 📌 Future Improvements 89 | Integrate real-time sensor APIs (weather, air quality, seismic). 90 | 91 | Deploy as a web app with alert systems (email/SMS). 92 | 93 | Expand classification into multi-class hazard types. 94 | 95 | Use deep learning models for more complex patterns. 96 | 97 | 📜 License 98 | This project is open-source and available under the MIT License. 99 | 100 | 👤 Author 101 | Agbozu Ebingiye Nelvin 102 | Email: nelvinebingiye@gmail.com 103 | --------------------------------------------------------------------------------