Created the demo
Browse files- app.py +83 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from sklearn.datasets import make_blobs
|
4 |
+
from sklearn.linear_model import LogisticRegression
|
5 |
+
from sklearn.inspection import DecisionBoundaryDisplay
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
import matplotlib
|
10 |
+
matplotlib.use('agg')
|
11 |
+
|
12 |
+
def create_dataset(num_samples):
|
13 |
+
# make 3-class dataset for classification
|
14 |
+
centers = [[-5, 0], [0, 1.5], [5, -1]]
|
15 |
+
X, y = make_blobs(n_samples=num_samples, centers=centers, random_state=42)
|
16 |
+
transformation = [[0.4, 0.2], [-0.4, 1.2]]
|
17 |
+
X = np.dot(X, transformation)
|
18 |
+
|
19 |
+
return X, y
|
20 |
+
|
21 |
+
def train_plot(multi_class, num_samples):
|
22 |
+
|
23 |
+
X, y = create_dataset(num_samples)
|
24 |
+
clf = LogisticRegression(
|
25 |
+
solver="sag", max_iter=100, random_state=42, multi_class=multi_class
|
26 |
+
).fit(X, y)
|
27 |
+
|
28 |
+
fig, ax = plt.subplots()
|
29 |
+
DecisionBoundaryDisplay.from_estimator(
|
30 |
+
clf, X, response_method="predict", cmap=plt.cm.Paired, ax=ax
|
31 |
+
)
|
32 |
+
plt.title("Decision surface of LogisticRegression (%s)" % multi_class)
|
33 |
+
plt.axis("tight")
|
34 |
+
|
35 |
+
colors = "bry"
|
36 |
+
for i, color in zip(clf.classes_, colors):
|
37 |
+
idx = np.where(y == i)
|
38 |
+
plt.scatter(
|
39 |
+
X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired, edgecolor="black", s=20
|
40 |
+
)
|
41 |
+
|
42 |
+
# Plot the three one-against-all classifiers
|
43 |
+
xmin, xmax = plt.xlim()
|
44 |
+
ymin, ymax = plt.ylim()
|
45 |
+
coef = clf.coef_
|
46 |
+
intercept = clf.intercept_
|
47 |
+
|
48 |
+
def plot_hyperplane(c, color):
|
49 |
+
def line(x0):
|
50 |
+
return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
|
51 |
+
|
52 |
+
plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)
|
53 |
+
|
54 |
+
for i, color in zip(clf.classes_, colors):
|
55 |
+
plot_hyperplane(i, color)
|
56 |
+
|
57 |
+
return fig, clf.score(X, y)
|
58 |
+
|
59 |
+
def plot_both(num_samples):
|
60 |
+
fig1, score1 = train_plot("multinomial", num_samples)
|
61 |
+
fig2, score2 = train_plot("ovr", num_samples)
|
62 |
+
|
63 |
+
return fig1, fig2, score1, score2
|
64 |
+
|
65 |
+
title = "Plot multinomial and One-vs-Rest Logistic Regression "
|
66 |
+
description = "Plot decision surface of multinomial and One-vs-Rest Logistic Regression. The hyperplanes corresponding to the three One-vs-Rest (OVR) classifiers are represented by the dashed lines."
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown(f"## {title}")
|
69 |
+
gr.Markdown(description)
|
70 |
+
|
71 |
+
num_samples = gr.Slider(minimum=500, maximum=2000, step=500, value=500, label="Number of samples")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
plot = gr.Plot()
|
75 |
+
plot2 = gr.Plot()
|
76 |
+
with gr.Row():
|
77 |
+
score1 = gr.Textbox(label="Multinomial score")
|
78 |
+
score2 = gr.Textbox(label="OVR score")
|
79 |
+
|
80 |
+
num_samples.change(fn=plot_both, inputs=[num_samples], outputs=[plot, plot2, score1, score2])
|
81 |
+
|
82 |
+
demo.launch(enable_queue=True)
|
83 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
scikit-learn
|
2 |
+
matplotlib
|
3 |
+
numpy
|