linear_regression#
- class braintools.optim.linear_regression(X, Y, *, fit_intercept=True)#
Fit an ordinary least-squares linear regression.
Minimises \(\|Y - X W - b\|_F^2\) with
jax.numpy.linalg.lstsq(). WhenXis rank deficient (for example more features than samples, or a constant feature withfit_intercept=True) the minimum-norm solution is returned, so constant features get a zero coefficient.- Parameters:
X (array_like or brainunit.Quantity) – Design matrix, shape
(n_samples, n_features).Y (array_like or brainunit.Quantity) – Targets, shape
(n_samples,)or(n_samples, n_targets).fit_intercept (
bool) – Fit an unpenalised intercept by centringXandYfirst.
- Returns:
coefof shape(n_features,)or(n_features, n_targets)(in the unit ofYover the unit ofX) andintercept(in the unit ofY).- Return type:
- Raises:
ValueError – If
Xis not 2-D,Yis not 1-D or 2-D, or the sample counts differ.
See also
ridge_regressionL2-penalised least squares.
RLSthe same solution computed online, one batch at a time.
Notes
Matches
sklearn.linear_model.LinearRegression. The function is pure and supportsjax.jit,jax.vmapandjax.grad(gradients with respect toXrequire distinct non-zero singular values of the centredX; gradients with respect toYalways exist).Examples
>>> import jax.numpy as jnp >>> import braintools >>> X = jnp.array([[0.], [1.], [2.], [3.]]) >>> Y = 2. * X[:, 0] + 1. >>> res = braintools.optim.linear_regression(X, Y) >>> print(jnp.round(res.coef, 4), round(float(res.intercept), 4)) [2.] 1.0