LabelEncoder transformer.
Convert string attributes to nominal (integer-encoded categorical).
Classes
Convert string attributes to nominal (integer-encoded categorical).
Encodes categorical string values into distinct integers. This is a common preprocessing step for algorithms that require numerical input.
Constructor
__init__( self, columns: Optional[List[int]] = None, )
Parameters
columns
list of int
Indices of columns to convert. If
None, automatically detects and converts all non-numeric (string/object) columns.
Attributes
categories_
dict
Mapping of column index to the list of unique categories found.
Notes
-
Unlike
OrdinalEncoder, this does not assume any particular order. - It can handle unseen categories by mapping them to -1 during transform.
Encode string categories to integers:
python
>>> from tuiml.preprocessing.encoding import LabelEncoder
>>> import numpy as np
>>> X = np.array([['cat'], ['dog'], ['cat']], dtype=object)
>>> encoder = LabelEncoder()
>>> X_encoded = encoder.fit_transform(X)
>>> print(X_encoded.flatten())
[0. 1. 0.]
Methods
get_parameter_schema
(cls)
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None, feature_names: Optional[List[str]]=None) -> 'LabelEncoder'
transform
(self, X: np.ndarray) -> np.ndarray
categories_
(self)
__repr__
(self) -> str