This repository was archived by the owner on Jul 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML.py
More file actions
140 lines (121 loc) · 6.44 KB
/
Copy pathML.py
File metadata and controls
140 lines (121 loc) · 6.44 KB
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
from numpy.core.numeric import True_
from sklearn import metrics
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, RocCurveDisplay, PrecisionRecallDisplay
from sklearn.metrics import precision_score, recall_score
from sklearn.tree import DecisionTreeClassifier
st.set_option('deprecation.showPyplotGlobalUse', False)
def main():
st.title('Introduction to building Streamlit WebApp')
st.sidebar.title('This is the sidebar')
st.sidebar.markdown('Let’s start with binary classification!!')
if __name__ == "__main__":
main()
@st.cache_data(persist= True)
def load():
data= pd.read_csv("data/Updated_Subset_1.csv")
# label= LabelEncoder()
# for i in data.columns:
# data[i] = label.fit_transform(data[i])
return data
df = load()
#df.loc[df.label != '4', 'label'] = 0
#df.loc[df.label == '4', 'label'] = 1
if st.sidebar.checkbox("Display data", False):
st.subheader("Show mHealth dataset")
st.write(df)
@st.cache_data(persist=True)
def split(df):
y = df.label
x = df.drop(columns=["sub_id", "label"])
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.3, random_state=0)
return x_train, x_test, y_train, y_test
x_train, x_test, y_train, y_test = split(df)
def plot_metrics(metrics_list):
if "Confusion Matrix" in metrics_list:
st.subheader("Confusion Matrix")
ConfusionMatrixDisplay.from_estimator(model, x_test, y_test, display_labels=class_names)
st.pyplot()
#cm=confusion_matrix(y_test, y_pred)
#ConfusionMatrixDisplay(cm,model.classes_).plot()
if "ROC Curve" in metrics_list:
st.subheader("ROC Curve")
RocCurveDisplay.from_estimator(model, x_test, y_test)
st.pyplot()
if "Precision-Recall Curve" in metrics_list:
st.subheader("Precision-Recall Curve")
PrecisionRecallDisplay.from_estimator(model, x_test, y_test)
st.pyplot()
class_names = ["walk", "run"]
st.sidebar.subheader("Choose classifier")
classifier = st.sidebar.selectbox("Classifier", ("Support Vector Machine (SVM)", "Logistic Regression", "Random Forest", "Decision Tree"))
if classifier == "Support Vector Machine (SVM)":
st.sidebar.subheader("Hyperparameters")
C = st.sidebar.number_input("C (Regularization parameter)", 0.01, 10.0, step=0.01, key="C")
kernel = st.sidebar.radio("Kernel", ("rbf", "linear"), key="kernel")
gamma = st.sidebar.radio("Gamma (Kernal coefficient", ("scale", "auto"), key="gamma")
metrics = st.sidebar.multiselect("What metrics to plot?", ("Confusion Matrix", "ROC Curve", "Precision-Recall Curve"))
if st.sidebar.button("Classify", key="classify"):
st.subheader("Support Vector Machine (SVM) results")
model = SVC(C=C, kernel=kernel, gamma=gamma)
model.fit(x_train, y_train)
accuracy = model.score(x_test, y_test)
y_pred = model.predict(x_test)
st.write("Accuracy: ", accuracy.round(2))
st.write("Precision: ", precision_score(y_test, y_pred, labels=class_names).round(2))
st.write("Recall: ", recall_score(y_test, y_pred, labels=class_names).round(2))
plot_metrics(metrics)
if classifier == "Logistic Regression":
st.sidebar.subheader("Hyperparameters")
C = st.sidebar.number_input("C (Regularization parameter)", 0.01, 10.0, step=0.01, key="C_LR")
max_iter = st.sidebar.slider("Maximum iterations", 100, 500, key="max_iter")
metrics = st.sidebar.multiselect("What metrics to plot?", ("Confusion Matrix", "ROC Curve", "Precision-Recall Curve"))
if st.sidebar.button("Classify", key="classify"):
st.subheader("Logistic Regression Results")
model = LogisticRegression(C=C, max_iter=max_iter)
model.fit(x_train, y_train)
accuracy = model.score(x_test, y_test)
y_pred = model.predict(x_test)
st.write("Accuracy: ", accuracy.round(2))
st.write("Precision: ", precision_score(y_test, y_pred, labels=class_names).round(2))
st.write("Recall: ", recall_score(y_test, y_pred, labels=class_names).round(2))
plot_metrics(metrics)
if classifier == "Random Forest":
st.sidebar.subheader("Hyperparameters")
n_estimators= st.sidebar.number_input("The number of trees in the forest", 100, 5000, step=10, key="n_estimators")
max_depth = st.sidebar.number_input("The maximum depth of tree", 1, 20, step =1, key="max_depth")
bootstrap = st.sidebar.radio("Bootstrap samples when building trees", ("True", "False"), key="bootstrap")
metrics = st.sidebar.multiselect("What metrics to plot?", ("Confusion Matrix", "ROC Curve", "Precision-Recall Curve"))
if st.sidebar.button("Classify", key="classify"):
st.subheader("Random Forest Results")
model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, bootstrap= bootstrap, n_jobs=-1 )
model.fit(x_train, y_train)
accuracy = model.score(x_test, y_test)
y_pred = model.predict(x_test)
st.write("Accuracy: ", accuracy.round(2))
st.write("Precision: ", precision_score(y_test, y_pred, labels=class_names).round(2))
st.write("Recall: ", recall_score(y_test, y_pred, labels=class_names).round(2))
plot_metrics(metrics)
if classifier == 'Decision Tree':
st.sidebar.subheader('Model parameters')
#choose parameters
criterion= st.sidebar.radio('Criterion(measures the quality of split)', ('gini', 'entropy'), key='criterion')
splitter = st.sidebar.radio('Splitter (How to split at each node?)', ('best', 'random'), key='splitter')
metrics = st.sidebar.multiselect('Select your metrics : ', ('Confusion Matrix', 'ROC Curve', 'Precision-Recall Curve'))
if st.sidebar.button('Classify', key='classify'):
st.subheader('Decision Tree Results')
model = DecisionTreeClassifier(criterion=criterion, splitter=splitter)
model.fit(x_train, y_train)
accuracy = model.score(x_test, y_test)
y_pred = model.predict(x_test)
st.write('Accuracy: ', accuracy.round(2)*100,'%')
st.write('Precision: ', precision_score(y_test, y_pred, labels=class_names).round(2))
st.write('Recall: ', recall_score(y_test, y_pred, labels=class_names).round(2))
plot_metrics(metrics)