-
Notifications
You must be signed in to change notification settings - Fork 0
/
Water_potability_deployment.py
256 lines (199 loc) · 8.88 KB
/
Water_potability_deployment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import streamlit as st
import numpy as np
import pandas as pd
import joblib
from sklearn.preprocessing import StandardScaler
from streamlit_lottie import st_lottie
import requests
from streamlit_option_menu import option_menu
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
######################## Functions & Definitions ########################
# Scale features and predict
def predict_potability(model, X):
# Scale input data using the fitted scaler
X_scaled = scaler.transform(X)
# Predict potability
y_pred = model.predict(X_scaled)
return y_pred
# Display animation
def load_lottie(url):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
# Loading the scaler
scaler = joblib.load("Models\scaler.joblib")
# Loading the models
rf_model = joblib.load("Models\Rf.joblib")
GB_clf_model = joblib.load("Models\GB.joblib")
decision_tree_model = joblib.load("Models\Dt.joblib")
knn_model = joblib.load("Models\knn.joblib")
log_reg_model = joblib.load("Models\log_reg.joblib")
svm_model = joblib.load("Models\svm.joblib")
# Define models dictionary
models = {
"Gradient Boosting": GB_clf_model,
"Random Forest": rf_model,
"Decision Tree": decision_tree_model,
"KNN": knn_model,
"Logistic Regression": log_reg_model,
"SVM": svm_model
}
# Define confusion matrices dictionary
confusionMatrices = {
"Gradient Boosting": "Graphs\Matrix GB.png",
"Random Forest": "Graphs\Matrix RF.png",
"Decision Tree": "Graphs\Matix DT.png",
"KNN": "Graphs\Matrix KNN.png",
"Logistic Regression": "Graphs\Matrix LR.png",
"SVM": "Graphs\Matrix SVM.png"
}
accuracies = {
"Gradient Boosting": "Accuracies\gradient_boosting.txt",
"Random Forest": "Accuracies\Random_forest.txt",
"Decision Tree": "Accuracies\Decision Tree.txt",
"KNN": "Accuracies\k_n_n.txt",
"Logistic Regression": "Accuracies\Logistic Regression.txt",
"SVM": "Accuracies\Support Vector Machine.txt"
}
# Define the columns
columns = ['PH', 'Hardness', 'Solids', 'Chloramines', 'Sulfate', 'Conductivity', 'Organic carbon',
'Trihalomethanes', 'Turbidity']
######################## Main Display ########################
#Set page configuration
st.set_page_config(
page_title='Water Potabiliy Prediction',
page_icon='::magnifier::',
initial_sidebar_state='expanded',
)
# Center align the header
st.markdown("<h1 style='text-align: center;'>Water Potability Prediction System</h1>", unsafe_allow_html=True)
# Display animation at the top
lottie_link = "https://lottie.host/12d4c21a-4d80-44bf-bee9-f12771920386/ho13u1DTVB.json"
animation = load_lottie(lottie_link)
# Error handling for animation
if animation is not None:
st_lottie(animation, speed=1, height=300, key="initial")
else:
st.write("Failed to load animation")
# Sidebar Design
with st.sidebar:
choose = option_menu(None, ["About", "Predictions", "Graphs"],
icons=[ 'house','kanban', 'book'],
menu_icon="app-indicator", default_index=0,
styles={
"container": {"padding": "5!important", "background-color": "#fafafa"},
"icon": {"color": '#E0E0EF', "font-size": "25px"},
"nav-link": {"font-size": "16px", "text-align": "left", "margin":"0px", "--hover-color": "#eee"},
"nav-link-selected": {"background-color": "#428DFF"},
}
)
# About Page
if choose == 'About':
st.write('### Water Potability System About:')
st.write('---')
st.write("##### Access to safe drinking water is essential to health, a basic human right and a component of effective policy for health protection.\n ##### This is important as a health and development issue at a national, regional, and local level.\n ##### In some regions, it has been shown that investments in water supply and sanitation can yield a net economic benefit, since the reductions in adverse health effects and health care costs outweigh the costs of undertaking the interventions.\n ##### This classification project involves building a model to evaluate water potability to help you access safe drinking water. 🚰💙")
# Predictions Page
elif choose == 'Predictions':
st.write('### Water Potability System Predictions:')
st.write('---')
# Select model using dropdown menu
selected_model = st.selectbox("Select Model", list(models.keys()), key='model_selection')
# Open the file in read mode
file_path = accuracies[selected_model] # Get the file path from the dictionary
with open(file_path, "r") as file:
# Read the contents of the file
file_contents = file.read()
session_state = st.session_state
if 'Show_matrix_and_accuracy' not in session_state:
session_state.Show_matrix_and_accuracy = False
# Toggles button so that when it's pressed, it stays pressed and doesn't refresh.
if st.button('Show matrix and accuracy'):
session_state.Show_matrix_and_accuracy = True
if session_state.Show_matrix_and_accuracy:
# Display accuracy and confussion matrix using the selected model
st.write("##### This is the accuracy of the", selected_model , " : ", file_contents, '%')
st.write(" ")
st.write("##### This is the confussion matrix for the ", selected_model , " :")
st.write(" ")
st.image(confusionMatrices[selected_model])
st.write('---')
# Add input fields for all columns and take input from user
inputs = {}
for col in columns:
inputs[col] = st.number_input(f'**Enter {col}**', min_value=0.0, format="%.9f")
if st.button('Predict'):
# Create list with columns values from inputs dictionary
values_list = []
for col, value in inputs.items():
# Append each value to the list
values_list.append(value)
# Convert the list to a NumPy array
X = np.array([values_list]).reshape(1, -1)
# Predict potability using the selected model
y_pred = predict_potability(models[selected_model], X)
# Display the predicted potability
if y_pred == 1:
st.write("#### Our Prediction Says: This Water Is Safe To Drink. 💙")
st.balloons()
elif y_pred == 0:
st.write("#### Our Prediction Says: This Water Isn't Safe To Drink. ❌")
# Graphs Page
elif choose == 'Graphs':
st.write('### Water Potability System Graphs :')
st.write('---')
# Pie chart
st.write("### Distribution of potability class:")
st.write("##### Before Oversampling:")
st.image("Graphs\True and false biased.png")
st.write("##### After Oversampling:")
st.image("Graphs\True and false oversampled.png")
# Box Plot
st.write("### Box Plot Graph Before Scaling:")
st.image("Graphs\Boxplots of non Scaled Water Quality Parameters.png")
st.write("### Box Plot Graph After Scaling:")
st.image("Graphs\Boxplots of Scaled Water Quality Parameters.png")
# Correlation Map
st.write("### Correlation Map Before Oversampling And Scaling:")
st.image("Graphs\Heat map.png")
# Histograms
st.write("### Histograms Of Unscaled Data Of Columns Containing Null Values:")
st.write("##### Sulfate Histogram:")
st.image("Graphs\Sulfate Histogram.png")
st.write("##### ph Histogram:")
st.image("Graphs\ph Histogram.png")
st.write("##### Trihalomethanes Histogram:")
st.image("Graphs\Trihalomethanes Histogram.png")
st.write("### Histograms Of Scaled Data:")
st.write("##### Chloromines Histogram:")
st.image("Graphs\HistogramChloromines.png")
st.write("##### Conductivity Histogram:")
st.image("Graphs\HistogramConduct.png")
st.write("##### Hardness Histogram:")
st.image("Graphs\HistogramHardness.png")
st.write("##### Organic Carbon Histogram:")
st.image("Graphs\HistogramOrganicCO2.png")
st.write("##### PH Histogram:")
st.image("Graphs\HistogramPH.png")
st.write("##### Solids Histogram:")
st.image("Graphs\HistogramSolids.png")
st.write("##### Sulphate Histogram:")
st.image("Graphs\HistogramSulphate.png")
st.write("##### Trihalomethanes Histogram:")
st.image("Graphs\HistogramTrihalo.png")
st.write("##### Turbidiry Histogram:")
st.image("Graphs\HistogramTurbidity.png")
# Confusion Matrices
st.write("### Confusion Matrix Random Forest:")
st.image("Graphs\Matrix RF.png")
st.write("### Confusion Matrix Decision Tree:")
st.image("Graphs\Matix DT.png")
st.write("### Confusion Matrix Support Vector Machine:")
st.image("Graphs\Matrix SVM.png")
st.write("### Confusion Matrix Logistic Regression:")
st.image("Graphs\Matrix LR.png")
st.write("### Confusion Matrix KNN:")
st.image("Graphs\Matrix KNN.png")
st.write("### Confusion Matrix Gradient Boosting:")
st.image("Graphs\Matrix GB.png")