Categorical & Target Encoders
Rust-accelerated categorical feature and target label encoders in ochreml.preprocessing (OneHotEncoder, OrdinalEncoder, TargetEncoder, LabelEncoder, LabelBinarizer, MultiLabelBinarizer).
OchreML implements six categorical and label encoders in ochreml.preprocessing (also lazily importable from ochreml), powered by Rust Rayon hash/sort kernels with UTF-32 fast paths for NumPy string arrays and native .dit serialization. (*≧ω≦*)
Overview
| Class | Input Type | Output | inverse_transform | .dit Tag |
|---|---|---|---|---|
OneHotEncoder | 2D categorical features | Sparse CSR or dense one-hot array | Yes | 19 |
OrdinalEncoder | 2D categorical features | 2D integer-coded numeric array | Yes | 20 |
TargetEncoder | 2D categorical features + 1D target y | 2D smoothed target-mean array | No | 24 |
LabelEncoder | 1D target labels y | 1D int64 indices 0 .. n_classes - 1 | Yes | 21 |
LabelBinarizer | 1D target labels y | 2D one-vs-all binary matrix (sparse or dense) | Yes | 22 |
MultiLabelBinarizer | Iterable of label iterables y | 2D multilabel indicator matrix (sparse or dense) | Yes | 23 |
All encoders support .save(path), .load(path, device=None), .to(device), and device_.
OneHotEncoder
Encodes categorical columns as a one-hot numeric array, with support for dropping collinear columns, handling unseen categories, and grouping infrequent categories. All constructor parameters are keyword-only.
from ochreml.preprocessing import OneHotEncoder
enc = OneHotEncoder(
categories="auto",
drop=None,
sparse_output=True,
dtype=None,
handle_unknown="error",
min_frequency=None,
max_categories=None,
device=None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
categories | "auto" or sequence of sequences | "auto" | Unique category values per feature. |
drop | {"first", "if_binary"} or sequence or None | None | Strategy to drop one category per feature. |
sparse_output | bool | True | If True, returns a scipy.sparse.csr_matrix; if False, returns a dense np.ndarray. |
dtype | NumPy dtype or None | np.float64 | Output array data type. |
handle_unknown | {"error", "ignore", "infrequent_if_exist"} | "error" | Behavior when unseen categories appear during transform. |
min_frequency | int | float | None | None | Minimum count (if int) or fraction (if float) below which categories are grouped as infrequent. |
max_categories | int | None | None | Upper limit on output categories per input feature (excess grouped as infrequent). |
device | str | None | None | Target compute device. |
Methods & Fitted Attributes
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),inverse_transform(X),get_feature_names_out(input_features=None) - Attributes:
categories_,drop_idx_,infrequent_categories_,n_features_in_,device_
from ochreml.preprocessing import OneHotEncoder
X = [["red", "S"], ["blue", "M"], ["red", "L"]]
enc = OneHotEncoder(sparse_output=False, handle_unknown="ignore")
X_oh = enc.fit_transform(X)
print(enc.get_feature_names_out(["color", "size"]))OrdinalEncoder
Encodes 2D categorical features as ordinal codes 0 .. n_categories - 1 per column. All constructor parameters are keyword-only.
from ochreml.preprocessing import OrdinalEncoder
enc = OrdinalEncoder(
categories="auto",
dtype=None,
handle_unknown="error",
unknown_value=None,
encoded_missing_value=float("nan"),
min_frequency=None,
max_categories=None,
device=None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
categories | "auto" or sequence of sequences | "auto" | Unique categories per feature. |
dtype | NumPy dtype or None | np.float64 | Output array data type. |
handle_unknown | {"error", "use_encoded_value"} | "error" | Whether to raise or substitute unknown_value on unseen categories. |
unknown_value | float | None | None | Code assigned to unseen categories when handle_unknown="use_encoded_value". |
encoded_missing_value | float | nan | Encoded value assigned to missing categories. |
min_frequency | int | float | None | None | Minimum frequency for non-infrequent categories. |
max_categories | int | None | None | Maximum categories per feature. |
device | str | None | None | Target compute device. |
Methods & Fitted Attributes
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),inverse_transform(X),get_feature_names_out(input_features=None) - Attributes:
categories_,infrequent_categories_,n_features_in_,device_
TargetEncoder
Encodes high-cardinality categorical features using empirical Bayes shrinkage toward the global target mean. Calling fit_transform(X, y) uses internal cv-fold cross-fitting to prevent target leakage, while fit(X, y) fits on the full dataset for transforming test sets. All constructor parameters are keyword-only.
from ochreml.preprocessing import TargetEncoder
enc = TargetEncoder(
categories="auto",
target_type="auto",
smooth="auto",
cv=5,
shuffle=True,
random_state=None,
device=None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
categories | "auto" or sequence of sequences | "auto" | Categories per feature. |
target_type | {"auto", "continuous", "binary", "multiclass"} | "auto" | Target distribution type; inferred automatically when "auto". |
smooth | "auto" or float | "auto" | Shrinkage weight mixing category target mean with the global target mean. |
cv | int | 5 | Number of internal cross-fitting folds used in fit_transform. |
shuffle | bool | True | Whether to shuffle samples before assigning CV folds. |
random_state | int | None | None | Random seed when shuffle=True. |
device | str | None | None | Target compute device. |
Methods & Fitted Attributes
- Methods:
fit(X, y),fit_transform(X, y),transform(X),get_feature_names_out(input_features=None) - Attributes:
categories_,encodings_,target_mean_,target_type_,classes_,n_features_in_,device_
For multiclass targets, TargetEncoder outputs one encoded column per (feature, class) pair.
LabelEncoder
Encodes 1D target labels into contiguous int64 integers from 0 to n_classes - 1.
from ochreml.preprocessing import LabelEncoder
le = LabelEncoder(device=None)
y_enc = le.fit_transform(["cat", "dog", "cat", "bird"])
y_orig = le.inverse_transform(y_enc)- Parameters:
device: str | None = None - Methods:
fit(y),transform(y),fit_transform(y),inverse_transform(y) - Attributes:
classes_,device_
LabelBinarizer
Binarizes 1D target labels in a one-vs-all matrix format. Binary targets produce shape (n_samples, 1); multiclass targets produce shape (n_samples, n_classes). All constructor parameters are keyword-only.
from ochreml.preprocessing import LabelBinarizer, label_binarize
lb = LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False, device=None)
Y = lb.fit_transform(["a", "b", "c", "a"])
y_back = lb.inverse_transform(Y, threshold=None)
# Functional helper with explicit class ordering
Y_explicit = label_binarize(["a", "c"], classes=["a", "b", "c"], neg_label=0, pos_label=1, sparse_output=False)- Parameters:
neg_label: int = 0,pos_label: int = 1,sparse_output: bool = False,device: str | None = None - Methods:
fit(y),transform(y),fit_transform(y),inverse_transform(Y, threshold=None) - Attributes:
classes_,y_type_("binary","multiclass", or"unknown"),device_
MultiLabelBinarizer
Transforms an iterable of label iterables (such as lists or sets of tags per sample) into a 2D binary indicator matrix. All constructor parameters are keyword-only.
from ochreml.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer(classes=None, sparse_output=False, device=None)
Y = mlb.fit_transform([("rust", "ml"), ("python",), ("rust", "gpu")])
tags_back = mlb.inverse_transform(Y)- Parameters:
classes: Sequence[Any] | None = None,sparse_output: bool = False,device: str | None = None - Methods:
fit(y),transform(y),fit_transform(y),inverse_transform(yt)(returnslist[tuple]) - Attributes:
classes_,device_
Scalers & Normalizers
Hardware-accelerated feature scaling and sample normalization in ochreml.preprocessing (StandardScaler, MinMaxScaler, MaxAbsScaler, RobustScaler, Normalizer).
Non-Linear, Feature & Kernel Transformers
Distribution transforms, discretization, polynomial/spline basis expansion, kernel centering, and Random Fourier Features in ochreml.preprocessing and ochreml.kernel_approximation.