pretab.transformers.OneHotFromOrdinalTransformer
- class pretab.transformers.OneHotFromOrdinalTransformer[source]
Bases:
TransformerMixin,BaseEstimatorConvert ordinal-encoded features into a one-hot encoded representation.
This is useful when features have already been ordinal-encoded and a one-hot representation is required for model training.
- Variables:
max_bins (ndarray of shape (n_features,)) – Array containing the maximum bin index (plus one) for each feature, which determines the width of the one-hot block for that feature.
Notes
Each input feature
iis expanded intomax_bins_[i]columns, so the total number of output columns is the sum ofmax_bins_across all features.Examples
>>> import numpy as np >>> from pretab.transformers import OneHotFromOrdinalTransformer >>> X = np.array([[0, 1], [1, 0], [2, 1]]) >>> transformer = OneHotFromOrdinalTransformer() >>> transformer.fit_transform(X).shape (3, 5)Methods
__init__()fit(X[, y])Learn the maximum bin index for each feature from the data.
fit_transform(X[, y])Fit to data, then transform it.
get_feature_names_out([input_features])Generate feature names for the one-hot encoded output.
get_metadata_routing()Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
transform(X)Convert ordinal-encoded features into one-hot encoded format.
- fit(X, y=None)[source]
Learn the maximum bin index for each feature from the data.
- Parameters:
X (array-like of shape (n_samples, n_features)) – The input data containing ordinal-encoded features.
y (Ignored) – Not used, present for API consistency by convention.
- Returns:
self (object) – Fitted transformer.
- get_feature_names_out(input_features=None)[source]
Generate feature names for the one-hot encoded output.
- Parameters:
input_features (list of str) – The names of the input features that were ordinal-encoded.
- Returns:
feature_names (ndarray of shape (n_output_features,)) – The names of the one-hot encoded features.
- transform(X)[source]
Convert ordinal-encoded features into one-hot encoded format.
Uses the
max_bins_learned during fitting.- Parameters:
X (array-like of shape (n_samples, n_features)) – The input data containing ordinal-encoded features.
- Returns:
X_one_hot (ndarray of shape (n_samples, n_output_features)) – The one-hot encoded features.
Notes
Codes outside the range learned during
fit()(negative values or values>= max_bins_[i]) are treated as unknown and encoded as an all-zero row, mirroring scikit-learn’shandle_unknown="ignore".