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.- interactions
int The highest order of the products of distinct columns in the design.
1is main terms, as R’s defaultglmformulay ~ ..- max_iter
int The most Newton steps of a binomial or multinomial fit.
- tol
float 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
binomialfit on labels or of amultinomialfit, 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,)forbinomialandgaussian, and(K - 1, p)formultinomial, one row per non-reference label.- n_iter_
int The Newton steps the fit took. Zero for
gaussian.
- family_
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:
- x
numpy.ndarray The
(n, d)covariates.
- x
- Returns:
numpy.ndarrayThe
(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:
- x
numpy.ndarray The
(n, d)covariates.- y
numpy.ndarray The target.
- sample_weight
numpy.ndarrayorNone Nonnegative observation weights.
Noneweights every row one.
- x
- Returns:
GLMThe fitted model.
- Raises:
DataErrorWhen 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:
- x
numpy.ndarray The
(n, d)covariates.
- x
- Returns:
numpy.ndarrayThe
(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:
- x
numpy.ndarray The
(n, d)covariates of the fit rows.- sample_weight
numpy.ndarrayorNone The weights; zero on rows the fit did not read.
- x
- Returns:
numpy.ndarrayThe information matrix (not divided by n).
- GLM.linear_predictor(x: NDArray[float64]) NDArray[float64][source]#
The linear predictor:
(n,), or(n, K)logits formultinomial.- Parameters:
- x
numpy.ndarray The
(n, d)covariates.
- x
- Returns:
numpy.ndarrayThe linear predictor.
- GLM.mean_jacobian(x: NDArray[float64]) NDArray[float64][source]#
The derivative of the fitted mean in the coefficients.
For
binomialandgaussianit is \(\mu'(\eta)\, x\), an(n, p)array. Formultinomialit is the(n, K, (K - 1) p)derivative of each label’s probability, in the order ofcoef_.reshape(-1).- Parameters:
- x
numpy.ndarray The
(n, d)covariates.
- x
- Returns:
numpy.ndarrayThe 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:
- x
FloatArray The design rows to check, before the intercept and products.
- x
- Returns:
BoolArrayTrueon 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:
- x
numpy.ndarray The
(n, d)covariates.
- x
- Returns:
numpy.ndarrayThe
(n,)mean. Amultinomialfit returns the most probable label.
- GLM.predict_proba(x: NDArray[float64]) NDArray[float64][source]#
The label probabilities, one column per label of
classes_.- Parameters:
- x
numpy.ndarray The
(n, d)covariates.
- x
- Returns:
numpy.ndarrayThe
(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:
- x
numpy.ndarray The
(n, d)covariates.- y
numpy.ndarray The target, coded as at the fit.
- sample_weight
numpy.ndarrayorNone The weights; a row the fit did not read takes weight zero.
- x
- Returns:
numpy.ndarrayThe 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
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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:
- Returns:
- self
object The updated object.
- self
- GLM.set_predict_proba_request(*, x: bool | None | str = '$UNCHANGED$') GLM#
Configure whether metadata should be requested to be passed to the
predict_probamethod.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(seesklearn.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 topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_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.
- GLM.set_predict_request(*, x: bool | None | str = '$UNCHANGED$') GLM#
Configure whether metadata should be requested to be passed to the
predictmethod.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(seesklearn.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 topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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.
Attributes#
- GLM.frequency_exact = True#
An unpenalized maximum-likelihood fit started from zero: a row of integer weight
cfits asccopies of the row (cleverly.longitudinal.frequency).
- GLM.n_parameters#
The count of free coefficients.