nemos.glm.GLM.fit#

GLM.fit(X, y, init_params=None)[source]#

Fit GLM to neural activity.

Fit and store the model parameters as attributes coef_ and coef_.

Parameters:
  • X (Union[Array, FeaturePytree, ArrayLike]) – Predictors, array of shape (n_time_bins, n_features) or pytree of the same shape.

  • y (ArrayLike) – Target neural activity arranged in a matrix, shape (n_time_bins, ).

  • init_params (Optional[Tuple[Union[dict, ArrayLike], ArrayLike]]) – 2-tuple of initial parameter values: (coefficients, intercepts). If None, we initialize coefficients with zeros, intercepts with the log of the mean neural activity. coefficients is an array of shape (n_features,) or pytree of same, intercepts is an array of shape (1, )

Raises:
  • ValueError – If init_params is not of length two.

  • ValueError – If dimensionality of init_params are not correct.

  • ValueError – If X is not two-dimensional.

  • ValueError – If y is not one-dimensional.

  • ValueError – If solver returns at least one NaN parameter, which means it found an invalid solution. Try tuning optimization hyperparameters.

  • TypeError – If init_params are not array-like

  • TypeError – If init_params[i] cannot be converted to jnp.ndarray for all i

Examples

>>> # example input
>>> import numpy as np
>>> X, y = np.random.normal(size=(10, 2)), np.random.poisson(size=10)
>>> # fit a ridge regression Poisson GLM
>>> import nemos as nmo
>>> model = nmo.glm.GLM(regularizer="Ridge", regularizer_strength=0.1)
>>> model = model.fit(X, y)
>>> # get model weights and intercept
>>> model_weights = model.coef_
>>> model_intercept = model.intercept_