Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
Β·
ba335a6
1
Parent(s):
9f47792
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,26 +25,24 @@ The demo is based on the [scikit-learn docs](https://scikit-learn.org/stable/aut
|
|
| 25 |
def func(x):
|
| 26 |
return np.sin(2 * np.pi * x)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
x_test = np.linspace(0.0, 1.0, 100)
|
| 34 |
|
| 35 |
-
n_order = 3
|
| 36 |
-
X_train = np.vander(x_train, n_order + 1, increasing=True)
|
| 37 |
-
X_test = np.vander(x_test, n_order + 1, increasing=True)
|
| 38 |
-
reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
|
| 39 |
-
|
| 40 |
-
def curve_fit():
|
| 41 |
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
|
| 42 |
for i, ax in enumerate(axes):
|
| 43 |
# Bayesian ridge regression with different initial value pairs
|
| 44 |
if i == 0:
|
| 45 |
init = [1 / np.var(y_train), 1.0] # Default values
|
| 46 |
elif i == 1:
|
| 47 |
-
init = [
|
| 48 |
reg.set_params(alpha_init=init[0], lambda_init=init[1])
|
| 49 |
reg.fit(X_train, y_train)
|
| 50 |
ymean, ystd = reg.predict(X_test, return_std=True)
|
|
@@ -73,11 +71,15 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 73 |
<h1 style='text-align: center'>Curve Fitting with Bayesian Ridge Regression π</h1>
|
| 74 |
''')
|
| 75 |
gr.Markdown(description)
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Row():
|
| 78 |
run_button = gr.Button('Fit the Curve')
|
| 79 |
with gr.Row():
|
| 80 |
plot_result = gr.Plot()
|
| 81 |
-
run_button.click(fn=curve_fit, inputs=[], outputs=[plot_result])
|
| 82 |
|
| 83 |
demo.launch()
|
|
|
|
| 25 |
def func(x):
|
| 26 |
return np.sin(2 * np.pi * x)
|
| 27 |
|
| 28 |
+
def curve_fit(size, alpha, lam):
|
| 29 |
+
rng = np.random.RandomState(1234)
|
| 30 |
+
x_train = rng.uniform(0.0, 1.0, size)
|
| 31 |
+
y_train = func(x_train) + rng.normal(scale=0.1, size=size)
|
| 32 |
+
x_test = np.linspace(0.0, 1.0, 100)
|
| 33 |
|
| 34 |
+
n_order = 3
|
| 35 |
+
X_train = np.vander(x_train, n_order + 1, increasing=True)
|
| 36 |
+
X_test = np.vander(x_test, n_order + 1, increasing=True)
|
| 37 |
+
reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
|
| 40 |
for i, ax in enumerate(axes):
|
| 41 |
# Bayesian ridge regression with different initial value pairs
|
| 42 |
if i == 0:
|
| 43 |
init = [1 / np.var(y_train), 1.0] # Default values
|
| 44 |
elif i == 1:
|
| 45 |
+
init = [alpha, lam]
|
| 46 |
reg.set_params(alpha_init=init[0], lambda_init=init[1])
|
| 47 |
reg.fit(X_train, y_train)
|
| 48 |
ymean, ystd = reg.predict(X_test, return_std=True)
|
|
|
|
| 71 |
<h1 style='text-align: center'>Curve Fitting with Bayesian Ridge Regression π</h1>
|
| 72 |
''')
|
| 73 |
gr.Markdown(description)
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
size = gr.Slider(minimum=10, maximum=100, step=5, value=25, label="Number of Data Points")
|
| 77 |
+
alpha = gr.Slider(minimum=1e-2, maximum=2, step=0.1, value=1, label="Initial Alpha")
|
| 78 |
+
lam = gr.Slider(minimum=1e-5, maximum=1, step=1e-4, value=1e-3, label="Initial Lambda")
|
| 79 |
with gr.Row():
|
| 80 |
run_button = gr.Button('Fit the Curve')
|
| 81 |
with gr.Row():
|
| 82 |
plot_result = gr.Plot()
|
| 83 |
+
run_button.click(fn=curve_fit, inputs=[size, alpha, lam], outputs=[plot_result])
|
| 84 |
|
| 85 |
demo.launch()
|