Basic Usage¶
The following simple example shows out-of-the-box functionality of BSR.
# Uncomment the following line when running on Google Colab
# !pip install "autora[theorist-bsr]"
After importing the necessary modules,
# imports
from autora.theorist.bsr import BSRRegressor
import numpy as np
import matplotlib.pyplot as plt
we begin by generating data with a ground-truth equation, $y = \sin(x) + x^3$.
x = np.expand_dims(np.linspace(start=-1, stop=1, num=500), 1)
y = np.power(x, 3) + np.sin(x)
Then we set up the BSR regressor with our chosen meta-parameters. In this case, we will keep the defaults but choose a small number of iterations, itr_num
, for ease and efficiency of illustration.
# initialize regressor
bsr = BSRRegressor(itr_num = 500)
With our regressor initialized, we can call the fit
method to discover an equation for our data and then use the predict
method to generate predictions using our discovered equation.
bsr.fit(x, y)
y_pred = bsr.predict(x)
Finally, we can plot the results.
# plot out the ground truth versus predicted responses
plt.figure()
plt.plot(x, y, "o")
plt.plot(x, y_pred, "-")
plt.show()
In this simple case, the algorithm provides results that are quite good, even with a small number of iterations.
References¶
Jin, Ying, et al. "Bayesian symbolic regression." arXiv preprint arXiv:1910.08892 (2019).