Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,145 +0,0 @@
|
|
1 |
-
# Code source: Gaël Varoquaux
|
2 |
-
# License: BSD 3 clause
|
3 |
-
|
4 |
-
import numpy as np
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
from sklearn import svm
|
7 |
-
import gradio as gr
|
8 |
-
plt.switch_backend("agg")
|
9 |
-
|
10 |
-
kernels = ["linear", "poly", "rbf"]
|
11 |
-
|
12 |
-
font1 = {'family':'DejaVu Sans','size':20}
|
13 |
-
|
14 |
-
cmaps = {'Set1': plt.cm.Set1, 'Set2': plt.cm.Set2, 'Set3': plt.cm.Set3,
|
15 |
-
'tab10': plt.cm.tab10, 'tab20': plt.cm.tab20}
|
16 |
-
|
17 |
-
# fit the model
|
18 |
-
def clf_kernel(kernel, cmap, dpi = 300, use_random = False):
|
19 |
-
|
20 |
-
#example data
|
21 |
-
if use_random == False:
|
22 |
-
X = np.c_[
|
23 |
-
(0.4, -0.7),
|
24 |
-
(-1.5, -1),
|
25 |
-
(-1.4, -0.9),
|
26 |
-
(-1.3, -1.2),
|
27 |
-
(-1.5, 0.2),
|
28 |
-
(-1.2, -0.4),
|
29 |
-
(-0.5, 1.2),
|
30 |
-
(-1.5, 2.1),
|
31 |
-
(1, 1),
|
32 |
-
# --
|
33 |
-
(1.3, 0.8),
|
34 |
-
(1.5, 0.5),
|
35 |
-
(0.2, -2),
|
36 |
-
(0.5, -2.4),
|
37 |
-
(0.2, -2.3),
|
38 |
-
(0, -2.7),
|
39 |
-
(1.3, 2.8),
|
40 |
-
].T
|
41 |
-
else:
|
42 |
-
#emulate some random data
|
43 |
-
x = np.random.uniform(-2, 2, size=(16, 1))
|
44 |
-
y = np.random.uniform(-2, 2, size=(16, 1))
|
45 |
-
X = np.hstack((x, y))
|
46 |
-
|
47 |
-
Y = [0] * 8 + [1] * 8
|
48 |
-
|
49 |
-
clf = svm.SVC(kernel=kernel, gamma=2)
|
50 |
-
clf.fit(X, Y)
|
51 |
-
|
52 |
-
# plot the line, the points, and the nearest vectors to the plane
|
53 |
-
fig= plt.figure(figsize=(10, 6), facecolor = 'none',
|
54 |
-
frameon = False, dpi = dpi)
|
55 |
-
ax = fig.add_subplot(111)
|
56 |
-
|
57 |
-
ax.scatter(
|
58 |
-
clf.support_vectors_[:, 0],
|
59 |
-
clf.support_vectors_[:, 1],
|
60 |
-
s=80,
|
61 |
-
facecolors="none",
|
62 |
-
zorder=10,
|
63 |
-
edgecolors="k",
|
64 |
-
)
|
65 |
-
ax.scatter(X[:, 0], X[:, 1], c=Y, zorder=10, cmap=cmap, edgecolors="k")
|
66 |
-
|
67 |
-
ax.axis("tight")
|
68 |
-
x_min = -3
|
69 |
-
x_max = 3
|
70 |
-
y_min = -3
|
71 |
-
y_max = 3
|
72 |
-
|
73 |
-
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
|
74 |
-
Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
|
75 |
-
|
76 |
-
# Put the result into a color plot
|
77 |
-
Z = Z.reshape(XX.shape)
|
78 |
-
ax.pcolormesh(XX, YY, Z > 0, cmap=cmap)
|
79 |
-
ax.contour(
|
80 |
-
XX,
|
81 |
-
YY,
|
82 |
-
Z,
|
83 |
-
colors=["k", "k", "k"],
|
84 |
-
linestyles=["--", "-", "--"],
|
85 |
-
levels=[-0.5, 0, 0.5],
|
86 |
-
)
|
87 |
-
|
88 |
-
ax.set_xlim(x_min, x_max)
|
89 |
-
ax.set_ylim(y_min, y_max)
|
90 |
-
|
91 |
-
ax.set_xticks(())
|
92 |
-
ax.set_yticks(())
|
93 |
-
ax.set_title('Type of kernel: ' + kernel,
|
94 |
-
color = "white", fontdict = font1, pad=20,
|
95 |
-
bbox=dict(boxstyle="round,pad=0.3",
|
96 |
-
color = "#6366F1"))
|
97 |
-
|
98 |
-
plt.close()
|
99 |
-
return fig
|
100 |
-
|
101 |
-
intro = """<h1 style="text-align: center;">🤗 Introducing SVM-Kernels 🤗</h1>
|
102 |
-
"""
|
103 |
-
desc = """<h3 style="text-align: center;">Three different types of SVM-Kernels are displayed below.
|
104 |
-
The polynomial and RBF are especially useful when the data-points are not linearly separable. </h3>
|
105 |
-
"""
|
106 |
-
notice = """<div style = "text-align: left;"> <em>Notice: Run the model on example data or press
|
107 |
-
<strong>Randomize data</strong> button to check out the model on emulated data-points.</em></div>"""
|
108 |
-
|
109 |
-
made ="""<div style="text-align: center;">
|
110 |
-
<p>Made with ❤</p>"""
|
111 |
-
|
112 |
-
link = """<div style="text-align: center;">
|
113 |
-
<a href="https://scikit-learn.org/stable/auto_examples/svm/plot_svm_kernels.html#sphx-glr-auto-examples-svm-plot-svm-kernels-py" target="_blank" rel="noopener noreferrer">
|
114 |
-
Demo is based on this script from scikit-learn documentation</a>"""
|
115 |
-
|
116 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo",
|
117 |
-
secondary_hue="violet",
|
118 |
-
neutral_hue="slate",
|
119 |
-
font = gr.themes.GoogleFont("Inter")),
|
120 |
-
title="SVM-Kernels") as demo:
|
121 |
-
|
122 |
-
gr.HTML(intro)
|
123 |
-
gr.HTML(desc)
|
124 |
-
with gr.Column():
|
125 |
-
kernel = gr.Radio(kernels, label="Select kernel:",
|
126 |
-
show_label = True, value = 'linear')
|
127 |
-
plot = gr.Plot(label="Plot")
|
128 |
-
|
129 |
-
with gr.Accordion(label = "More options", open = True):
|
130 |
-
cmap = gr.Radio(['Set1', 'Set2', 'Set3', 'tab10', 'tab20'], label="Choose color map: ", value = 'Set2')
|
131 |
-
dpi = gr.Slider(50, 150, value = 75, step = 1, label = "Set the resolution: ")
|
132 |
-
gr.HTML(notice)
|
133 |
-
random = gr.Button("Randomize data").style(full_width = False)
|
134 |
-
|
135 |
-
cmap.change(fn=clf_kernel, inputs=[kernel,cmap,dpi], outputs=plot)
|
136 |
-
dpi.change(fn=clf_kernel, inputs=[kernel,cmap,dpi], outputs=plot)
|
137 |
-
kernel.change(fn=clf_kernel, inputs=[kernel,cmap,dpi], outputs=plot)
|
138 |
-
|
139 |
-
random.click(fn=clf_kernel, inputs=[kernel,cmap,dpi,random], outputs=plot)
|
140 |
-
|
141 |
-
demo.load(fn=clf_kernel, inputs=[kernel,cmap,dpi], outputs=plot)
|
142 |
-
gr.HTML(made)
|
143 |
-
gr.HTML(link)
|
144 |
-
|
145 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|