Skip to main content

LabelEncoder

Encodes categorical labels to contiguous integer indices [0,nclasses)[0,\, n_{\text{classes}}), suitable for use as targets in classification models.

Mapping

f:L{0,1,,L1}f: \mathcal{L} \to \{0, 1, \ldots, |\mathcal{L}| - 1\}

where L\mathcal{L} is the set of unique labels sorted in ascending order. The mapping uses sorted order and binary search for O(logL)O(\log |\mathcal{L}|) lookup per label.

The inverse mapping inverse_transform recovers the original labels from their integer encoding.

Mirrors sklearn.preprocessing.LabelEncoder.

Constructor

Skigen::LabelEncoder<Label> encoder;
TemplateDefaultDescription
LabelintLabel type

Methods

MethodDescription
fit(y)Learn label-to-index mapping
transform(y)Encode labels to integers
fit_transform(y)Fit and transform in one call
inverse_transform(y)Decode back to original labels

Fitted Attributes

AccessorTypeDescription
classes()std::vector<Label>Sorted unique classes
n_classes()Eigen::IndexNumber of classes

Example

#include <Skigen/Preprocessing>

Skigen::LabelEncoder encoder;
auto encoded = encoder.fit_transform(labels);
auto decoded = encoder.inverse_transform(encoded);