RamAnanth1 commited on
Commit
b1c07bb
·
1 Parent(s): 2d5ccbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -21,7 +21,7 @@ This demo computes a Bayesian Ridge Regression of Sinusoids.
21
 
22
  In general, when fitting a curve with a polynomial by Bayesian ridge regression, the selection of initial values of the regularization parameters (alpha, lambda) may be important. This is because the regularization parameters are determined by an iterative procedure that depends on initial values.
23
 
24
- In this demo, the sinusoid is approximated by a cubic polynomial using different pairs of initial values.
25
 
26
  The demo is based on the [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/linear_model/plot_bayesian_ridge_curvefit.html#sphx-glr-auto-examples-linear-model-plot-bayesian-ridge-curvefit-py)
27
  """
@@ -40,37 +40,53 @@ def curve_fit(size, alpha, lam):
40
  X_test = np.vander(x_test, n_order + 1, increasing=True)
41
  reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
42
 
43
- fig, axes = plt.subplots(1, 2, figsize=(8, 4))
44
- results = []
45
- for i, ax in enumerate(axes):
46
  # Bayesian ridge regression with different initial value pairs
47
- if i == 0:
48
- init = [1 / np.var(y_train), 1.0] # Default values
49
- elif i == 1:
50
- init = [alpha, lam]
51
- reg.set_params(alpha_init=init[0], lambda_init=init[1])
52
- reg.fit(X_train, y_train)
53
- ymean, ystd = reg.predict(X_test, return_std=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- ax.plot(x_test, func(x_test), color="blue", label="sin($2\\pi x$)")
56
- ax.scatter(x_train, y_train, s=50, alpha=0.5, label="observation")
57
- ax.plot(x_test, ymean, color="red", label="predict mean")
58
- ax.fill_between(
59
- x_test, ymean - ystd, ymean + ystd, color="pink", alpha=0.5, label="predict std"
60
- )
61
- ax.set_ylim(-1.3, 1.3)
62
- ax.legend()
63
- title = "$\\alpha$_init$={:.2f},\\ \\lambda$_init$={}$".format(init[0], init[1])
64
- if i == 0:
65
- title += " (Default)"
66
- ax.set_title(title, fontsize=12)
67
- text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
68
- reg.alpha_, reg.lambda_, reg.scores_[-1]
69
- )
70
- ax.text(0.05, -1.0, text, fontsize=12)
71
- results.append(ax)
72
 
73
- return results[0], results[1]
74
 
75
 
76
  with gr.Blocks(theme=theme) as demo:
 
21
 
22
  In general, when fitting a curve with a polynomial by Bayesian ridge regression, the selection of initial values of the regularization parameters (alpha, lambda) may be important. This is because the regularization parameters are determined by an iterative procedure that depends on initial values.
23
 
24
+ In this demo, the sinusoid is approximated by a cubic polynomial using different pairs of initial values. By evaluating log marginal likelihood (L) of these models, we can determine which one is better. It can be concluded that the model with larger L is more likely.
25
 
26
  The demo is based on the [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/linear_model/plot_bayesian_ridge_curvefit.html#sphx-glr-auto-examples-linear-model-plot-bayesian-ridge-curvefit-py)
27
  """
 
40
  X_test = np.vander(x_test, n_order + 1, increasing=True)
41
  reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
42
 
43
+ fig1, ax1 = plt.subplots(1, 1, figsize=(8, 4))
44
+ fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4))
45
+
46
  # Bayesian ridge regression with different initial value pairs
47
+ init = [1 / np.var(y_train), 1.0] # Default values
48
+ reg.fit(X_train, y_train)
49
+ ymean, ystd = reg.predict(X_test, return_std=True)
50
+
51
+ ax1.plot(x_test, func(x_test), color="blue", label="sin($2\\pi x$)")
52
+ ax1.scatter(x_train, y_train, s=50, alpha=0.5, label="observation")
53
+ ax1.plot(x_test, ymean, color="red", label="predict mean")
54
+ ax1.fill_between(
55
+ x_test, ymean - ystd, ymean + ystd, color="pink", alpha=0.5, label="predict std"
56
+ )
57
+ ax1.set_ylim(-1.3, 1.3)
58
+ ax1.legend()
59
+ title = "$\\alpha$_init$={:.2f},\\ \\lambda$_init$={}$".format(init[0], init[1])
60
+ title += " (Default)"
61
+ ax1.set_title(title, fontsize=12)
62
+ text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
63
+ reg.alpha_, reg.lambda_, reg.scores_[-1]
64
+ )
65
+ ax1.text(0.05, -1.0, text, fontsize=12)
66
+
67
+ init = [alpha, lam]
68
+ reg.set_params(alpha_init=init[0], lambda_init=init[1])
69
+ reg.fit(X_train, y_train)
70
+ ymean, ystd = reg.predict(X_test, return_std=True)
71
+
72
+ ax2.plot(x_test, func(x_test), color="blue", label="sin($2\\pi x$)")
73
+ ax2.scatter(x_train, y_train, s=50, alpha=0.5, label="observation")
74
+ ax2.plot(x_test, ymean, color="red", label="predict mean")
75
+ ax2.fill_between(
76
+ x_test, ymean - ystd, ymean + ystd, color="pink", alpha=0.5, label="predict std"
77
+ )
78
+ ax2.set_ylim(-1.3, 1.3)
79
+ ax2.legend()
80
+ title = "$\\alpha$_init$={:.2f},\\ \\lambda$_init$={}$".format(init[0], init[1])
81
 
82
+ title += " (Chosen)"
83
+ ax2.set_title(title, fontsize=12)
84
+ text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
85
+ reg.alpha_, reg.lambda_, reg.scores_[-1]
86
+ )
87
+ ax2.text(0.05, -1.0, text, fontsize=12)
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ return fig1, fig2
90
 
91
 
92
  with gr.Blocks(theme=theme) as demo: