Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
==========================================================
|
3 |
+
Gradio demo to Plot multi-class SGD on the iris dataset
|
4 |
+
==========================================================
|
5 |
+
|
6 |
+
Plot decision surface of multi-class SGD on iris dataset.
|
7 |
+
The hyperplanes corresponding to the three one-versus-all (OVA) classifiers
|
8 |
+
are represented by the dashed lines.
|
9 |
+
|
10 |
+
Created by Syed Affan <saffand03@gmail.com>
|
11 |
+
|
12 |
+
"""
|
13 |
+
import gradio as gr
|
14 |
+
import numpy as np
|
15 |
+
import matplotlib.pyplot as plt
|
16 |
+
from sklearn import datasets
|
17 |
+
from sklearn.linear_model import SGDClassifier
|
18 |
+
from sklearn.inspection import DecisionBoundaryDisplay
|
19 |
+
import matplotlib.cm
|
20 |
+
|
21 |
+
def make_plot(alpha,max_iter,Standardize):
|
22 |
+
# import some data to play with
|
23 |
+
iris = datasets.load_iris()
|
24 |
+
fig = plt.figure()
|
25 |
+
|
26 |
+
# we only take the first two features. We could
|
27 |
+
# avoid this ugly slicing by using a two-dim dataset
|
28 |
+
X = iris.data[:, :2]
|
29 |
+
y = iris.target
|
30 |
+
colors = "bry"
|
31 |
+
|
32 |
+
# shuffle
|
33 |
+
idx = np.arange(X.shape[0])
|
34 |
+
np.random.seed(13)
|
35 |
+
np.random.shuffle(idx)
|
36 |
+
X = X[idx]
|
37 |
+
y = y[idx]
|
38 |
+
|
39 |
+
# standardize
|
40 |
+
if Standardize:
|
41 |
+
mean = X.mean(axis=0)
|
42 |
+
std = X.std(axis=0)
|
43 |
+
X = (X - mean) / std
|
44 |
+
|
45 |
+
|
46 |
+
clf = SGDClassifier(alpha=alpha, max_iter=max_iter).fit(X, y)
|
47 |
+
accuracy = clf.score(X,y)
|
48 |
+
acc = f'## The Accuracy on the entire dataset: {accuracy}'
|
49 |
+
ax = plt.gca()
|
50 |
+
DecisionBoundaryDisplay.from_estimator(
|
51 |
+
clf,
|
52 |
+
X,
|
53 |
+
cmap=matplotlib.cm.Paired,
|
54 |
+
ax=ax,
|
55 |
+
response_method="predict",
|
56 |
+
xlabel=iris.feature_names[0],
|
57 |
+
ylabel=iris.feature_names[1],
|
58 |
+
)
|
59 |
+
plt.axis("tight")
|
60 |
+
|
61 |
+
# Plot also the training points
|
62 |
+
for i, color in zip(clf.classes_, colors):
|
63 |
+
idx = np.where(y == i)
|
64 |
+
plt.scatter(
|
65 |
+
X[idx, 0],
|
66 |
+
X[idx, 1],
|
67 |
+
c=color,
|
68 |
+
label=iris.target_names[i],
|
69 |
+
cmap=matplotlib.cm.Paired,
|
70 |
+
edgecolor="black",
|
71 |
+
s=20,
|
72 |
+
)
|
73 |
+
plt.title("Decision surface of multi-class SGD")
|
74 |
+
plt.axis("tight")
|
75 |
+
|
76 |
+
# Plot the three one-against-all classifiers
|
77 |
+
xmin, xmax = plt.xlim()
|
78 |
+
ymin, ymax = plt.ylim()
|
79 |
+
coef = clf.coef_
|
80 |
+
intercept = clf.intercept_
|
81 |
+
|
82 |
+
|
83 |
+
def plot_hyperplane(c, color):
|
84 |
+
def line(x0):
|
85 |
+
return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
|
86 |
+
|
87 |
+
plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)
|
88 |
+
|
89 |
+
|
90 |
+
for i, color in zip(clf.classes_, colors):
|
91 |
+
plot_hyperplane(i, color)
|
92 |
+
plt.legend()
|
93 |
+
|
94 |
+
return fig,acc
|
95 |
+
|
96 |
+
title = "Plot multi-class SGD on the iris dataset"
|
97 |
+
|
98 |
+
model_card = f"""
|
99 |
+
## Description
|
100 |
+
Plot decision surface of multi-class SGD on iris dataset.
|
101 |
+
The hyperplanes corresponding to the three one-versus-all (OVA) classifiers are represented by the dashed lines.
|
102 |
+
## Dataset
|
103 |
+
Iris Dataset
|
104 |
+
"""
|
105 |
+
|
106 |
+
with gr.Blocks(title=title) as demo:
|
107 |
+
gr.Markdown('''
|
108 |
+
<div>
|
109 |
+
<h1 style='text-align: center'>⚒ Plot multi-class SGD on iris dataset 🛠</h1>
|
110 |
+
</div>
|
111 |
+
''')
|
112 |
+
|
113 |
+
gr.Markdown(model_card)
|
114 |
+
d0 = gr.Slider(0.001,5,step=0.001,value=0.001,label='alpha')
|
115 |
+
d1 = gr.Slider(1,1000,step=10,value=100,label='max_iter')
|
116 |
+
d2 = gr.Checkbox(value=True,label='Standardize')
|
117 |
+
|
118 |
+
btn =gr.Button(value='Submit')
|
119 |
+
btn.click(make_plot,inputs=[d0,d1,d2],outputs=[gr.Plot(),gr.Markdown()])
|
120 |
+
|
121 |
+
demo.launch()
|