-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_functions.py
53 lines (42 loc) · 1.72 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
52
53
"""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()
def load_data():
"""This function returns the preprocessed data"""
# Load the Diabetes dataset into DataFrame.
df = pd.read_csv('https://s3-whjr-curriculum-uploads.whjr.online/b510b80d-2fd6-4c08-bfdf-2a24f733551d.csv')
# Rename the column names in the DataFrame.
df.rename(columns = {"BloodPressure": "Blood_Pressure",}, inplace = True)
df.rename(columns = {"SkinThickness": "Skin_Thickness",}, inplace = True)
df.rename(columns = {"DiabetesPedigreeFunction": "Pedigree_Function",}, inplace = True)
# Perform feature and target split
X = df[["Glucose", "Blood_Pressure", "Insulin", "BMI", "Pedigree_Function", "Age"]]
y = df['Outcome']
return df, X, y
@st.cache()
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