Note
Click here to download the full example code
JMAPΒΆ
Out:
JMAP
/home/docs/checkouts/readthedocs.org/user_builds/sparsereg/checkouts/latest/sparsereg/model/bayes.py:49: UserWarning: Consider using sklearn.linear_model.BayesianRidge instead of JMAP.
f"Consider using sklearn.linear_model.BayesianRidge instead of {self.__class__.__name__}."
lambda 5.846788253508231e-07
alpha 15.959280652140635
coef [0.98553912 2.46879503] [0.00086711 0.0012283 ]
SKLEARN BayesianRidge
lambda 1.898742424623188e-07
alpha 95.90650799118377
coef [1.00020773 2.50007688] [0.0059014 0.0028405]
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sparsereg.model.bayes import JMAP
from sparsereg.model.bayes import scale_sigma
size = 10000
scale = 0.1
x = 3 * np.sort(np.random.normal(size=(size, 1)), axis=0)
y = x[:, 0] + 2.5 * x[:, 0] ** 2 + np.random.normal(scale=scale, size=size)
normalize = True
degree = 2
poly = PolynomialFeatures(degree=degree, include_bias=False)
xfeat = poly.fit_transform(x, y)
print("JMAP")
model = JMAP(normalize=normalize)
model.fit(xfeat, y)
# print("ve", model.ve_, model.ve_.shape)
# print("vf", model.vf_, np.sqrt(model.vf_))
print("lambda", model.lambda_)
print("alpha", model.alpha_)
print("coef", model.coef_, model.std_coef_)
from sklearn.linear_model import BayesianRidge
print("SKLEARN BayesianRidge")
model = BayesianRidge(normalize=normalize)
model.fit(xfeat, y)
print("lambda", model.lambda_)
print("alpha", model.alpha_)
print("coef", model.coef_, scale_sigma(model, model.X_offset_, model.X_scale_)[1])
Total running time of the script: ( 0 minutes 8.749 seconds)