forked from dmr0010/Water-Quality-Inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_functions.py
51 lines (38 loc) · 1.5 KB
/
web_functions.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
"""This module contains necessary function needed"""
# Import necessary modules
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
import streamlit as st
@st.cache_data()
def load_data():
"""This function returns the preprocessed data"""
# Load the Diabetes dataset into DataFrame.
df = pd.read_csv('water_potability.csv')
# Perform feature and target split
X = df[["aluminium", "ammonia", "arsenic","barium","cadmium","chloramine","chromium","copper","flouride","bacteria","viruses","lead","nitrates","nitrites","mercury","radium","selenium","silver","uranium"]]
y = df['is_safe']
return df, X, y
@st.cache_data()
def train_model(X, y):
"""This function trains the model and return the model and model score"""
# Create the model
model = DecisionTreeClassifier(
ccp_alpha=0.0, class_weight=None, criterion='entropy',
max_depth=4, max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
random_state=42, splitter='best'
)
# Fit the data on model
model.fit(X, y)
# Get the model score
score = model.score(X, y)
# Return the values
return model, score
def predict(X, y, features):
# Get model and model score
model, score = train_model(X, y)
# Predict the value
prediction = model.predict(np.array(features).reshape(1, -1))
return prediction, score