Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
|
@@ -5,37 +16,30 @@ from sklearn import datasets
|
|
| 5 |
from sklearn.linear_model import SGDClassifier
|
| 6 |
from sklearn.inspection import DecisionBoundaryDisplay
|
| 7 |
|
| 8 |
-
def predict_class(x, y):
|
| 9 |
-
iris = datasets.load_iris()
|
| 10 |
-
X = iris.data[:, :2]
|
| 11 |
-
y = iris.target
|
| 12 |
-
colors = "bry"
|
| 13 |
-
idx = np.arange(X.shape[0])
|
| 14 |
-
np.random.seed(13)
|
| 15 |
-
np.random.shuffle(idx)
|
| 16 |
-
X = X[idx]
|
| 17 |
-
y = y[idx]
|
| 18 |
-
mean = X.mean(axis=0)
|
| 19 |
-
std = X.std(axis=0)
|
| 20 |
-
X = (X - mean) / std
|
| 21 |
-
clf = SGDClassifier(alpha=0.001, max_iter=100).fit(X, y)
|
| 22 |
-
predicted_class = clf.predict(np.array([[x, y]]))[0]
|
| 23 |
-
return iris.target_names[predicted_class]
|
| 24 |
|
| 25 |
-
def
|
|
|
|
| 26 |
iris = datasets.load_iris()
|
|
|
|
|
|
|
|
|
|
| 27 |
X = iris.data[:, :2]
|
| 28 |
y = iris.target
|
| 29 |
colors = "bry"
|
|
|
|
|
|
|
| 30 |
idx = np.arange(X.shape[0])
|
| 31 |
np.random.seed(13)
|
| 32 |
np.random.shuffle(idx)
|
| 33 |
X = X[idx]
|
| 34 |
y = y[idx]
|
|
|
|
|
|
|
| 35 |
mean = X.mean(axis=0)
|
| 36 |
std = X.std(axis=0)
|
| 37 |
X = (X - mean) / std
|
| 38 |
-
|
|
|
|
| 39 |
ax = plt.gca()
|
| 40 |
DecisionBoundaryDisplay.from_estimator(
|
| 41 |
clf,
|
|
@@ -46,54 +50,49 @@ def decision_boundary(x_min, x_max, y_min, y_max):
|
|
| 46 |
xlabel=iris.feature_names[0],
|
| 47 |
ylabel=iris.feature_names[1],
|
| 48 |
)
|
| 49 |
-
plt.axis(
|
| 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 |
-
layout="vertical",
|
| 96 |
-
allow_flagging=False,
|
| 97 |
-
live=True,
|
| 98 |
-
outputs=[None, decision_boundary],
|
| 99 |
-
).launch()
|
|
|
|
| 1 |
+
|
| 2 |
+
"""
|
| 3 |
+
========================================
|
| 4 |
+
Plot multi-class SGD on the iris dataset
|
| 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
|
|
|
|
| 16 |
from sklearn.linear_model import SGDClassifier
|
| 17 |
from sklearn.inspection import DecisionBoundaryDisplay
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def plot(alpha):
|
| 21 |
+
# import some data to play with
|
| 22 |
iris = datasets.load_iris()
|
| 23 |
+
|
| 24 |
+
# we only take the first two features. We could
|
| 25 |
+
# avoid this ugly slicing by using a two-dim dataset
|
| 26 |
X = iris.data[:, :2]
|
| 27 |
y = iris.target
|
| 28 |
colors = "bry"
|
| 29 |
+
|
| 30 |
+
# shuffle
|
| 31 |
idx = np.arange(X.shape[0])
|
| 32 |
np.random.seed(13)
|
| 33 |
np.random.shuffle(idx)
|
| 34 |
X = X[idx]
|
| 35 |
y = y[idx]
|
| 36 |
+
|
| 37 |
+
# standardize
|
| 38 |
mean = X.mean(axis=0)
|
| 39 |
std = X.std(axis=0)
|
| 40 |
X = (X - mean) / std
|
| 41 |
+
|
| 42 |
+
clf = SGDClassifier(alpha=alpha, max_iter=100).fit(X, y)
|
| 43 |
ax = plt.gca()
|
| 44 |
DecisionBoundaryDisplay.from_estimator(
|
| 45 |
clf,
|
|
|
|
| 50 |
xlabel=iris.feature_names[0],
|
| 51 |
ylabel=iris.feature_names[1],
|
| 52 |
)
|
| 53 |
+
plt.axis("tight")
|
| 54 |
+
|
| 55 |
+
# Plot also the training points
|
| 56 |
+
for i, color in zip(clf.classes_, colors):
|
| 57 |
+
idx = np.where(y == i)
|
| 58 |
+
plt.scatter(
|
| 59 |
+
X[idx, 0],
|
| 60 |
+
X[idx, 1],
|
| 61 |
+
c=color,
|
| 62 |
+
label=iris.target_names[i],
|
| 63 |
+
cmap=plt.cm.Paired,
|
| 64 |
+
edgecolor="black",
|
| 65 |
+
s=20,
|
| 66 |
+
)
|
| 67 |
+
plt.title("Decision surface of multi-class SGD")
|
| 68 |
+
plt.axis("tight")
|
| 69 |
+
|
| 70 |
+
# Plot the three one-against-all classifiers
|
| 71 |
+
xmin, xmax = plt.xlim()
|
| 72 |
+
ymin, ymax = plt.ylim()
|
| 73 |
+
coef = clf.coef_
|
| 74 |
+
intercept = clf.intercept_
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def plot_hyperplane(c, color):
|
| 78 |
+
def line(x0):
|
| 79 |
+
return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
|
| 80 |
+
|
| 81 |
+
plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)
|
| 82 |
+
|
| 83 |
+
for i, color in zip(clf.classes_, colors):
|
| 84 |
+
plot_hyperplane(i, color)
|
| 85 |
+
plt.legend()
|
| 86 |
+
#plt.show()
|
| 87 |
+
return plt
|
| 88 |
+
|
| 89 |
+
#pl = plot(0)
|
| 90 |
+
#pl.show()
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
+
alpha = gr.Slider(minimum=0.0001, maximum=10, step=0.01, value=0.0001, label="Alpha Value")
|
| 93 |
+
with gr.Row():
|
| 94 |
+
plt = gr.Plot()
|
| 95 |
+
|
| 96 |
+
alpha.change(fn=plot, inputs=[alpha],outputs=[plt])
|
| 97 |
+
|
| 98 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|