snehilsanyal commited on
Commit
da91a58
Β·
1 Parent(s): 65a77d4

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ from sklearn import svm, datasets
3
+ from sklearn.inspection import DecisionBoundaryDisplay
4
+
5
+ def plot_svm_classifiers():
6
+ # import some data to play with
7
+ iris = datasets.load_iris()
8
+ # Take the first two features. We could avoid this by using a two-dim dataset
9
+ X = iris.data[:, :2]
10
+ y = iris.target
11
+
12
+ # we create an instance of SVM and fit out data. We do not scale our
13
+ # data since we want to plot the support vectors
14
+ C = 1.0 # SVM regularization parameter
15
+ models = (
16
+ svm.SVC(kernel="linear", C=C),
17
+ svm.LinearSVC(C=C, max_iter=10000),
18
+ svm.SVC(kernel="rbf", gamma=0.7, C=C),
19
+ svm.SVC(kernel="poly", degree=3, gamma="auto", C=C),
20
+ )
21
+ models = (clf.fit(X, y) for clf in models)
22
+
23
+ # title for the plots
24
+ titles = (
25
+ "SVC with linear kernel",
26
+ "LinearSVC (linear kernel)",
27
+ "SVC with RBF kernel",
28
+ "SVC with polynomial (degree 3) kernel",
29
+ )
30
+
31
+ # Set-up 2x2 grid for plotting.
32
+ fig, sub = plt.subplots(2, 2)
33
+ plt.subplots_adjust(wspace=0.4, hspace=0.4)
34
+
35
+ X0, X1 = X[:, 0], X[:, 1]
36
+
37
+ for clf, title, ax in zip(models, titles, sub.flatten()):
38
+ disp = DecisionBoundaryDisplay.from_estimator(
39
+ clf,
40
+ X,
41
+ response_method="predict",
42
+ cmap=plt.cm.coolwarm,
43
+ alpha=0.8,
44
+ ax=ax,
45
+ xlabel=iris.feature_names[0],
46
+ ylabel=iris.feature_names[1],
47
+ )
48
+ ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors="k")
49
+ ax.set_xticks(())
50
+ ax.set_yticks(())
51
+ ax.set_title(title)
52
+ plt.axis('tight')
53
+ #plt.show()
54
+ return fig
55
+
56
+ heading = 'πŸ€—πŸ§‘πŸ€πŸ’™ Plot different SVM Classifiers on Iris Dataset'
57
+
58
+ with gr.Blocks(title = heading, theme= 'snehilsanyal/scikit-learn') as demo:
59
+ gr.Markdown("# {}".format(heading))
60
+ gr.Markdown(
61
+ """
62
+ ### This demo visualizes different SVM Classifiers on a 2D projection
63
+ of the Iris dataset. The features to be considered are:
64
+ <b>1. Sepal length </b>
65
+ <b>2. Sepal width </b>
66
+ The SVM Classifiers used for this demo are:
67
+ <b>1. SVC with linear kernel </b>
68
+ <b>2. Linear SVC </b>
69
+ <b>3. SVC with RBF kernel</b>
70
+ <b>4. SVC with Polynomial (degree 3) kernel</b>
71
+ """
72
+ )
73
+ gr.Markdown('**[Demo is based on this script from scikit-learn documentation](https://scikit-learn.org/stable/auto_examples/svm/plot_iris_svc.html#sphx-glr-auto-examples-svm-plot-iris-svc-py)**')
74
+ button = gr.Button(value = 'Visualize different SVM Classifiers on Iris Dataset')
75
+ button.click(plot_svm_classifiers, outputs = gr.Plot())
76
+
77
+ demo.launch()