OrdinalEncoder transformer.

Convert ordinal (ordered categorical) to numeric values.

Classes

OrdinalEncoder

class preprocessing.encoding.ordinal.OrdinalEncoder(Transformer)

Convert ordinal (ordered categorical) attributes to numeric.

Maps categories to integer values based on a specified or inferred order. This is suitable for features where the order carries meaning (e.g., "low", "medium", "high").
Constructor
__init__(
    self,
    categories: Optional[Dict[int, List]] = None,
    columns: Optional[List[int]] = None,
)

Parameters

categories
dict or list

The order of categories for each column.

  • Dict: {col_idx: [cat1, cat2, ...]}
  • List: [[cats_col0], [cats_col1], ...]
If None, the order is inferred from the first occurrence in the data.
columns
list of int
Indices of columns to encode. If None, all columns are encoded.

Attributes

category_maps_
dict
Mapping of categories to integers for each column.

Encode ordered categories:

python
>>> from tuiml.preprocessing.encoding import OrdinalEncoder
>>> import numpy as np
>>> X = np.array([['low'], ['medium'], ['high']], dtype=object)
>>> encoder = OrdinalEncoder(categories=[['low', 'medium', 'high']])
>>> X_encoded = encoder.fit_transform(X)
>>> print(X_encoded.flatten())
[0. 1. 2.]

Methods

get_parameter_schema (cls)
fit (self, X: np.ndarray, y: Optional[np.ndarray]=None, feature_names: Optional[List[str]]=None) -> 'OrdinalEncoder'
transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str