caliex commited on
Commit
3614731
·
1 Parent(s): cd42abc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ from sklearn import datasets
4
+ from sklearn.gaussian_process import GaussianProcessClassifier
5
+ from sklearn.gaussian_process.kernels import RBF
6
+ import gradio as gr
7
+
8
+ def plot_decision_boundary(kernel_type):
9
+ iris = datasets.load_iris()
10
+ X = iris.data[:, :2] # we only take the first two features.
11
+ y = np.array(iris.target, dtype=int)
12
+
13
+ h = 0.02 # step size in the mesh
14
+
15
+ if kernel_type == "isotropic":
16
+ kernel = 1.0 * RBF([1.0])
17
+ clf = GaussianProcessClassifier(kernel=kernel).fit(X, y)
18
+ elif kernel_type == "anisotropic":
19
+ kernel = 1.0 * RBF([1.0, 1.0])
20
+ clf = GaussianProcessClassifier(kernel=kernel).fit(X, y)
21
+ else:
22
+ return None
23
+
24
+ x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
25
+ y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
26
+ xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
27
+
28
+ Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])
29
+ Z = Z.reshape((xx.shape[0], xx.shape[1], 3))
30
+
31
+ plt.figure(figsize=(7, 5))
32
+ plt.imshow(Z, extent=(x_min, x_max, y_min, y_max), origin="lower")
33
+ plt.scatter(X[:, 0], X[:, 1], c=np.array(["r", "g", "b"])[y], edgecolors=(0, 0, 0))
34
+ plt.xlabel("Sepal length")
35
+ plt.ylabel("Sepal width")
36
+ plt.xlim(xx.min(), xx.max())
37
+ plt.ylim(yy.min(), yy.max())
38
+ plt.xticks(())
39
+ plt.yticks(())
40
+ plt.title("%s, LML: %.3f" % (kernel_type.capitalize(), clf.log_marginal_likelihood(clf.kernel_.theta)))
41
+ plt.tight_layout()
42
+ return plt
43
+
44
+ kernel_select = gr.inputs.Radio(["isotropic", "anisotropic"], label="Kernel Type")
45
+ gr_interface = gr.Interface(fn=plot_decision_boundary, inputs=kernel_select, outputs="plot", title="Gaussian Process Classification on Iris Dataset", description="This example illustrates the predicted probability of GPC for an isotropic and anisotropic RBF kernel on a two-dimensional version for the iris-dataset. The anisotropic RBF kernel obtains slightly higher log-marginal-likelihood by assigning different length-scales to the two feature dimensions. See the original example at https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpc_iris.html")
46
+ gr_interface.launch()