cleverly.learners.GLM#

class cleverly.learners.GLM(family: Literal['auto', 'binomial', 'multinomial', 'gaussian'] = 'auto', interactions: int = 1, max_iter: int = 100, tol: float = 1e-10)[source]#

An unpenalized generalized linear model with a canonical link.

Parameters:
family{“auto”, “binomial”, “multinomial”, “gaussian”}

The model family. "auto" chooses from the target, as the module docstring states.

interactionsint

The highest order of the products of distinct columns in the design. 1 is main terms, as R’s default glm formula y ~ ..

max_iterint

The most Newton steps of a binomial or multinomial fit.

tolfloat

The convergence tolerance on the largest Newton step, relative to one plus the largest coefficient.

Attributes:
family_str

The fitted family.

classes_numpy.ndarray

The labels of a binomial fit on labels or of a multinomial fit, in sorted order. Absent on a fit of a float target.

active_numpy.ndarray

The boolean mask of design columns that are not aliased.

alias_numpy.ndarray

(p_active, p_aliased): each aliased column as a combination of the active columns on the fit rows. outside_span() reads it.

coef_numpy.ndarray

The coefficients of the active columns. (p,) for binomial and gaussian, and (K - 1, p) for multinomial, one row per non-reference label.

n_iter_int

The Newton steps the fit took. Zero for gaussian.

Examples

>>> import numpy as np
>>> from cleverly.learners import GLM
>>> x = np.array([[0.0], [0.0], [1.0], [1.0]])
>>> model = GLM().fit(x, np.array([0.2, 0.4, 0.6, 0.8]))
>>> model.family_
'binomial'
>>> np.round(model.predict(np.array([[0.0], [1.0]])), 6)
array([0.3, 0.7])

Methods#

GLM.design(x: NDArray[float64]) → NDArray[float64][source]#

The design of the active columns, the one the score and the information read.

Parameters:
xnumpy.ndarray

The (n, d) covariates.

Returns:
numpy.ndarray

The (n, p) active design.

GLM.fit(x: NDArray[float64], y: NDArray[float64], sample_weight: NDArray[float64] | None = None) → GLM[source]#

Fit the model by maximum likelihood.

Parameters:
xnumpy.ndarray

The (n, d) covariates.

ynumpy.ndarray

The target.

sample_weightnumpy.ndarray or None

Nonnegative observation weights. None weights every row one.

Returns:
GLM

The fitted model.

Raises:
DataError

When the fit has no row with positive weight, when a binomial target leaves [0, 1], or when the Newton iteration does not converge or reaches separation.

GLM.full_design(x: NDArray[float64]) → NDArray[float64][source]#

The design before aliasing: an intercept, the columns, and their products.

Parameters:
xnumpy.ndarray

The (n, d) covariates.

Returns:
numpy.ndarray

The (n, P) design.

GLM.information(x: NDArray[float64], sample_weight: NDArray[float64] | None = None) → NDArray[float64][source]#

The negative derivative of the summed score, (n_parameters, n_parameters).

Parameters:
xnumpy.ndarray

The (n, d) covariates of the fit rows.

sample_weightnumpy.ndarray or None

The weights; zero on rows the fit did not read.

Returns:
numpy.ndarray

The information matrix (not divided by n).

GLM.linear_predictor(x: NDArray[float64]) → NDArray[float64][source]#

The linear predictor: (n,), or (n, K) logits for multinomial.

Parameters:
xnumpy.ndarray

The (n, d) covariates.

Returns:
numpy.ndarray

The linear predictor.

GLM.mean_jacobian(x: NDArray[float64]) → NDArray[float64][source]#

The derivative of the fitted mean in the coefficients.

For binomial and gaussian it is \(\mu'(\eta)\, x\), an (n, p) array. For multinomial it is the (n, K, (K - 1) p) derivative of each label’s probability, in the order of coef_.reshape(-1).

Parameters:
xnumpy.ndarray

The (n, d) covariates.

Returns:
numpy.ndarray

The Jacobian.

GLM.outside_span(x: NDArray[float64]) → NDArray[bool][source]#

Whether each row leaves the span the fit rows give the aliased columns.

An aliased column has no coefficient of its own. On the fit rows it is a combination of the active columns, so a prediction there is determined. A row where the column departs from that combination is predicted as if the column had no effect, which the data cannot support.

Parameters:
xFloatArray

The design rows to check, before the intercept and products.

Returns:
BoolArray

True on each row that leaves the span.

GLM.predict(x: NDArray[float64]) → NDArray[float64][source]#

The fitted mean: the probability of the last label for binomial.

Parameters:
xnumpy.ndarray

The (n, d) covariates.

Returns:
numpy.ndarray

The (n,) mean. A multinomial fit returns the most probable label.

GLM.predict_proba(x: NDArray[float64]) → NDArray[float64][source]#

The label probabilities, one column per label of classes_.

Parameters:
xnumpy.ndarray

The (n, d) covariates.

Returns:
numpy.ndarray

The (n, K) probabilities.

GLM.score_contributions(x: NDArray[float64], y: NDArray[float64], sample_weight: NDArray[float64] | None = None) → NDArray[float64][source]#

Each row’s weighted score at the fitted coefficients, (n, n_parameters).

Parameters:
xnumpy.ndarray

The (n, d) covariates.

ynumpy.ndarray

The target, coded as at the fit.

sample_weightnumpy.ndarray or None

The weights; a row the fit did not read takes weight zero.

Returns:
numpy.ndarray

The score rows. Their sum is zero on the fit rows, to the fit’s tolerance.

GLM.set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$', x: bool | None | str = '$UNCHANGED$') → GLM#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in fit.

Returns:
selfobject

The updated object.

GLM.set_predict_proba_request(*, x: bool | None | str = '$UNCHANGED$') → GLM#

Configure whether metadata should be requested to be passed to the predict_proba method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_proba.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in predict_proba.

Returns:
selfobject

The updated object.

GLM.set_predict_request(*, x: bool | None | str = '$UNCHANGED$') → GLM#

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in predict.

Returns:
selfobject

The updated object.

Attributes#

GLM.frequency_exact = True#

An unpenalized maximum-likelihood fit started from zero: a row of integer weight c fits as c copies of the row (cleverly.longitudinal.frequency).

GLM.n_parameters#

The count of free coefficients.