mojolearn.OrdinalEncoder#

class mojolearn.OrdinalEncoder(*, categories='auto', dtype=None, handle_unknown='error', unknown_value=None, encoded_missing_value=nan, min_frequency=None, max_categories=None)[source]#

Bases: _PrepBase

Encode numeric categorical features as integer codes.

Follows scikit-learn’s OrdinalEncoder for numeric columns. Each value is replaced by its index among its column’s sorted distinct training values, returned as float32. NaN is a category: when a column holds NaN in fit, NaN sorts last and is written as encoded_missing_value. Columns of strings or other objects are not supported.

Parameters:
  • categories (‘auto’ or list of array-like, default ‘auto’) – 'auto' takes the categories from the training data. Otherwise one list per column, numeric, sorted ascending, without duplicates in float32 and with NaN (if present) last; anything else raises ValueError.

  • dtype (default None) – Accepted for scikit-learn compatibility; the output is always float32.

  • handle_unknown ({‘error’, ‘use_encoded_value’}, default ‘error’) – 'error' raises ValueError when transform (or fit, with given categories) meets a value outside the categories. 'use_encoded_value' writes unknown_value instead.

  • unknown_value (float or None, default None) – Code for unknown values under handle_unknown='use_encoded_value'; must be a number (NaN is allowed) and must not equal a code already used by a category (ValueError).

  • encoded_missing_value (float, default NaN) – Code written for NaN. Must be a number and must not equal a code used by a known category.

  • min_frequency (int or float, default None) – Categories seen fewer times than this (an int >= 1, or a fraction of the rows in (0, 1)) are grouped into one infrequent code placed after the frequent ones. The NaN category is never grouped.

  • max_categories (int, default None) – Upper limit on output codes per column, counting the infrequent code; must be >= 1.

Variables:
  • categories_ (list of arrays) – Sorted categories of each column, float32, NaN last when present.

  • 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.

Notes

inverse_transform maps codes back to categories. Where scikit-learn returns None (an unknown value) or the string 'infrequent_sklearn' (the infrequent code), the result here is NaN, since the output is a float array. A code that names no category raises ValueError. Unlike scikit-learn, unknown_value may not equal the infrequent code.

Examples

>>> import numpy as np, mojolearn
>>> X = np.array([[3.0, 10.0], [1.0, 20.0], [3.0, 10.0]], dtype=np.float32)
>>> mojolearn.OrdinalEncoder().fit_transform(X)
__init__(*, categories='auto', dtype=None, handle_unknown='error', unknown_value=None, encoded_missing_value=nan, min_frequency=None, max_categories=None)[source]#

Methods

fit(X[, y])

fit_transform(X[, y])

Fit to X, then transform X.

get_params([deep])

Return the constructor parameters as a dict.

inverse_transform(X)

set_params(**params)

Set constructor parameters.

transform(X)

fit_transform(X, y=None, **fit_params)[source]#

Fit to X, then transform X.

Returns:

array – The transformed data.