Somesh
commited on
Commit
Β·
5322e36
1
Parent(s):
da1c72b
adding all the files
Browse files- README.md +1 -1
- app.py +121 -0
- requirements.txt +6 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Kernel Ridge And Gaussian Process Regression
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Kernel Ridge And Gaussian Process Regression
|
3 |
+
emoji: π
|
4 |
colorFrom: indigo
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
from sklearn.linear_model import Ridge
|
7 |
+
from sklearn.gaussian_process.kernels import ExpSineSquared
|
8 |
+
from sklearn.kernel_ridge import KernelRidge
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
import random
|
12 |
+
|
13 |
+
|
14 |
+
def generate_data(n_samples: int) -> tuple:
|
15 |
+
rng = np.random.RandomState(random.randint(0, 1000))
|
16 |
+
data = np.linspace(0, 30, num=n_samples).reshape(-1, 1)
|
17 |
+
target = np.sin(data).ravel()
|
18 |
+
training_sample_indices = rng.choice(
|
19 |
+
np.arange(0, int(0.4 * n_samples)), size=int(0.2 * n_samples), replace=False
|
20 |
+
)
|
21 |
+
training_data = data[training_sample_indices]
|
22 |
+
training_noisy_target = (
|
23 |
+
target[training_sample_indices] + 0.5 * rng.randn(len(training_sample_indices))
|
24 |
+
)
|
25 |
+
|
26 |
+
return data, target, training_data, training_noisy_target
|
27 |
+
|
28 |
+
|
29 |
+
# def plot_ridge_and_kernel(n_samples: int) -> plt.figure:
|
30 |
+
|
31 |
+
# data, target, training_data, training_noisy_target = generate_data(n_samples)
|
32 |
+
|
33 |
+
# ridge = Ridge().fit(training_data, training_noisy_target)
|
34 |
+
# kernel_ridge = KernelRidge(kernel=ExpSineSquared())
|
35 |
+
# kernel_ridge.fit(training_data, training_noisy_target)
|
36 |
+
|
37 |
+
# fig, ax = plt.subplots(figsize=(8, 4))
|
38 |
+
|
39 |
+
# ax.plot(data, target, label="True signal", linewidth=2)
|
40 |
+
# ax.scatter(
|
41 |
+
# training_data,
|
42 |
+
# training_noisy_target,
|
43 |
+
# color="black",
|
44 |
+
# label="Noisy measurements",
|
45 |
+
# )
|
46 |
+
# ax.plot(data, ridge.predict(data), label="Ridge regression")
|
47 |
+
# ax.plot(
|
48 |
+
# data,
|
49 |
+
# kernel_ridge.predict(data),
|
50 |
+
# label="Kernel ridge",
|
51 |
+
# linewidth=2,
|
52 |
+
# linestyle="dashdot",
|
53 |
+
# )
|
54 |
+
# ax.legend()
|
55 |
+
# ax.set_xlabel("data")
|
56 |
+
# ax.set_ylabel("target")
|
57 |
+
# _ = ax.set_title("Ridge vs Kernel Ridge")
|
58 |
+
|
59 |
+
# return fig
|
60 |
+
|
61 |
+
def plot_ridge_and_kernel(n_samples: int) -> plt.figure:
|
62 |
+
data, target, training_data, training_noisy_target = generate_data(n_samples)
|
63 |
+
|
64 |
+
ridge = Ridge().fit(training_data, training_noisy_target)
|
65 |
+
kernel_ridge = KernelRidge(kernel=ExpSineSquared())
|
66 |
+
kernel_ridge.fit(training_data, training_noisy_target)
|
67 |
+
|
68 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
69 |
+
|
70 |
+
ridge_predictions = ridge.predict(data)
|
71 |
+
kernel_ridge_predictions = kernel_ridge.predict(data)
|
72 |
+
|
73 |
+
ax.plot(data, target, label="True signal", linewidth=2)
|
74 |
+
ax.scatter(
|
75 |
+
training_data,
|
76 |
+
training_noisy_target,
|
77 |
+
color="black",
|
78 |
+
label="Noisy measurements",
|
79 |
+
)
|
80 |
+
ax.plot(data, ridge_predictions, label="Ridge regression")
|
81 |
+
ax.plot(
|
82 |
+
data,
|
83 |
+
kernel_ridge_predictions,
|
84 |
+
label="Kernel ridge",
|
85 |
+
linewidth=2,
|
86 |
+
linestyle="dashdot",
|
87 |
+
)
|
88 |
+
ax.fill_between(
|
89 |
+
data.ravel(),
|
90 |
+
ridge_predictions,
|
91 |
+
kernel_ridge_predictions,
|
92 |
+
color="lightgrey",
|
93 |
+
alpha=0.4,
|
94 |
+
)
|
95 |
+
ax.legend()
|
96 |
+
ax.set_xlabel("data")
|
97 |
+
ax.set_ylabel("target")
|
98 |
+
_ = ax.set_title("Ridge vs Kernel Ridge with the area between highlighted")
|
99 |
+
|
100 |
+
return fig
|
101 |
+
|
102 |
+
|
103 |
+
def gradio_plot(n_samples: int) -> Image.Image:
|
104 |
+
fig = plot_ridge_and_kernel(n_samples)
|
105 |
+
buf = BytesIO()
|
106 |
+
fig.savefig(buf, format="png")
|
107 |
+
buf.seek(0)
|
108 |
+
im = Image.open(buf)
|
109 |
+
return im
|
110 |
+
|
111 |
+
|
112 |
+
inputs = [
|
113 |
+
gr.inputs.Slider(minimum=100, maximum=5000, step=100, label="n_samples", default=1000),
|
114 |
+
]
|
115 |
+
|
116 |
+
|
117 |
+
# Create the Gradio app
|
118 |
+
title = "Comparison of kernel ridge and Gaussian process regression"
|
119 |
+
description = "Kernel ridge regression and Gaussian process regression both use the kernel trick to fit data. While kernel ridge regression aims to find a single target function minimizing the loss (mean squared error), Gaussian process regression takes a probabilistic approach, defining a Gaussian posterior distribution over target functions using Bayes' theorem. Essentially, kernel ridge regression seeks one best function, while Gaussian process regression considers a range of probable functions based on prior probabilities and observed data. \n \n link to the official doc https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_compare_gpr_krr.html#sphx-glr-auto-examples-gaussian-process-plot-compare-gpr-krr-py"
|
120 |
+
iface = gr.Interface(fn=gradio_plot, inputs=inputs, outputs="image", title = title , description = description)
|
121 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.32.0
|
2 |
+
matplotlib==3.7.1
|
3 |
+
numpy==1.24.3
|
4 |
+
Pillow==9.5.0
|
5 |
+
Pillow==9.5.0
|
6 |
+
scikit_learn==1.2.2
|