frenkt commited on
Commit
448e358
1 Parent(s): 19d9bf9

Improve description and add axes labels

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -32,8 +32,8 @@ def get_app_fn():
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 = (
@@ -49,16 +49,18 @@ def get_app_fn():
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]
@@ -72,17 +74,26 @@ def get_app_fn():
72
 
73
  title = "Bayesian Ridge Regression"
74
  description = (
75
- "This example shows the effect of different initial values for the regularisation paramters (alpha, lambda)."
76
- "When starting from the default values (alpha_init = 1.90, lambda_init = 1.), the bias of the resulting curve is large, "
77
- "and the variance is small. So, lambda_init should be relatively small (1.e-3) to reduce the bias."
 
 
 
78
  )
79
  with gr.Blocks(title=title) as demo:
80
  gr.Markdown(f"## {title}")
81
  gr.Markdown(description)
82
-
83
- n_samples_input = gr.Slider(minimum=5, maximum=100, value=25, step=1, label="#observations")
84
- alpha_input = gr.Slider(minimum=0.001, maximum=5, value=1.9, step=0.01, label="alpha_init")
85
- lambda_input = gr.Slider(minimum=0.001, maximum=5, value=1., step=0.01, label="lambda_init")
 
 
 
 
 
 
86
  outputs = gr.Plot(label="Output")
87
  inputs = [n_samples_input, alpha_input, lambda_input]
88
  app_fn = get_app_fn()
 
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 = (
 
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="predicted mean")
53
  ax.fill_between(
54
  x_test,
55
  ymean - ystd,
56
  ymean + ystd,
57
  color="pink",
58
  alpha=0.5,
59
+ label="predicted std",
60
  )
61
  ax.set_ylim(-1.3, 1.3)
62
+ ax.set_xlabel("Cycles")
63
+ ax.set_ylabel("Amplitude")
64
  ax.legend()
65
  text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
66
  reg.alpha_, reg.lambda_, reg.scores_[-1]
 
74
 
75
  title = "Bayesian Ridge Regression"
76
  description = (
77
+ "This demo is based on the [Bayesian Ridge Regression](https://scikit-learn.org/stable/auto_examples/linear_model/plot_bayesian_ridge_curvefit.html#curve-fitting-with-bayesian-ridge-regression) "
78
+ "example from scikit-learn.\n"
79
+ "The example shows the effect of different initial values for the regularisation parameters `alpha` and `lambda`. "
80
+ "When starting from the default values (`alpha_init = 1.90`, `lambda_init = 1.`), the bias of the resulting curve is large, "
81
+ "and the variance is small. So, `lambda_init` should be relatively small (e.g. `1.e-3`) to reduce the bias.\n"
82
+ "By evaluating log marginal likelihood (`L`) of these models, we can determine which one is better. A model with larger `L` is more likely."
83
  )
84
  with gr.Blocks(title=title) as demo:
85
  gr.Markdown(f"## {title}")
86
  gr.Markdown(description)
87
+
88
+ n_samples_input = gr.Slider(
89
+ minimum=5, maximum=100, value=25, step=1, label="#observations"
90
+ )
91
+ alpha_input = gr.Slider(
92
+ minimum=0.001, maximum=5, value=1.9, step=0.01, label="alpha_init"
93
+ )
94
+ lambda_input = gr.Slider(
95
+ minimum=0.001, maximum=5, value=1.0, step=0.01, label="lambda_init"
96
+ )
97
  outputs = gr.Plot(label="Output")
98
  inputs = [n_samples_input, alpha_input, lambda_input]
99
  app_fn = get_app_fn()