Categorical#

class superglm.Categorical(
base: Literal['most_exposed', 'first'] | str = 'most_exposed',
grouping=None,
*,
levels=None,
unseen: Literal['error', 'base'] | str = 'error',
constraint: ConstraintSpec | ConstraintRules | None = None,
)#

Bases: object

One-hot encoded categorical feature.

Parameters:
basestr

How to choose the reference level.

  • 'most_exposed' - level with highest total sample_weight (default, best for insurance)

  • 'first' - first level in the level universe (alphabetical when inferred, as declared when levels= or a categorical dtype bounds it)

Or pass a specific level name as a string.

groupingLevelGrouping, optional

Collapse original levels into groups before fitting.

levelslist | tuple | Series | ndarray | CategoricalDtype, optional

The level universe to bind to. With a grouping this declares the RAW, pre-collapse universe, which the grouping must cover exactly. Levels with no training rows are pinned to base rather than dropped; training rows outside the universe are an error.

unseenstr

Predict-time policy for levels outside the universe. 'error' (default) raises; 'base' routes those rows, raw labels a grouping does not know included, to the base level with one warning per call. Any other value names a group of grouping whose effect those levels then take, again with one warning per call; the fit checks that the group exists and has a place in the fitted universe.

constraintConstraintSpec | ConstraintRules, optional

A relativity cap, Constraint.fit.cap(lo, hi): every level’s printed relativity against the base lies in [lo, hi] (needs a log link). A shape rule needs ordered levels; use OrderedCategorical for one.

property constraint_rules: ConstraintRules#

The fit-time rules this term carries: a cap, or none.

property constraint_kind: str | None#

The rules’ description, or None without a cap.

property constraint_mode: str#

"fit" with a cap; "postfit", the unconstrained default, without.

adopt_dtype_categories(categories: list) → None#

Adopt a dtype-declared universe unless one is already declared.

apply_level_binding(binding) → None#

Adopt a full-frame binding: universe if unset, base pin if unpinned.

resolve_binding(
values: NDArray,
sample_weight=None,
)#

Compute this spec’s full-frame binding without mutating the spec.

build(
x: NDArray,
sample_weight: NDArray[floating] | None = None,
) → GroupInfo#

Build sparse one-hot design columns, choosing the base level from x.

transform(
x: NDArray,
) → NDArray#

One-hot encode using levels learned during build().

score(
x: NDArray,
beta: NDArray[floating],
) → NDArray[floating]#

Score the fitted categorical contribution directly on new data.

reconstruct(
beta: NDArray[floating],
) → dict[str, Any]#

Coefficients -> relativity table.