Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
from sklearn.gaussian_process import GaussianProcessClassifier
|
| 7 |
+
from sklearn.gaussian_process.kernels import RBF, DotProduct
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def classify_xor_dataset(kernel_name):
|
| 11 |
+
xx, yy = np.meshgrid(np.linspace(-3, 3, 50), np.linspace(-3, 3, 50))
|
| 12 |
+
rng = np.random.RandomState(0)
|
| 13 |
+
X = rng.randn(200, 2)
|
| 14 |
+
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
|
| 15 |
+
|
| 16 |
+
# fit the model
|
| 17 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 18 |
+
kernels = [1.0 * RBF(length_scale=1.15), 1.0 * DotProduct(sigma_0=1.0) ** 2]
|
| 19 |
+
kernel_idx = 0 if kernel_name == "RBF" else 1
|
| 20 |
+
kernel = kernels[kernel_idx]
|
| 21 |
+
clf = GaussianProcessClassifier(kernel=kernel, warm_start=True).fit(X, Y)
|
| 22 |
+
|
| 23 |
+
# plot the decision function for each datapoint on the grid
|
| 24 |
+
Z = clf.predict_proba(np.vstack((xx.ravel(), yy.ravel())).T)[:, 1]
|
| 25 |
+
Z = Z.reshape(xx.shape)
|
| 26 |
+
|
| 27 |
+
ax.imshow(
|
| 28 |
+
Z,
|
| 29 |
+
interpolation="nearest",
|
| 30 |
+
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
|
| 31 |
+
aspect="auto",
|
| 32 |
+
origin="lower",
|
| 33 |
+
cmap=plt.cm.PuOr_r,
|
| 34 |
+
)
|
| 35 |
+
ax.contour(xx, yy, Z, levels=[0.5], linewidths=2, colors=["k"])
|
| 36 |
+
ax.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors=(0, 0, 0))
|
| 37 |
+
ax.set_xticks(())
|
| 38 |
+
ax.set_yticks(())
|
| 39 |
+
ax.axis([-3, 3, -3, 3])
|
| 40 |
+
ax.set_title(
|
| 41 |
+
"%s\n Log-Marginal-Likelihood:%.3f"
|
| 42 |
+
% (clf.kernel_, clf.log_marginal_likelihood(clf.kernel_.theta)),
|
| 43 |
+
fontsize=12,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
fig.canvas.draw()
|
| 47 |
+
pil_image = Image.frombytes("RGB", fig.canvas.get_width_height(), fig.canvas.tostring_rgb())
|
| 48 |
+
plt.close(fig)
|
| 49 |
+
return pil_image
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
title = "Gaussian Process Classification on the XOR Dataset"
|
| 53 |
+
description = "This example illustrates GPC on XOR data. Compared are a stationary, isotropic kernel (RBF) and a non-stationary kernel (DotProduct). On this particular dataset, the DotProduct kernel obtains considerably better results because the class-boundaries are linear and coincide with the coordinate axes. In general, stationary kernels often obtain better results. See the original scikit-learn example at https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpc_xor.html"
|
| 54 |
+
kernel_options = ["RBF", "DotProduct"]
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
classify_xor_dataset,
|
| 57 |
+
gr.inputs.Radio(choices=kernel_options, label="Kernel"),
|
| 58 |
+
gr.outputs.Image(label="Decision Boundary", type="pil"),
|
| 59 |
+
title=title,
|
| 60 |
+
description=description,
|
| 61 |
+
theme="default",
|
| 62 |
+
layout="vertical",
|
| 63 |
+
analytics_enabled=False,
|
| 64 |
+
examples=[
|
| 65 |
+
["RBF"],
|
| 66 |
+
["DotProduct"],
|
| 67 |
+
],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
iface.launch()
|