File size: 2,812 Bytes
679c425
da91a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f01a5e4
8d7f7f8
d4c2604
f1caf9a
8d7f7f8
4b33f7a
8d7f7f8
4b33f7a
8d7f7f8
d4c2604
f1caf9a
 
4b33f7a
8d7f7f8
4b33f7a
8d7f7f8
4b33f7a
8d7f7f8
4b33f7a
da91a58
 
 
 
 
 
 
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
import gradio as gr
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.inspection import DecisionBoundaryDisplay

def plot_svm_classifiers():
    # import some data to play with
    iris = datasets.load_iris()
    # Take the first two features. We could avoid this by using a two-dim dataset
    X = iris.data[:, :2]
    y = iris.target

    # we create an instance of SVM and fit out data. We do not scale our
    # data since we want to plot the support vectors
    C = 1.0  # SVM regularization parameter
    models = (
        svm.SVC(kernel="linear", C=C),
        svm.LinearSVC(C=C, max_iter=10000),
        svm.SVC(kernel="rbf", gamma=0.7, C=C),
        svm.SVC(kernel="poly", degree=3, gamma="auto", C=C),
    )
    models = (clf.fit(X, y) for clf in models)

    # title for the plots
    titles = (
        "SVC with linear kernel",
        "LinearSVC (linear kernel)",
        "SVC with RBF kernel",
        "SVC with polynomial (degree 3) kernel",
    )

    # Set-up 2x2 grid for plotting.
    fig, sub = plt.subplots(2, 2)
    plt.subplots_adjust(wspace=0.4, hspace=0.4)

    X0, X1 = X[:, 0], X[:, 1]

    for clf, title, ax in zip(models, titles, sub.flatten()):
        disp = DecisionBoundaryDisplay.from_estimator(
            clf,
            X,
            response_method="predict",
            cmap=plt.cm.coolwarm,
            alpha=0.8,
            ax=ax,
            xlabel=iris.feature_names[0],
            ylabel=iris.feature_names[1],
        )
        ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors="k")
        ax.set_xticks(())
        ax.set_yticks(())
        ax.set_title(title)
    plt.axis('tight')
    #plt.show()
    return fig 

heading = 'πŸ€—πŸ§‘πŸ€πŸ’™ Plot different SVM Classifiers on Iris Dataset'

with gr.Blocks(title = heading, theme= 'snehilsanyal/scikit-learn') as demo:
    gr.Markdown("# {}".format(heading))
    gr.Markdown(
        """
        ### This demo visualizes different SVM Classifiers on a 2D projection of the Iris dataset. 
        
        <b>The features to be considered are:</b>\
        \
        
        1. Sepal length (cm) \
        
        2. Sepal width (cm) \
        
        <b>The SVM Classifiers used for this demo are:</b>\
        \

        1. SVC with linear kernel \
        
        2. Linear SVC \
        
        3. SVC with RBF kernel\
        
        4. SVC with Polynomial (degree 3) kernel
        """
    )
    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)**')
    button = gr.Button(value = 'Visualize different SVM Classifiers on Iris Dataset')
    button.click(plot_svm_classifiers, outputs = gr.Plot())

demo.launch()