Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from sklearn import svm
|
4 |
+
import gradio as gr
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
def calculate_score(clf):
|
8 |
+
xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500))
|
9 |
+
X_test = np.c_[xx.ravel(), yy.ravel()]
|
10 |
+
Y_test = np.logical_xor(xx.ravel() > 0, yy.ravel() > 0)
|
11 |
+
return clf.score(X_test, Y_test)
|
12 |
+
|
13 |
+
def getColorMap(kernel, gamma):
|
14 |
+
# prepare the training dataset
|
15 |
+
np.random.seed(0)
|
16 |
+
X = np.random.randn(300, 2)
|
17 |
+
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
|
18 |
+
|
19 |
+
# fit the model
|
20 |
+
clf = svm.NuSVC(kernel=kernel, gamma=gamma)
|
21 |
+
clf.fit(X, Y)
|
22 |
+
|
23 |
+
#create a grid for the plotting the decision function
|
24 |
+
xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500))
|
25 |
+
|
26 |
+
# plot the decision function for each datapoint on the grid
|
27 |
+
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
28 |
+
Z = Z.reshape(xx.shape)
|
29 |
+
|
30 |
+
plt.imshow(
|
31 |
+
Z,
|
32 |
+
interpolation="nearest",
|
33 |
+
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
|
34 |
+
aspect="auto",
|
35 |
+
origin="lower",
|
36 |
+
cmap=plt.cm.PuOr_r,
|
37 |
+
)
|
38 |
+
contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2, linestyles="dashed")
|
39 |
+
plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors='k')
|
40 |
+
plt.title(f"Decision function for Non-Linear SVC with the {kernel} kernel and '{gamma}' gamma ", fontsize='14') #title
|
41 |
+
plt.xlabel("X",fontsize='13') #adds a label in the x axis
|
42 |
+
plt.ylabel("Y",fontsize='13') #adds a label in the y axis
|
43 |
+
return plt, calculate_score(clf)
|
44 |
+
|
45 |
+
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("## Learning the XOR function: An application of Binary Classification using Non-linear SVM")
|
48 |
+
gr.Markdown("This demo is based on this [scikit-learn example](https://scikit-learn.org/stable/auto_examples/svm/plot_svm_nonlinear.html#sphx-glr-auto-examples-svm-plot-svm-nonlinear-py).")
|
49 |
+
gr.Markdown("In this demo, we find the XOR of the inputs by learning the XOR function using Non-linear SVM.")
|
50 |
+
|
51 |
+
xor_image = Image.open("xor.png")
|
52 |
+
gr.Image(xor_image, label="Table explaining the 'XOR' operator")
|
53 |
+
|
54 |
+
gr.HTML("<hr>")
|
55 |
+
|
56 |
+
gr.Markdown("Furthermore, we observe that we get different decision function plots by varying the Kernel and Gamma hyperparameters the Non-Linear SVC.")
|
57 |
+
|
58 |
+
inp1 = gr.Radio(['poly', 'rbf', 'sigmoid'], label="Kernel", info="Choose a kernel")
|
59 |
+
inp2 = gr.Radio(['scale', 'auto'], label="Gamma", info="Choose a gamma value")
|
60 |
+
btn = gr.Button(value="Submit")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
plot = gr.Plot(label=f"Decision function plot for Non-Linear SVC with the '{inp1}' kernel and '{inp2}' gamma ")
|
64 |
+
num = gr.Textbox(label="Test Accuracy")
|
65 |
+
|
66 |
+
btn.click(getColorMap, inputs=[inp1, inp2], outputs=[plot, num])
|
67 |
+
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
print("hdh")
|
71 |
+
demo.launch()
|
72 |
+
print("gedhhfhf")
|