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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from sklearn.cluster import KMeans
|
| 8 |
+
from sklearn.model_selection import train_test_split
|
| 9 |
+
|
| 10 |
+
data = pd.read_csv("waterQuality1.csv")
|
| 11 |
+
|
| 12 |
+
data.replace('#NUM!', np.nan, inplace=True)
|
| 13 |
+
data = data.apply(pd.to_numeric, errors='coerce')
|
| 14 |
+
data.fillna(data.mean(), inplace=True)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
X = data.drop('is_safe', axis=1)
|
| 18 |
+
y_safe = data['is_safe']
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
scaler = StandardScaler()
|
| 22 |
+
X_scaled = scaler.fit_transform(X)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
inertia = []
|
| 26 |
+
K_range = range(1, 10)
|
| 27 |
+
|
| 28 |
+
for k in K_range:
|
| 29 |
+
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
|
| 30 |
+
kmeans.fit(X_scaled)
|
| 31 |
+
inertia.append(kmeans.inertia_)
|
| 32 |
+
|
| 33 |
+
plt.figure()
|
| 34 |
+
plt.plot(K_range, inertia, marker='o')
|
| 35 |
+
plt.xlabel("Number of Clusters (k)")
|
| 36 |
+
plt.ylabel("Inertia")
|
| 37 |
+
plt.title("Elbow Method")
|
| 38 |
+
plt.show()
|
| 39 |
+
|
| 40 |
+
optimal_k = 4
|
| 41 |
+
|
| 42 |
+
kmeans = KMeans(n_clusters=optimal_k, random_state=42, n_init=10)
|
| 43 |
+
clusters = kmeans.fit_predict(X_scaled)
|
| 44 |
+
|
| 45 |
+
data['cluster'] = clusters
|
| 46 |
+
|
| 47 |
+
data['anomaly'] = data['is_safe'].apply(lambda x: 0 if x == 1 else 1)
|
| 48 |
+
y_anomaly = data['anomaly']
|
| 49 |
+
y_final = np.column_stack((y_anomaly, y_safe))
|
| 50 |
+
|
| 51 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 52 |
+
X, y_final, test_size=0.2, random_state=42
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
cluster_summary = data.groupby('cluster').mean()
|
| 57 |
+
|
| 58 |
+
print("\nCluster Summary:")
|
| 59 |
+
print(cluster_summary[['is_safe', 'anomaly']])
|
| 60 |
+
|
| 61 |
+
ranked_clusters = cluster_summary['is_safe'].sort_values().index.tolist()
|
| 62 |
+
labels = [
|
| 63 |
+
"Heavily contaminated group",
|
| 64 |
+
"Contaminated group",
|
| 65 |
+
"Borderline group",
|
| 66 |
+
"Clean water group"
|
| 67 |
+
]
|
| 68 |
+
cluster_label_map = dict(zip(ranked_clusters, labels))
|
| 69 |
+
|
| 70 |
+
# Apply labels
|
| 71 |
+
data['cluster_label'] = data['cluster'].map(cluster_label_map)
|
| 72 |
+
|
| 73 |
+
print("\nCluster Counts (Numeric + Label):")
|
| 74 |
+
print(data.groupby(['cluster', 'cluster_label']).size())
|
| 75 |
+
|
| 76 |
+
print("\nFirst 10 rows:")
|
| 77 |
+
print(data[['cluster', 'cluster_label']].head(10))
|
| 78 |
+
|
| 79 |
+
"""### Preparing Data for ANN
|
| 80 |
+
|
| 81 |
+
Now, I'll combine the scaled features with the cluster assignments. The K-Means clustering has already been applied, and `clusters` array contains the cluster IDs for each data point. I'll add this as a new feature to the scaled dataset `X_scaled`.
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
X_scaled_df = pd.DataFrame(X_scaled, columns=X.columns)
|
| 85 |
+
X_scaled_df['cluster'] = clusters
|
| 86 |
+
|
| 87 |
+
# Convert y_safe to integer type for classification
|
| 88 |
+
y_safe_int = y_safe.astype(int)
|
| 89 |
+
|
| 90 |
+
# Split the data into training and testing sets for the ANN
|
| 91 |
+
X_train_ann, X_test_ann, y_train_ann, y_test_ann = train_test_split(
|
| 92 |
+
X_scaled_df, y_safe_int, test_size=0.2, random_state=42
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
print("Shape of X_train_ann:", X_train_ann.shape)
|
| 96 |
+
print("Shape of X_test_ann:", X_test_ann.shape)
|
| 97 |
+
print("Shape of y_train_ann:", y_train_ann.shape)
|
| 98 |
+
print("Shape of y_test_ann:", y_test_ann.shape)
|
| 99 |
+
|
| 100 |
+
"""### Artificial Neural Network (ANN) Implementation
|
| 101 |
+
|
| 102 |
+
I will now implement a simple Multi-layer Perceptron (MLP) Classifier from `sklearn` to act as the ANN. This model will take the combined features (original scaled features + cluster ID) and predict whether the water is safe (`is_safe`).
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
from sklearn.neural_network import MLPClassifier
|
| 106 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 107 |
+
|
| 108 |
+
# Initialize the MLPClassifier (ANN)
|
| 109 |
+
mlp = MLPClassifier(
|
| 110 |
+
hidden_layer_sizes=(100, 50), # Two hidden layers with 100 and 50 neurons respectively
|
| 111 |
+
max_iter=500, # Maximum number of iterations for the solver to converge
|
| 112 |
+
activation='relu', # Rectified Linear Unit activation function
|
| 113 |
+
solver='adam', # Adam optimizer
|
| 114 |
+
random_state=42, # For reproducibility
|
| 115 |
+
verbose=True # To see the training progress
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Train the ANN
|
| 119 |
+
mlp.fit(X_train_ann, y_train_ann)
|
| 120 |
+
|
| 121 |
+
# Make predictions on the test set
|
| 122 |
+
y_pred_ann = mlp.predict(X_test_ann)
|
| 123 |
+
|
| 124 |
+
# Evaluate the ANN
|
| 125 |
+
print("\nANN Accuracy:", accuracy_score(y_test_ann, y_pred_ann))
|
| 126 |
+
print("\nANN Classification Report:\n", classification_report(y_test_ann, y_pred_ann))
|
| 127 |
+
|
| 128 |
+
"""### Gradio Implementation
|
| 129 |
+
|
| 130 |
+
Now, let's create a Gradio interface to make our combined K-Means and ANN model interactive. This interface will allow us to input raw water sample parameters and get an instant prediction of whether the water is safe, along with the predicted cluster type.
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
import gradio as gr
|
| 134 |
+
|
| 135 |
+
# The column order is important for consistent input to the model
|
| 136 |
+
feature_columns = X.columns.tolist()
|
| 137 |
+
|
| 138 |
+
def predict_water_safety(*args):
|
| 139 |
+
# Create a DataFrame from the input arguments, ensuring correct column order
|
| 140 |
+
input_data = pd.DataFrame([args], columns=feature_columns)
|
| 141 |
+
|
| 142 |
+
# Scale the input features using the already fitted scaler
|
| 143 |
+
input_scaled = scaler.transform(input_data)
|
| 144 |
+
|
| 145 |
+
# Predict the cluster for the input data
|
| 146 |
+
predicted_cluster = kmeans.predict(input_scaled)[0]
|
| 147 |
+
|
| 148 |
+
# Convert the scaled input to a DataFrame and add the cluster ID
|
| 149 |
+
input_scaled_df = pd.DataFrame(input_scaled, columns=feature_columns)
|
| 150 |
+
input_scaled_df['cluster'] = predicted_cluster
|
| 151 |
+
|
| 152 |
+
# Make prediction using the trained ANN
|
| 153 |
+
ann_prediction = mlp.predict(input_scaled_df)[0]
|
| 154 |
+
ann_proba = mlp.predict_proba(input_scaled_df)[0]
|
| 155 |
+
|
| 156 |
+
# Get the cluster label
|
| 157 |
+
cluster_label = cluster_label_map.get(predicted_cluster, "Unknown Cluster")
|
| 158 |
+
|
| 159 |
+
# Interpret the ANN prediction
|
| 160 |
+
safety_status = "Safe to Drink" if ann_prediction == 1 else "Not Safe to Drink"
|
| 161 |
+
confidence = ann_proba[ann_prediction] * 100
|
| 162 |
+
|
| 163 |
+
return f"Predicted Cluster Type: {cluster_label}", f"Water Safety Status: {safety_status} (Confidence: {confidence:.2f}%)"
|
| 164 |
+
|
| 165 |
+
# Define the examples list
|
| 166 |
+
examples_data = [
|
| 167 |
+
# WHO-safe sample
|
| 168 |
+
[0.2, 0.5, 0.01, 0.7, 0.003, 4.0, 0.05, 2.0, 1.5,
|
| 169 |
+
0.0, 0.0, 0.01, 10.0, 1.0, 0.006, 0.015, 5.0, 0.04, 0.1, 0.03],
|
| 170 |
+
## Heavily contaminated sample
|
| 171 |
+
[3.5, 29.8, 0.4, 1.5, 0.04, 8.6, 0.8, 2.0, 1.5,
|
| 172 |
+
0.9, 0.8, 0.18, 18.0, 2.93, 0.01, 0.08, 7.99, 0.1, 0.45, 0.05],
|
| 173 |
+
# Another heavily contaminated sample (expected: Not Safe to Drink)
|
| 174 |
+
[3.5, 29.8, 0.5, 1.5, 0.08, 8.6, 0.8, 2.0, 1.5,
|
| 175 |
+
1.0, 1.0, 0.2, 18.0, 2.93, 0.01, 0.08, 7.99, 0.1, 0.45, 0.08],
|
| 176 |
+
# Clean water sample (from original data, cluster 2)
|
| 177 |
+
[1.65, 9.08, 0.04, 2.85, 0.007, 4.0, 0.05, 2.0, 1.5,
|
| 178 |
+
0.0, 0.0, 0.01, 10.0, 1.0, 0.006, 0.015, 5.0, 0.04, 0.1, 0.03]
|
| 179 |
+
]
|
| 180 |
+
|
| 181 |
+
from sklearn.decomposition import PCA
|
| 182 |
+
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
|
| 183 |
+
|
| 184 |
+
def get_algo_plots():
|
| 185 |
+
# 1. K-Means Elbow Method
|
| 186 |
+
fig1 = plt.figure(figsize=(6, 4))
|
| 187 |
+
plt.plot(list(K_range), inertia, marker='o', color='b')
|
| 188 |
+
plt.xlabel("Number of Clusters (k)")
|
| 189 |
+
plt.ylabel("Inertia")
|
| 190 |
+
plt.title("K-Means Elbow Method")
|
| 191 |
+
plt.grid(True)
|
| 192 |
+
|
| 193 |
+
# 2. PCA Scatter Plot
|
| 194 |
+
pca = PCA(n_components=2)
|
| 195 |
+
X_pca = pca.fit_transform(X_scaled)
|
| 196 |
+
fig2 = plt.figure(figsize=(6, 4))
|
| 197 |
+
|
| 198 |
+
# Map colors from high risk (red) to clean (blue)
|
| 199 |
+
color_map = {
|
| 200 |
+
"Heavily contaminated group": "#d62728", # red
|
| 201 |
+
"Contaminated group": "#ff7f0e", # orange
|
| 202 |
+
"Borderline group": "#bcbd22", # yellow-green
|
| 203 |
+
"Clean water group": "#1f77b4" # blue
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
unique_clusters = np.unique(clusters)
|
| 207 |
+
for cid in unique_clusters:
|
| 208 |
+
idx = clusters == cid
|
| 209 |
+
label = cluster_label_map.get(cid, f"Cluster {cid}")
|
| 210 |
+
color = color_map.get(label, 'grey')
|
| 211 |
+
plt.scatter(X_pca[idx, 0], X_pca[idx, 1], label=label, alpha=0.6, s=15, color=color)
|
| 212 |
+
|
| 213 |
+
plt.xlabel("Principal Component 1")
|
| 214 |
+
plt.ylabel("Principal Component 2")
|
| 215 |
+
plt.title("PCA Cluster Visualization")
|
| 216 |
+
plt.legend(title='Water Profiles', fontsize='small', loc='best')
|
| 217 |
+
plt.tight_layout()
|
| 218 |
+
|
| 219 |
+
# 3. ANN Training Loss Curve
|
| 220 |
+
fig3 = plt.figure(figsize=(6, 4))
|
| 221 |
+
plt.plot(mlp.loss_curve_, color='red', linewidth=2)
|
| 222 |
+
plt.xlabel("Epochs")
|
| 223 |
+
plt.ylabel("Training Loss")
|
| 224 |
+
plt.title("ANN Training Loss Curve")
|
| 225 |
+
plt.grid(True)
|
| 226 |
+
|
| 227 |
+
# 4. ANN Confusion Matrix
|
| 228 |
+
fig4, ax4 = plt.subplots(figsize=(6, 4))
|
| 229 |
+
cm = confusion_matrix(y_test_ann, y_pred_ann)
|
| 230 |
+
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["Not Safe", "Safe"])
|
| 231 |
+
disp.plot(ax=ax4, cmap=plt.cm.Blues)
|
| 232 |
+
plt.title("ANN Confusion Matrix")
|
| 233 |
+
plt.tight_layout()
|
| 234 |
+
|
| 235 |
+
return fig1, fig2, fig3, fig4
|
| 236 |
+
|
| 237 |
+
with gr.Blocks(title="Water Quality Analysis") as iface:
|
| 238 |
+
gr.Markdown("# 💧 Detailed Water Quality Prediction System")
|
| 239 |
+
gr.Markdown("Analyze water parameters using K-Means Clustering and an Artificial Neural Network.")
|
| 240 |
+
|
| 241 |
+
with gr.Row():
|
| 242 |
+
with gr.Column(scale=2):
|
| 243 |
+
gr.Markdown("### 1. Enter Water Parameters")
|
| 244 |
+
input_components = []
|
| 245 |
+
|
| 246 |
+
# Create a 4-column layout for the inputs
|
| 247 |
+
with gr.Row():
|
| 248 |
+
cols = [gr.Column() for _ in range(4)]
|
| 249 |
+
for i, col in enumerate(feature_columns):
|
| 250 |
+
with cols[i % 4]:
|
| 251 |
+
inp = gr.Slider(minimum=float(X[col].min()), maximum=float(X[col].max()), label=col)
|
| 252 |
+
input_components.append(inp)
|
| 253 |
+
|
| 254 |
+
with gr.Row():
|
| 255 |
+
predict_btn = gr.Button("Predict Safety Status", variant="primary")
|
| 256 |
+
|
| 257 |
+
gr.Examples(
|
| 258 |
+
examples=examples_data,
|
| 259 |
+
inputs=input_components,
|
| 260 |
+
label="Quick Presets"
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
with gr.Column(scale=1):
|
| 264 |
+
gr.Markdown("### 2. Prediction Results")
|
| 265 |
+
out_cluster = gr.Textbox(label="Predicted Cluster Type")
|
| 266 |
+
out_safety = gr.Textbox(label="Water Safety Status")
|
| 267 |
+
|
| 268 |
+
predict_btn.click(
|
| 269 |
+
fn=predict_water_safety,
|
| 270 |
+
inputs=input_components,
|
| 271 |
+
outputs=[out_cluster, out_safety]
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
gr.Markdown("---")
|
| 275 |
+
gr.Markdown("### 📊 Algorithm Insights")
|
| 276 |
+
with gr.Row():
|
| 277 |
+
plot1 = gr.Plot(label="Elbow Method")
|
| 278 |
+
plot2 = gr.Plot(label="PCA Cluster Visualization")
|
| 279 |
+
with gr.Row():
|
| 280 |
+
plot3 = gr.Plot(label="ANN Loss Curve")
|
| 281 |
+
plot4 = gr.Plot(label="ANN Confusion Matrix")
|
| 282 |
+
|
| 283 |
+
iface.load(fn=get_algo_plots, inputs=None, outputs=[plot1, plot2, plot3, plot4])
|
| 284 |
+
|
| 285 |
+
# Launch the interface
|
| 286 |
+
iface.launch()
|