mojolearn.OneHotEncoder#

class mojolearn.OneHotEncoder(*, categories='auto', drop=None, sparse_output=True, dtype=None, handle_unknown='error', min_frequency=None, max_categories=None, feature_name_combiner='concat')[source]#

Bases: _PrepBase

Encode numeric categorical features as one-hot columns.

Follows scikit-learn’s OneHotEncoder for numeric columns. Each column becomes a block of 0/1 float32 columns, one per category (minus a dropped one, see drop). The output is always a dense float32 array, whatever sparse_output says. NaN is a category and sorts last. 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.

  • drop ({None, ‘first’, ‘if_binary’} or array-like, default None) – Category to leave out of each block: none, the first, the first of two-category columns only, or one given category per column. Dropping a category that is not in the training data, or one grouped as infrequent, raises ValueError.

  • sparse_output (bool, default True) – Accepted for scikit-learn compatibility; the output is always dense.

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

  • handle_unknown ({‘error’, ‘ignore’, ‘infrequent_if_exist’, ‘warn’}, default ‘error’) – 'error' raises ValueError on a value outside the categories. 'ignore' writes an all-zero block. 'infrequent_if_exist' and 'warn' send the value to the column’s infrequent category when it has one, otherwise an all-zero block; 'warn' (and 'ignore' or 'infrequent_if_exist' together with drop) issues a UserWarning.

  • 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 column, the last of the feature’s block.

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

  • feature_name_combiner (str, default ‘concat’) – Accepted for scikit-learn compatibility.

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

  • drop_idx_ (list or None) – Index into categories_[j] of the dropped category of each column (None for no drop).

  • 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 takes each block’s largest entry. An all-zero block decodes to the dropped category; with no dropped category it decodes to NaN where scikit-learn would return None (under handle_unknown='ignore', or under 'infrequent_if_exist' or 'warn' for a column without an infrequent category), and raises ValueError otherwise. The infrequent column decodes to NaN where scikit-learn returns 'infrequent_sklearn'.

Examples

>>> import numpy as np, mojolearn
>>> X = np.array([[0.0, 1.0], [1.0, 1.0], [2.0, 0.0]], dtype=np.float32)
>>> mojolearn.OneHotEncoder(drop="first").fit_transform(X)
__init__(*, categories='auto', drop=None, sparse_output=True, dtype=None, handle_unknown='error', min_frequency=None, max_categories=None, feature_name_combiner='concat')[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.