Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,16 @@
|
|
1 |
-
|
2 |
"""
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
|
7 |
Plot decision surface of multi-class SGD on iris dataset.
|
8 |
The hyperplanes corresponding to the three one-versus-all (OVA) classifiers
|
9 |
are represented by the dashed lines.
|
10 |
|
|
|
|
|
11 |
"""
|
|
|
12 |
import gradio as gr
|
13 |
import numpy as np
|
14 |
import matplotlib.pyplot as plt
|
@@ -17,10 +19,10 @@ from sklearn.linear_model import SGDClassifier
|
|
17 |
from sklearn.inspection import DecisionBoundaryDisplay
|
18 |
import matplotlib.cm
|
19 |
|
20 |
-
|
21 |
-
def plot(alpha):
|
22 |
# import some data to play with
|
23 |
iris = datasets.load_iris()
|
|
|
24 |
|
25 |
# we only take the first two features. We could
|
26 |
# avoid this ugly slicing by using a two-dim dataset
|
@@ -36,16 +38,21 @@ def plot(alpha):
|
|
36 |
y = y[idx]
|
37 |
|
38 |
# standardize
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
clf = SGDClassifier(alpha=alpha, max_iter=
|
44 |
-
|
|
|
|
|
|
|
45 |
DecisionBoundaryDisplay.from_estimator(
|
46 |
clf,
|
47 |
X,
|
48 |
-
cmap=
|
49 |
ax=ax,
|
50 |
response_method="predict",
|
51 |
xlabel=iris.feature_names[0],
|
@@ -80,20 +87,19 @@ def plot(alpha):
|
|
80 |
return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
|
81 |
|
82 |
plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)
|
83 |
-
|
|
|
84 |
for i, color in zip(clf.classes_, colors):
|
85 |
plot_hyperplane(i, color)
|
86 |
plt.legend()
|
87 |
-
|
88 |
-
return
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
demo.launch()
|
|
|
|
|
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 <[email protected]>
|
11 |
+
|
12 |
"""
|
13 |
+
|
14 |
import gradio as gr
|
15 |
import numpy as np
|
16 |
import matplotlib.pyplot as plt
|
|
|
19 |
from sklearn.inspection import DecisionBoundaryDisplay
|
20 |
import matplotlib.cm
|
21 |
|
22 |
+
def make_plot(alpha,max_iter,Standardize):
|
|
|
23 |
# import some data to play with
|
24 |
iris = datasets.load_iris()
|
25 |
+
fig = plt.figure()
|
26 |
|
27 |
# we only take the first two features. We could
|
28 |
# avoid this ugly slicing by using a two-dim dataset
|
|
|
38 |
y = y[idx]
|
39 |
|
40 |
# standardize
|
41 |
+
if Standardize:
|
42 |
+
mean = X.mean(axis=0)
|
43 |
+
std = X.std(axis=0)
|
44 |
+
X = (X - mean) / std
|
45 |
+
|
46 |
|
47 |
+
clf = SGDClassifier(alpha=alpha, max_iter=max_iter).fit(X, y)
|
48 |
+
accuracy = clf.score(X,y)
|
49 |
+
acc = f'## The Accuracy on the entire dataset: {accuracy}'
|
50 |
+
#fig,ax = subplots()
|
51 |
+
ax = plt.gca()
|
52 |
DecisionBoundaryDisplay.from_estimator(
|
53 |
clf,
|
54 |
X,
|
55 |
+
cmap=matplotlib.cm.Paired,
|
56 |
ax=ax,
|
57 |
response_method="predict",
|
58 |
xlabel=iris.feature_names[0],
|
|
|
87 |
return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
|
88 |
|
89 |
plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)
|
90 |
+
|
91 |
+
|
92 |
for i, color in zip(clf.classes_, colors):
|
93 |
plot_hyperplane(i, color)
|
94 |
plt.legend()
|
95 |
+
|
96 |
+
return fig,acc
|
97 |
+
|
98 |
+
demo = gr.Interface(
|
99 |
+
title = 'Plot multi-class SGD on the iris dataset',
|
100 |
+
fn = make_plot,
|
101 |
+
inputs = [gr.Slider(0.0001,5,step = 0.001,value = 0.001),
|
102 |
+
gr.Slider(1,1000,step=10,value=100),
|
103 |
+
gr.Checkbox(value=True)],
|
104 |
+
outputs = [gr.Plot(),gr.Markdown()]
|
105 |
+
).launch()
|
|
|
|