mojolearn.TargetEncoder#
- class mojolearn.TargetEncoder(categories='auto', target_type='auto', smooth='auto', cv=5, shuffle=True, random_state=None)[source]#
Bases:
_PrepBaseEncode numeric categorical features by the target mean of each category.
Follows scikit-learn’s
TargetEncoderfor numeric category columns. Each category is replaced by a shrunk mean of the target over the rows in that category. Binary, continuous and multiclass targets are supported; a multiclass target produces one output column per class for each feature. Computation is float32.fit_transformcross-fits: each row is encoded with statistics learned from the other folds, as in scikit-learn.transform(andfitfollowed bytransform) uses statistics from all the training rows, sofit(X, y).transform(X)differs fromfit_transform(X, y).- Parameters:
categories (‘auto’ or list of array-like, default ‘auto’) –
'auto'takes the categories from the training data. Otherwise one list per column, numeric and sorted ascending. A training value outside a given list is left out of every category’s statistics but still counts toward the target mean; scikit-learn codes it as the first category instead.target_type ({‘auto’, ‘binary’, ‘continuous’, ‘multiclass’}, default ‘auto’) – Type of the target;
'auto'infers it fromy.smooth (‘auto’ or float, default ‘auto’) – Shrinkage toward the target mean:
'auto'uses an empirical Bayes estimate, a float>= 0sets it directly.cv (int, splitter or iterable, default 5) – Folds for
fit_transform. An int>= 2uses KFold for a continuous target and StratifiedKFold otherwise. A splitter object (itssplit(X, y)) or an iterable of(train, test)index pairs is also accepted: the test folds must cover every row exactly once (ValueErrorotherwise), and each fold’s training rows must be all the other rows (NotImplementedErrorotherwise).shuffle (bool, default True) – Shuffle the rows before splitting into folds.
random_state (int or None, default None) – Seed for the shuffle. None uses seed 0, so repeated fits give the same folds. The shuffle uses mojolearn’s own generator, so folds differ from scikit-learn’s for the same seed.
- Variables:
categories_ (list of arrays) – Sorted categories of each column, float32.
encodings_ (list of arrays) – Per-column encoding of each category (times the number of classes for a multiclass target).
target_type_ (str) – Target type used for the fit.
target_mean_ (array) – Mean of the target (one value per class for a multiclass target).
classes_ (array or None) – Class labels for a binary or multiclass target, None for a continuous one.
n_features_in_ (int) – Number of columns seen in fit.
numeric_mode_ (str) – Numeric mode the estimator was fitted in; transform runs in the same mode.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> X = rng.integers(0, 10, (1000, 3)).astype(np.float32) >>> y = rng.random(1000).astype(np.float32) >>> Xt = mojolearn.TargetEncoder().fit_transform(X, y)
- __init__(categories='auto', target_type='auto', smooth='auto', cv=5, shuffle=True, random_state=None)[source]#
Methods
fit(X, y)fit_transform(X, y)Fit to
X, then transformX.get_params([deep])Return the constructor parameters as a dict.
set_params(**params)Set constructor parameters.
transform(X)