cross_validate#
- superglm.cross_validate(
- model,
- X: object,
- y: NDArray,
- *,
- cv,
- sample_weight: NDArray | None = None,
- offset: NDArray | Mapping[str, NDArray] | None = None,
- groups: NDArray | None = None,
- fit_mode: str | None = None,
- scoring: str | Callable | Sequence[str | Callable] | None = None,
- return_estimators: bool = False,
- return_oof: bool = False,
- error_score: float | str = nan,
- fit_kwargs: Mapping[str, Any] | None = None,
- warm_start: bool | None = None,
Cross-validate a SuperGLM or SuperLSS model with a pluggable splitter.
- Parameters:
- modelSuperGLM or SuperLSS
An unfitted (or fitted) model. A fresh clone is created for each fold; the input model is never mutated.
- Xpandas or eager Polars DataFrame
Feature matrix.
- yarray-like
Response variable.
- cvsplitter
Object with a
.split(X, y, groups)method yielding(train_idx, test_idx)tuples. Any sklearn splitter works.- sample_weightarray-like, optional
Sliced per fold; splitters operate on the physical compact rows, and they are read under the model’s declared
weight_semantics. Under"frequency", integer values are likelihood-equivalent to literal row replication within a fixed train/validation partition and fixed feature geometry. Under"prior"they state a precision,Var(Y_i) = phi * V(mu_i) / w_i; a Tweedie fit additionally requires them finite and strictly positive.- offsetarray-like, optional
SuperGLM offset term, sliced per fold. For SuperLSS, declare
offset=on its predictor helpers. The clones slice declared arrays for training and replace them with held-out values for scoring; column sources resolve from each fold’s frame.- groupsarray-like, optional
Group labels forwarded to
cv.split().- fit_mode{“fit”, “fit_reml”}, optional
Which fit method to call on each fold estimator.
"fit"holds the smoothing parameters fixed;"fit_reml"selects them again on each fold’s training rows. By default"fit"for a SuperGLM. A SuperLSS has no configured smoothing to hold fixed, so its default is"fit_reml", or"fit"whenfit_kwargsgiveslambdas. On an unfitted SuperLSS with smooth terms,"fit"needs thoselambdas: without them every fold fails, and each failure is logged.- scoringstr, callable, or sequence thereof, optional
Metrics to evaluate. Built-in for a SuperGLM:
"deviance","nll","gini";"deviance"by default. Built-in for a SuperLSS:"nll","crps","gini","actual_expected", all of them by default. A family with neither a closed-form CRPS nor a distribution function has no"crps", so it is left out by default and NaN if asked for. Vector families default to joint"nll"only and explicitly refuse scalar built-in scores; custom scalar callables receive the response in canonical component order. A SuperLSS has no single mean predictor to take a deviance of. Built-in deviance, NLL and CRPS divide their weighted totals by the likelihood size the declared contract implies:sum(sample_weight)under"frequency", the count of positive-weight rows under"prior", so the NLL of the two model types can be compared on the same folds."actual_expected"issum(w * y) / sum(w * mean). Gini remains a separately weighted ranking metric. On a SuperLSS, a fold whose total response is not positive has no Gini: it reads NaN, and the mean leaves it out, as it does a CRPS that cannot be resolved. Both are logged. Callables must followscorer(model, X, y, *, sample_weight, offset) -> float | dict.- return_estimatorsbool
If True, keep the fitted model from each fold.
- return_oofbool
If True, collect out-of-fold predictions. For a SuperLSS, a held-out row of weight zero that cannot be priced reads NaN, with a warning, and does not fail its fold. A held-out row with no predicted mean (
SuperGLM.predict; for a SuperLSS, no value of a parameter) reads NaN, and one warning names every such row by its position in the frame.- error_scorefloat or “raise”
Value to assign when a fold fails.
"raise"propagates the error. A score refused because held-out rows of positive weight have no predicted mean (NoPredictedMeanError) is assigned it too, with the reason infold_scores’"score refused"column;"raise"raises it, itspositionsthose rows’ positions in the frame (or, for a custom scorer that reads other rows, among the rows it read). That fold is not failed: it keeps its fit record, its estimator and its out-of-fold predictions, its other scores stand, and one warning says how many folds were scored.- fit_kwargsmapping, optional
Extra keyword arguments for every fold’s fit call, for example
{"max_reml_iter": 100}.sample_weightandoffsetcome from the split and cannot be set here. Nor can a SuperLSS’struncationandcensored: its folds are scored on complete rows.- warm_startbool, optional
Under
fit_mode="fit_reml", start every fold after the first converged one from that fold’s smoothing parameters (lambda2_init, orlambdasfor a SuperLSS). Components frozen as flat start cold, with the rest of their penalty block. Each fold still searches to its own convergence test.Trueby default for a SuperGLM, whose later folds then take fewer outer iterations.Falseby default for a SuperLSS: its Fellner-Schall search can take longer from another fold’s parameters than from its own data-scaled start, and can reach its iteration limit. That argument infit_kwargstakes precedence for every fold.
- Returns:
- CrossValidationResult
Per-fold scores, mean/std aggregates, and optionally out-of-fold predictions and fitted estimators.
Notes
Everything a fit estimates is estimated inside its fold, and the scores read held-out rows only.
std_scoresdescribes how the folds spread; it is not a standard error of the mean, because folds share training rows (Bates, Hastie and Tibshirani, 2024).