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