forked from mssrisupang/Streamlit-Binary-Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
150 lines (121 loc) · 6.58 KB
/
app.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
import streamlit as st
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
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 precision_score, recall_score
from sklearn.metrics import ConfusionMatrixDisplay, RocCurveDisplay, PrecisionRecallDisplay
def main():
################ Step 1 Create Web Title #####################
st.title("Binary Classification Streamlit Application")
st.markdown(" เห็ดนี้กินได้หรือไม่??? 🍄🟫 By Gasidid 125")
st.markdown("This Application made for Mushroom Classification and Deploy Test")
st.sidebar.title("Binary Classification Streamlit App")
st.sidebar.markdown(" เห็ดนี้กินได้หรือไม่??? 🍄🟫🍄🟫🍄🟫")
############### Step 2 Load dataset and Preprocessing data ##########
# create read_csv function
@st.cache_data(persist=True) #ไม่ให้ streamlit โหลดข้อมูลใหม่ทุกๆครั้งเมื่อ run
def load_data():
# Define the base directory as the folder containing your script
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, 'data')
file_path = os.path.join(DATA_DIR, 'mushrooms.csv')
data = pd.read_csv(file_path)
label = LabelEncoder()
for col in data.columns:
data[col] = label.fit_transform(data[col])
return data
@st.cache_data(persist=True)
def spliting_data(df):
y = df.type
x = df.drop(columns=['type'])
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
def plot_metrics(metrics_list):
if 'Confusion Matrix' in metrics_list:
st.subheader("Confusion Matrix")
fig, ax = plt.subplots()
ConfusionMatrixDisplay.from_estimator(model, x_test, y_test, ax=ax, display_labels=class_names)
st.pyplot(fig)
if 'ROC Curve' in metrics_list:
st.subheader("ROC Curve")
fig, ax = plt.subplots()
RocCurveDisplay.from_estimator(model, x_test, y_test, ax=ax)
st.pyplot(fig)
if 'Precision-Recall Curve' in metrics_list:
st.subheader("Precision-Recall Curve")
fig, ax = plt.subplots()
PrecisionRecallDisplay.from_estimator(model, x_test, y_test, ax=ax)
st.pyplot(fig)
# Load and split data
df = load_data()
x_train, x_test, y_train, y_test = spliting_data(df)
class_names = ['edible', 'poisonous']
# Sidebar for classifier selection
st.sidebar.subheader("Choose Classifiers")
classifier = st.sidebar.selectbox("Classifier", ("Support Vector Machine (SVM)", "Logistic Regression", "Random Forest"))
############### Step 3 Train a SVM Classifier ##########
if classifier == 'Support Vector Machine (SVM)':
st.sidebar.subheader("Model 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 (Kernel 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)
precision = precision_score(y_test, y_pred).round(2)
recall = recall_score(y_test, y_pred).round(2)
st.write("Accuracy: ", round(accuracy, 2))
st.write("Precision: ", precision)
st.write("Recall: ", recall)
plot_metrics(metrics)
############### Step 4 Training a Logistic Regression Classifier ##########
if classifier == 'Logistic Regression':
st.sidebar.subheader("Model Hyperparameters")
C = st.sidebar.number_input("C (Regularization parameter)", 0.01, 10.0, step=0.01, key='logistic_C')
metrics = st.sidebar.multiselect("What metrics to plot?", ("Confusion Matrix", "ROC Curve", "Precision-Recall Curve"))
if st.sidebar.button("Classify", key='logistic_classify'):
st.subheader("Logistic Regression results")
model = LogisticRegression(C=C, max_iter=1000)
model.fit(x_train, y_train)
accuracy = model.score(x_test, y_test)
y_pred = model.predict(x_test)
precision = precision_score(y_test, y_pred).round(2)
recall = recall_score(y_test, y_pred).round(2)
st.write("Accuracy: ", round(accuracy, 2))
st.write("Precision: ", precision)
st.write("Recall: ", recall)
plot_metrics(metrics)
############### Step 5 Training a Random Forest Classifier ##########
if classifier == 'Random Forest':
st.sidebar.subheader("Model 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 the tree", 1, 20, step=1, key='max_depth')
metrics = st.sidebar.multiselect("What metrics to plot?", ("Confusion Matrix", "ROC Curve", "Precision-Recall Curve"))
if st.sidebar.button("Classify", key='rf_classify'):
st.subheader("Random Forest results")
model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=0)
model.fit(x_train, y_train)
accuracy = model.score(x_test, y_test)
y_pred = model.predict(x_test)
precision = precision_score(y_test, y_pred).round(2)
recall = recall_score(y_test, y_pred).round(2)
st.write("Accuracy: ", round(accuracy, 2))
st.write("Precision: ", precision)
st.write("Recall: ", recall)
plot_metrics(metrics)
# Display raw data
if st.sidebar.checkbox("Show raw data", False):
st.subheader("Mushroom dataset")
st.write(df)
if __name__ == '__main__':
main()