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. The features to be considered are:\ \ 1. Sepal length (cm) \ 2. Sepal width (cm) \ The SVM Classifiers used for this demo are:\ \ 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()