models.target_encode()
Out-of-fold target encoding of a high-cardinality factor with
Usage
models.target_encode(
values,
y,
expected,
splits,
k,
exposure=None,
)credibility (CAS Monograph 16, chapter 6): each level’s actual-to-expected ratio shrunk to 1 by z = n / (n + k), n its exposure, z * A / E + (1 - z). Each training row is encoded from the other folds only, so its own response never enters its encoding; the returned TargetEncoding, from all rows, encodes new data. Its log enters a GLM as a numeric column.
Parameters
values: list of str-
The factor’s level per row.
y: list of float-
Actual responses.
expected: list of float-
Expected responses: the overall mean times exposure, or a first model’s predictions.
splits: list of (list of int, list of int)-
From k_fold or group_k_fold; every row in exactly one test set.
k: float-
The credibility constant
K. exposure: list of float = None- Credibility weights; 1 per row by default.
Returns
tuple of (list of float, TargetEncoding)- The out-of-fold encoding of each training row, and the encoding for new data.
Examples
>>> from prospicio.models import target_encode
>>> splits = [([1, 3], [0, 2]), ([0, 2], [1, 3])]
>>> rows, enc = target_encode(["a", "a", "b", "b"], [2.0, 2.0, 0.0, 1.0], [1.0] * 4, splits, 1.0)
>>> rows[0]1.5