Upload 2 files
Browse files- app.py +88 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.linear_model import BayesianRidge
|
5 |
+
|
6 |
+
SEED = 1234
|
7 |
+
ORDER = 3
|
8 |
+
MAX_SAMPLES = 100
|
9 |
+
|
10 |
+
|
11 |
+
def sin_wave(x: np.array):
|
12 |
+
"""Sinusoidal wave function"""
|
13 |
+
return np.sin(2 * np.pi * x)
|
14 |
+
|
15 |
+
|
16 |
+
def generate_train_data(n_samples: int):
|
17 |
+
"""Generates sinuosidal data with noise"""
|
18 |
+
rng = np.random.RandomState(SEED)
|
19 |
+
x_train = rng.uniform(0.0, 1.0, n_samples)
|
20 |
+
y_train = sin_wave(x_train) + rng.normal(scale=0.1, size=n_samples)
|
21 |
+
X_train = np.vander(x_train, ORDER + 1, increasing=True)
|
22 |
+
return x_train, X_train, y_train
|
23 |
+
|
24 |
+
|
25 |
+
def get_app_fn():
|
26 |
+
"""Returns the demo function with pre-generated data and model"""
|
27 |
+
x_test = np.linspace(0.0, 1.0, 100)
|
28 |
+
X_test = np.vander(x_test, ORDER + 1, increasing=True)
|
29 |
+
y_test = sin_wave(x_test)
|
30 |
+
reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
|
31 |
+
x_train_full, X_train_full, y_train_full = generate_train_data(MAX_SAMPLES)
|
32 |
+
|
33 |
+
def app_fn(n_samples: int, alpha_init: float, lambda_init: float):
|
34 |
+
"""Train a Bayesian Ridge regression model and plot the predicted points"""
|
35 |
+
|
36 |
+
rng = np.random.RandomState(SEED)
|
37 |
+
subset_idx = rng.randint(0, MAX_SAMPLES, n_samples)
|
38 |
+
|
39 |
+
x_train, X_train, y_train = (
|
40 |
+
x_train_full[subset_idx],
|
41 |
+
X_train_full[subset_idx],
|
42 |
+
y_train_full[subset_idx],
|
43 |
+
)
|
44 |
+
reg.set_params(alpha_init=alpha_init, lambda_init=lambda_init)
|
45 |
+
reg.fit(X_train, y_train)
|
46 |
+
|
47 |
+
ymean, ystd = reg.predict(X_test, return_std=True)
|
48 |
+
|
49 |
+
fig, ax = plt.subplots()
|
50 |
+
ax.plot(x_test, y_test, color="blue", label="sin($2\\pi x$)")
|
51 |
+
ax.scatter(x_train, y_train, s=50, alpha=0.5, label="observation")
|
52 |
+
ax.plot(x_test, ymean, color="red", label="predict mean")
|
53 |
+
ax.fill_between(
|
54 |
+
x_test,
|
55 |
+
ymean - ystd,
|
56 |
+
ymean + ystd,
|
57 |
+
color="pink",
|
58 |
+
alpha=0.5,
|
59 |
+
label="predict std",
|
60 |
+
)
|
61 |
+
ax.set_ylim(-1.3, 1.3)
|
62 |
+
ax.legend()
|
63 |
+
text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
|
64 |
+
reg.alpha_, reg.lambda_, reg.scores_[-1]
|
65 |
+
)
|
66 |
+
ax.text(0.05, -1.0, text, fontsize=12)
|
67 |
+
|
68 |
+
return fig
|
69 |
+
|
70 |
+
return app_fn
|
71 |
+
|
72 |
+
|
73 |
+
title = "Bayesian Ridge Regression"
|
74 |
+
with gr.Blocks(title=title) as demo:
|
75 |
+
gr.Markdown(f"## {title}")
|
76 |
+
|
77 |
+
n_samples_input = gr.Slider(minimum=5, maximum=100, value=25, step=1, label="#observations")
|
78 |
+
alpha_input = gr.Slider(minimum=0.001, maximum=5, value=1.9, step=0.01, label="alpha_init")
|
79 |
+
lambda_input = gr.Slider(minimum=0.001, maximum=5, value=1., step=0.01, label="lambda_init")
|
80 |
+
outputs = gr.Plot(label="Output")
|
81 |
+
inputs = [n_samples_input, alpha_input, lambda_input]
|
82 |
+
app_fn = get_app_fn()
|
83 |
+
|
84 |
+
n_samples_input.change(fn=app_fn, inputs=inputs, outputs=outputs)
|
85 |
+
alpha_input.change(fn=app_fn, inputs=inputs, outputs=outputs)
|
86 |
+
lambda_input.change(fn=app_fn, inputs=inputs, outputs=outputs)
|
87 |
+
|
88 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
matplotlib==3.5.3
|
2 |
+
numpy==1.24.2
|
3 |
+
scikit_learn==1.2.2
|