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:
_PrepBaseEncode numeric categorical features as integer codes.
Follows scikit-learn’s
OrdinalEncoderfor 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 asencoded_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 raisesValueError.dtype (default None) – Accepted for scikit-learn compatibility; the output is always float32.
handle_unknown ({‘error’, ‘use_encoded_value’}, default ‘error’) –
'error'raisesValueErrorwhen transform (or fit, with givencategories) meets a value outside the categories.'use_encoded_value'writesunknown_valueinstead.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:
Notes
inverse_transformmaps codes back to categories. Where scikit-learn returnsNone(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 raisesValueError. Unlike scikit-learn,unknown_valuemay 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 transformX.get_params([deep])Return the constructor parameters as a dict.
inverse_transform(X)set_params(**params)Set constructor parameters.
transform(X)