├── code ├── README.md └── Water analysis results.csv /code: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from sklearn.model_selection import train_test_split 4 | from sklearn.ensemble import RandomForestRegressor 5 | from sklearn.metrics import mean_absolute_error, r2_score 6 | 7 | # Load data 8 | data = pd.DataFrame({ 9 | "Borehole": ["BH1", "BH2", "BH3", "BH4", "BH5", "BH6", "BH7", "BH8", "BH9", "BH10"], # Add all boreholes 10 | "Lat": [5.036889, 5.01975, 5.016722, 5.002366, 4.957417, 4.94325, 4.908472, 4.929167, 4.917722, 4.91175], 11 | "Long": [6.405972, 6.398167, 6.396528, 6.387691, 6.35375, 6.324806, 6.337083, 6.300806, 6.317583, 6.305972], 12 | "pH": [6.12, 6.3, 6.38, 6.1, 5.99, 5.93, 5.6, 6.69, 6.14, 6.74], 13 | "EC": [406, 715, 857, 782, 164, 175, 763, 1156, 269, 1652], 14 | "TDS": [203, 356, 430, 391, 82, 84, 383, 578, 135, 826], 15 | "NO3": [0.36, 0.165, 0.335, 0.175, 0.165, 0.094, 0.085, 0.096, 0.348, 0.42], 16 | "Cl": [39, 15, 21, 14, 14, 16, 14, 22, 34, 47], 17 | "SO4": [1.4, 0.8, 1.67, 0.86, 0.82, 0.48, 0.45, 0.5, 1.75, 2.1], 18 | "TA": [26, 15, 17, 19, 3, 5, 16, 15, 16, 15], 19 | "TH": [25, 45, 18, 37, 35, 32, 30, 46, 101, 45], 20 | "Ca": [22.4, 8.5, 13.7, 8.85, 8.6, 9, 7.4, 12.48, 20, 27.86], 21 | "Mg": [6.35, 2.48, 3, 2.5, 2.76, 2.85, 2.38, 3.62, 5.65, 7.5], 22 | "Na": [10.86, 4, 6.5, 4.85, 4.54, 5.2, 4.74, 5.8, 9.95, 13.58], 23 | "K": [4.2, 1.46, 1.55, 0.76, 1.2, 1.4, 1.2, 1.8, 4, 4.65], 24 | "Fe": [0.16, 0.14, 0.24, 0.12, 0.08, 0.13, 0.18, 0.17, 0.2, 0.13], 25 | "WQI": [42.12, 53.98, 62.89, 55.40, 22.86, 24.76, 55.18, 78.65, 42.97, 108.14] # Water Quality Index 26 | }) 27 | 28 | # Define features and target 29 | X = data.drop(columns=["Borehole", "WQI"]) 30 | y = data["WQI"] 31 | 32 | # Split dataset 33 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 34 | 35 | # Train a model 36 | model = RandomForestRegressor(n_estimators=100, random_state=42) 37 | model.fit(X_train, y_train) 38 | 39 | # Make predictions 40 | y_pred = model.predict(X_test) 41 | 42 | # Evaluate model 43 | mae = mean_absolute_error(y_test, y_pred) 44 | r2 = r2_score(y_test, y_pred) 45 | print(f"Mean Absolute Error: {mae}") 46 | print(f"R² Score: {r2}") 47 | 48 | # Predict water quality for a new sample 49 | new_sample = np.array([[5.5, 400, 200, 0.2, 20, 1.5, 10, 30, 15, 5, 8, 2, 6, 1.5, 0.2]]) 50 | predicted_wqi = model.predict(new_sample) 51 | print(f"Predicted Water Quality Index: {predicted_wqi[0]}") 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Water Quality Prediction 2 | 3 | ## Overview 4 | This project is focused on predicting water quality parameters based on various physicochemical characteristics. The goal is to develop a machine learning model capable of accurately predicting water quality indicators such as pH, electrical conductivity (EC), total dissolved solids (TDS), and other chemical constituents. 5 | 6 | ## Features 7 | The dataset includes the following water quality parameters: 8 | - **pH** – Acidity/alkalinity of water 9 | - **EC (Electrical Conductivity)** – Measures the ability of water to conduct electricity, indicating dissolved salts 10 | - **TDS (Total Dissolved Solids)** – The total amount of dissolved substances in water 11 | - **NO₃ (Nitrate)** – Presence of nitrates, an indicator of pollution from fertilizers and waste 12 | - **Cl (Chloride)** – A measure of chloride ion concentration 13 | - **SO₄ (Sulfate)** – Presence of sulfate ions in water 14 | - **TA (Total Alkalinity)** – The water's ability to neutralize acids 15 | - **TH (Total Hardness)** – Total concentration of calcium and magnesium ions 16 | - **Ca (Calcium)** – Calcium ion concentration 17 | - **Mg (Magnesium)** – Magnesium ion concentration 18 | - **Na (Sodium)** – Sodium ion concentration 19 | - **K (Potassium)** – Potassium ion concentration 20 | - **Fe (Iron)** – Presence of iron in water 21 | 22 | ## Project Objectives 23 | - Collect and preprocess water quality data 24 | - Develop a predictive model using machine learning techniques 25 | - Evaluate the model’s performance on unseen data 26 | - Provide insights into the factors influencing water quality 27 | 28 | ## Technology Stack 29 | - **Programming Language:** Python 30 | - **Libraries Used:** Pandas, NumPy, Scikit-learn, Matplotlib, Seaborn 31 | - **Machine Learning Models:** Regression, Decision Trees, Random Forest, Neural Networks (depending on data performance) 32 | 33 | ## Data Preprocessing 34 | - Handling missing values 35 | - Feature engineering and selection 36 | - Data normalization and scaling 37 | - Splitting data into training and testing sets 38 | 39 | ## Model Training & Evaluation 40 | - Training multiple models and selecting the best-performing one 41 | - Hyperparameter tuning to optimize performance 42 | - Evaluation metrics: RMSE, R² Score, MAE 43 | 44 | ## How to Use 45 | 1. Clone the repository: 46 | ```sh 47 | git clone https://github.com/your-username/water-quality-prediction.git 48 | ``` 49 | 2. Install dependencies: 50 | ```sh 51 | pip install -r requirements.txt 52 | ``` 53 | 3. Run the data preprocessing script: 54 | ```sh 55 | python preprocess.py 56 | ``` 57 | 4. Train the model: 58 | ```sh 59 | python train.py 60 | ``` 61 | 5. Make predictions: 62 | ```sh 63 | python predict.py --input sample_data.csv 64 | ``` 65 | 66 | ## Contribution 67 | Feel free to contribute by submitting pull requests. Please follow the coding standards and provide relevant documentation. 68 | 69 | ## License 70 | This project is licensed under the MIT License. 71 | 72 | ## Contact 73 | For any inquiries, contact **desmondeteh@gmail.com**. 74 | 75 | -------------------------------------------------------------------------------- /Water analysis results.csv: -------------------------------------------------------------------------------- 1 | Borehole,Lat,long,Town,pH,EC,TDS,NO3,Cl,SO4,TA,TH,Ca,Mg,Na,K,Fe,WQI,Iron 2 | BH1,5.036889,6.405972,Igbogene 1,6.12,406,203,0.36,39,1.4,26,25,22.4,6.35,10.86,4.2,0.16,42.1216181,0.6 3 | BH2,5.01975,6.398167,Yenagwe1,6.3,715,356,0.165,15,0.8,15,45,8.5,2.48,4,1.46,0.14,53.98172031,0.14 4 | BH3,5.016722,6.396528,Yenagwe 2,6.38,857,430,0.335,21,1.67,17,18,13.7,3,6.5,1.55,0.24,62.89770334,0.4 5 | BH4,5.002366,6.387691,Akenfa 1,6.1,782,391,0.175,14,0.86,19,37,8.85,2.5,4.85,0.76,0.12,55.40595753,0.37 6 | BH5,4.957417,6.35375,Etegwe 1,5.99,164,82,0.165,14,0.82,3,35,8.6,2.76,4.54,1.2,0.08,22.86374269,0.38 7 | BH6,4.94325,6.324806,Biogbolo 1,5.93,175,84,0.094,16,0.48,5,32,9,2.85,5.2,1.4,0.13,24.76845452,0.3 8 | BH7,4.908472,6.337083,Kpansia 1,5.6,763,383,0.085,14,0.45,16,30,7.4,2.38,4.74,1.2,0.18,55.18412497,0.15 9 | BH8,4.929167,6.300806,Ekeki 1,6.69,1156,578,0.096,22,0.5,15,46,12.48,3.62,5.8,1.8,0.17,78.65419883,0.35 10 | BH9,4.917722,6.317583,Kpansia 1,6.14,269,135,0.348,34,1.75,16,101,20,5.65,9.95,4,0.2,42.97825035,0.14 11 | BH10,4.91175,6.305972,Yenizue Epie 1,6.74,1652,826,0.42,47,2.1,15,45,27.86,7.5,13.58,4.65,0.13,108.1452058,0.36 12 | BH11,4.925861,6.275583,Amarata 1,6.05,422,211,0.204,37,0.96,18,91,21.48,6.2,9.84,2.62,0.18,48.18565142,0.16 13 | BH12,4.916,6.2755,Swail 1,6.87,722,361,0.49,23,2.45,9,33,16.74,4.4,7.6,2.5,0.28,60.54938805,0.36 14 | BH13,4.917028,6.251222,Ogbogoro 1,6.43,928,464,0.078,16,0.39,18,27,9.2,2.58,5.4,1.8,0.06,61.17732854,0.26 15 | BH14,4.903722,6.251222,Ogu 1,6.2,160,80,0.162,24,0.8,12,56,14.56,3.8,6,2.26,0.12,28.05512813,0.12 16 | BH15,4.91125,6.255611,Akaba 1,6.91,530,265,0.17,8,0.86,3,15,6.75,1.76,3.85,0.6,0.18,42.33777969,0.18 17 | BH16,5.026869,6.398981,Igbogene 2,6.33,496,248,0.137,13,1.28,8,65,8.16,2.42,5.82,1.76,0.32,50.33214192,0.39 18 | BH17,5.002678,6.379307,Akenfa 2,6.13,164,82,0.341,55,5.6,9,93,33.97,8.7,15.9,5.38,0.4,46.74196216,0.4 19 | BH18,4.992793,6.375336,Agudama 1,5.88,334,167,0.23,58,5.5,16,200,34.5,8.84,17.4,5.4,0.7,74.21779429,0.7 20 | BH19,4.98176,6.37166,Agudama 2,6.01,173,87,0.22,46,4.38,17,128,27.6,7.45,13.54,5.1,0.68,57.29759797,0.68 21 | BH20,4.953314,6.355015,Etegwe 2,5.99,164,82,0.165,14,0.82,3,35,8.6,2.76,4.54,1.2,0.08,22.84728269,0.8 22 | BH21,4.952838,6.34541,Okutukutu 1,5.85,91,46,0.132,14,1.42,14,56,8.78,1.96,4.62,0.78,0.32,27.92237124,0.32 23 | BH22,4.94409,6.331098,Opolo 1,5.93,84,42,0.374,43,3.4,22,90,26.74,6.4,12.43,2.7,0.65,46.20785065,0.65 24 | BH23,4.940728,6.326492,Opolo 2,6.38,94,48,0.41,65,5.6,24,115,35.6,7.64,14.9,5.4,0.4,46.07438836,0.4 25 | BH24,4.933825,6.307698,Kpansia 2,5.86,348,174,0.127,14,1.38,12,26,9.5,2.64,4.86,1.36,0.3,38.0913137,0.11 26 | BH25,4.916093,6.301615,Yenizue Epie 2,6.4,422,211,0.318,90,10.8,22,148,56.88,12.76,28.64,7.34,0.44,72.58534912,0.44 27 | BH26,4.935199,6.285502,Amarata 2,6.74,194,97,0.187,22,0.28,10,47,12.69,4.2,6.38,2.42,0.4,37.8099315,0.112 28 | BH27,4.923142,6.272686,Swail 2,6.46,486,243,0.172,19,1.64,17,116,11.28,3.54,5.38,1.34,0.32,55.19895768,0.35 29 | BH28,4.905837,6.258554,Akaba 2,5.99,77,38,0.213,40,4,14,111,23.86,5.72,12.58,2.55,0.4,39.54486379,0.4 30 | BH29,4.918221,6.25624,Ogbogoro 2,6.2,160,80,0.162,24,0.8,12,56,14.56,3.8,6,2.26,0.12,28.05512813,0.12 31 | BH30,4.899849,6.269169,Ogu 2,6.28,172,86,0.348,52,5.25,4,41,29.78,6.88,16.7,4.4,0.43,41.78656051,0.43 32 | BH31,4.983667,6.276111,Akaibiri 1,6.14,285,142,0.218,14,2.48,17,17,10.35,2.87,5.48,1.72,0.31,35.27294402,0.31 33 | BH32,4.987861,6.275722,Akaibiri 1,6.59,355,178,0.231,20,3.5,18,34,14.36,3.54,7.6,1.3,0.364,42.95478895,0.364 34 | BH33,5.000389,6.279556,Gbarantoru 1,6.01,420,210,0.31,20,4,20,52,13.3,4.2,6.5,2.6,0.136,41.60380917,0.136 35 | BH34,4.999861,6.280667,Gbarantoru 2,5.97,583,292,0.318,34,4.8,18,48,22.18,5.68,9.45,2.8,0.142,51.37814257,0.32 36 | BH35,4.999656,6.279361,Gbarantoru 3,5.96,363,182,0.22,20,3.85,12,36,14.7,2.53,6.84,1.76,0.36,42.88598307,0.36 37 | BH36,4.999222,6.2785,Gbarantoru 4,5.92,364,182,0.23,30,3.64,17,30,13.82,4.86,8.35,2.18,0.132,36.67706526,0.132 38 | BH37,5.004056,6.294028,Gbarantoru 5,6.15,310,155,0.197,12,3,18,26,17.48,2.25,5.42,3.2,0.38,41.02340524,0.38 39 | BH38,5.032306,6.312556,Ogbuna 1,6.49,379,189,0.271,13,4.3,17,43,9.47,2.84,5.46,1.85,0.348,44.0304606,0.348 40 | BH39,5.033528,6.311917,Ogbuna 2,6.35,304,152,0.176,14,2.34,18,27,10.2,3,4.96,1.41,0.186,33.37600427,0.186 41 | BH40,5.034,6.311778,Ogbuna 3,6.52,279,140,0.185,11,2.97,23,30,9.78,2.56,3.75,1.92,0.36,38.02697592,0.36 42 | BH41,5.033361,6.311056,Ogbuna 4,6.08,285,143,0.121,12,2.58,15,21,8.5,2.58,4.34,6.98,0.272,38.08975501,0.372 43 | BH42,5.038194,6.323444,Okolobiri 1,6.15,382,191,0.278,62,4.84,17,43,32.76,10.72,18.68,2.5,0.188,45.20214153,0.388 44 | BH43,5.038,6.319889,Okolobiri 2,5.99,457,274,0.328,16,4.75,17,44,13.6,3.52,7.48,1.2,0.174,43.40610743,0.374 45 | BH44,5.035417,6.321361,Okolobiri 3,6.6,348,174,0.281,12,3.84,26,41,9.55,2.84,4.72,1.48,0.328,41.57678878,0.328 46 | BH45,5.034306,6.318833,Okolobiri 4,6.83,298,199,0.217,12,3.76,23,35,9.28,1.78,5.46,1.24,0.146,33.71068039,0.146 47 | BH46,5.03425,6.31789,Okolobiri 5,6.62,306,153,0.227,13,4,28,35,10.32,2.1,4.8,1.2,0.346,39.19937644,0.346 48 | BH47,4.996806,6.262944,Tombia 1,6.24,436,218,0.29,14,3.46,24,45,9.88,3,5.75,2.25,0.33,46.80970726,0.33 49 | BH48,5.001417,6.263,Tombia 2,6.08,307,154,0.214,21,3.2,21,22,13.25,4.34,6.58,3.74,0.39,41.65627,0.39 50 | BH49,5.000861,6.265528,Tombia 3,6.1,376,188,0.245,32,4,22,19,18.72,5.63,9.36,3.96,0.136,38.68340624,0.136 51 | BH50,5.000639,6.266833,Tombia 4,5.67,357,178,0.235,33,3.85,18,10,19.3,5.82,9.65,1.55,0.382,42.03195999,0.382 52 | --------------------------------------------------------------------------------