Raaniel commited on
Commit
bcae252
1 Parent(s): cccda34

Upload 2 files

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