forked from campusx-official/100-days-of-machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit-viz-tool.py
83 lines (61 loc) · 2.41 KB
/
streamlit-viz-tool.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
import matplotlib.pyplot as plt
import streamlit as st
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification,make_blobs
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
def load_initial_graph(dataset,ax):
if dataset == "Binary":
X, y = make_blobs(n_features=2, centers=2,random_state=6)
ax.scatter(X.T[0], X.T[1], c=y, cmap='rainbow')
return X,y
elif dataset == "Multiclass":
X,y = make_blobs(n_features=2, centers=3,random_state=2)
ax.scatter(X.T[0], X.T[1], c=y, cmap='rainbow')
return X,y
def draw_meshgrid():
a = np.arange(start=X[:, 0].min() - 1, stop=X[:, 0].max() + 1, step=0.01)
b = np.arange(start=X[:, 1].min() - 1, stop=X[:, 1].max() + 1, step=0.01)
XX, YY = np.meshgrid(a, b)
input_array = np.array([XX.ravel(), YY.ravel()]).T
return XX, YY, input_array
plt.style.use('fivethirtyeight')
st.sidebar.markdown("# Logistic Regression Classifier")
dataset = st.sidebar.selectbox(
'Select Dataset',
('Binary','Multiclass')
)
penalty = st.sidebar.selectbox(
'Regularization',
('l2', 'l1','elasticnet','none')
)
c_input = float(st.sidebar.number_input('C',value=1.0))
solver = st.sidebar.selectbox(
'Solver',
('newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga')
)
max_iter = int(st.sidebar.number_input('Max Iterations',value=100))
multi_class = st.sidebar.selectbox(
'Multi Class',
('auto', 'ovr', 'multinomial')
)
l1_ratio = int(st.sidebar.number_input('l1 Ratio'))
# Load initial graph
fig, ax = plt.subplots()
# Plot initial graph
X,y = load_initial_graph(dataset,ax)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
orig = st.pyplot(fig)
if st.sidebar.button('Run Algorithm'):
orig.empty()
clf = LogisticRegression(penalty=penalty,C=c_input,solver=solver,max_iter=max_iter,multi_class=multi_class,l1_ratio=l1_ratio)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
XX, YY, input_array = draw_meshgrid()
labels = clf.predict(input_array)
ax.contourf(XX, YY, labels.reshape(XX.shape), alpha=0.5, cmap='rainbow')
plt.xlabel("Col1")
plt.ylabel("Col2")
orig = st.pyplot(fig)
st.subheader("Accuracy for Decision Tree " + str(round(accuracy_score(y_test, y_pred), 2)))