from tab_extra import add_extra_tabs import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.cluster import KMeans from sklearn.model_selection import train_test_split #load dataset data = pd.read_csv("waterQuality1.csv") #CHECK FOR MISSING VALUES for col in data.columns: print(col, data[col].unique()[:10]) #CHCK FOR OBJECT COLUMNS print(data.info()) #preprocessing (convert object colums into float, and !NUM value to mean) data.replace(['#NUM!', '?', '', 'NA', 'NULL'], np.nan, inplace=True) data = data.apply(pd.to_numeric, errors='coerce') data = data.fillna(data.mean()) #separating input and target columns X = data.drop('is_safe', axis=1) y_safe = data['is_safe'] #Normalization scaler = StandardScaler() X_scaled = scaler.fit_transform(X) #K means implementation inertia = [] K_range = range(1, 10) #for optimal k for k in K_range: kmeans = KMeans(n_clusters=k, random_state=42, n_init=10) kmeans.fit(X_scaled) inertia.append(kmeans.inertia_) optimal_k = 4 #train by setting k kmeans = KMeans(n_clusters=optimal_k, random_state=42, n_init=10) clusters = kmeans.fit_predict(X_scaled) #add extra column of cluster and anomaly into dataset data['cluster'] = clusters #train-test split X_train, X_test, y_train, y_test = train_test_split( X, y_safe, test_size=0.2, random_state=42 ) #mean of each cluster cluster_summary = data.groupby('cluster').mean() print("\nCluster Summary:") print(cluster_summary[['is_safe']]) #presentation of each cluster with label ranked_clusters = cluster_summary['is_safe'].sort_values().index.tolist() labels = [ "Heavily contaminated group", "Contaminated group", "Borderline group", "Clean water group" ] cluster_label_map = dict(zip(ranked_clusters, labels)) # Apply labels data['cluster_label'] = data['cluster'].map(cluster_label_map) print("\nCluster Counts (Numeric + Label):") print(data.groupby(['cluster', 'cluster_label']).size()) print("\nFirst 10 rows:") print(data[['cluster', 'cluster_label']].head(10)) # Preparing Data for ANN X_scaled_df = pd.DataFrame(X_scaled, columns=X.columns) X_scaled_df['cluster'] = clusters # Convert y_safe to integer type for classification y_safe_int = y_safe.astype(int) # Split the data into training and testing sets for the ANN X_train_ann, X_test_ann, y_train_ann, y_test_ann = train_test_split( X_scaled_df, y_safe_int, test_size=0.2, random_state=42 ) print("Shape of X_train_ann:", X_train_ann.shape) print("Shape of X_test_ann:", X_test_ann.shape) print("Shape of y_train_ann:", y_train_ann.shape) print("Shape of y_test_ann:", y_test_ann.shape) # Artificial Neural Network (ANN) Implementation from sklearn.neural_network import MLPClassifier from sklearn.metrics import accuracy_score, classification_report # Initialize the MLPClassifier (ANN) mlp = MLPClassifier( hidden_layer_sizes=(100, 50), # Two hidden layers with 100 and 50 neurons respectively max_iter=500, # Maximum number of iterations for the solver to converge activation='relu', # Rectified Linear Unit activation function solver='adam', # Adam optimizer random_state=42, # For reproducibility verbose=True # To see the training progress ) # Train the ANN mlp.fit(X_train_ann, y_train_ann) # Make predictions on the test set y_pred_ann = mlp.predict(X_test_ann) # Evaluate the ANN print("\nANN Accuracy:", accuracy_score(y_test_ann, y_pred_ann)) print("\nANN Classification Report:\n", classification_report(y_test_ann, y_pred_ann)) # Gradio Implementation import gradio as gr # The column order is important for consistent input to the model feature_columns = X.columns.tolist() def predict_water_safety(*args): # Create a DataFrame from the input arguments, ensuring correct column order input_data = pd.DataFrame([args], columns=feature_columns) # Scale the input features using the already fitted scaler input_scaled = scaler.transform(input_data) # Predict the cluster for the input data predicted_cluster = kmeans.predict(input_scaled)[0] # Convert the scaled input to a DataFrame and add the cluster ID input_scaled_df = pd.DataFrame(input_scaled, columns=feature_columns) input_scaled_df['cluster'] = predicted_cluster # Make prediction using the trained ANN ann_prediction = mlp.predict(input_scaled_df)[0] ann_proba = mlp.predict_proba(input_scaled_df)[0] # Get the cluster label cluster_label = cluster_label_map.get(predicted_cluster, "Unknown Cluster") # Interpret the ANN prediction safety_status = "Safe to Drink" if ann_prediction == 1 else "Not Safe to Drink" confidence = ann_proba[ann_prediction] * 100 return f"Predicted Cluster Type: {cluster_label}", f"Water Safety Status: {safety_status} (Confidence: {confidence:.2f}%)" # Define the examples list examples_data = [ # WHO-safe sample [0.2, 0.5, 0.01, 0.7, 0.003, 4.0, 0.05, 2.0, 1.5, 0.0, 0.0, 0.01, 10.0, 1.0, 0.006, 0.015, 5.0, 0.04, 0.1, 0.03], ## Heavily contaminated sample [3.5, 29.8, 0.4, 1.5, 0.04, 8.6, 0.8, 2.0, 1.5, 0.9, 0.8, 0.18, 18.0, 2.93, 0.01, 0.08, 7.99, 0.1, 0.45, 0.05], # Another heavily contaminated sample (expected: Not Safe to Drink) [3.5, 29.8, 0.5, 1.5, 0.08, 8.6, 0.8, 2.0, 1.5, 1.0, 1.0, 0.2, 18.0, 2.93, 0.01, 0.08, 7.99, 0.1, 0.45, 0.08], # Clean water sample (from original data, cluster 2) [1.65, 9.08, 0.04, 2.85, 0.007, 4.0, 0.05, 2.0, 1.5, 0.0, 0.0, 0.01, 10.0, 1.0, 0.006, 0.015, 5.0, 0.04, 0.1, 0.03] ] from sklearn.decomposition import PCA from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay def get_algo_plots(): # 1. K-Means Elbow Method fig1 = plt.figure(figsize=(6, 4)) plt.plot(list(K_range), inertia, marker='o', color='b') plt.xlabel("Number of Clusters (k)") plt.ylabel("Inertia") plt.title("K-Means Elbow Method") plt.grid(True) # 2. PCA Scatter Plot pca = PCA(n_components=2) X_pca = pca.fit_transform(X_scaled) fig2 = plt.figure(figsize=(6, 4)) # Map colors from high risk (red) to clean (blue) color_map = { "Heavily contaminated group": "#d62728", # red "Contaminated group": "#ff7f0e", # orange "Borderline group": "#bcbd22", # yellow-green "Clean water group": "#1f77b4" # blue } unique_clusters = np.unique(clusters) for cid in unique_clusters: idx = clusters == cid label = cluster_label_map.get(cid, f"Cluster {cid}") color = color_map.get(label, 'grey') plt.scatter(X_pca[idx, 0], X_pca[idx, 1], label=label, alpha=0.6, s=15, color=color) plt.xlabel("Principal Component 1") plt.ylabel("Principal Component 2") plt.title("PCA Cluster Visualization") plt.legend(title='Water Profiles', fontsize='small', loc='best') plt.tight_layout() # 3. ANN Training Loss Curve fig3 = plt.figure(figsize=(6, 4)) plt.plot(mlp.loss_curve_, color='red', linewidth=2) plt.xlabel("Epochs") plt.ylabel("Training Loss") plt.title("ANN Training Loss Curve") plt.grid(True) # 4. ANN Confusion Matrix fig4, ax4 = plt.subplots(figsize=(6, 4)) cm = confusion_matrix(y_test_ann, y_pred_ann) disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["Not Safe", "Safe"]) disp.plot(ax=ax4, cmap=plt.cm.Blues) plt.title("ANN Confusion Matrix") plt.tight_layout() return fig1, fig2, fig3, fig4 with gr.Blocks(title="Water Quality Analysis") as iface: gr.Markdown("# 💧 Detailed Water Quality Prediction System") gr.Markdown("Analyze water parameters using K-Means Clustering and an Artificial Neural Network.") with gr.Tabs(): with gr.TabItem("🔍 Predict"): with gr.Row(): with gr.Column(scale=2): gr.Markdown("### 1. Enter Water Parameters") input_components = [] # Create a 4-column layout for the inputs with gr.Row(): cols = [gr.Column() for _ in range(4)] for i, col in enumerate(feature_columns): with cols[i % 4]: inp = gr.Slider(minimum=float(X[col].min()), maximum=float(X[col].max()), label=col) input_components.append(inp) with gr.Row(): predict_btn = gr.Button("Predict Safety Status", variant="primary") gr.Examples( examples=examples_data, inputs=input_components, label="Quick Presets" ) with gr.Column(scale=1): gr.Markdown("### 2. Prediction Results") out_cluster = gr.Textbox(label="Predicted Cluster Type") out_safety = gr.Textbox(label="Water Safety Status") predict_btn.click( fn=predict_water_safety, inputs=input_components, outputs=[out_cluster, out_safety] ) gr.Markdown("---") gr.Markdown("### Algorithm Insights") with gr.Row(): plot1 = gr.Plot(label="Elbow Method") plot2 = gr.Plot(label="PCA Cluster Visualization") with gr.Row(): plot3 = gr.Plot(label="ANN Loss Curve") plot4 = gr.Plot(label="ANN Confusion Matrix") iface.load(fn=get_algo_plots, inputs=None, outputs=[plot1, plot2, plot3, plot4]) add_extra_tabs() # Launch the interface iface.launch(debug=True, share=True)