pretab.transformers.CustomBinTransformer
- class pretab.transformers.CustomBinTransformer(output_dim=UNSET)[source]
Bases:
AliasResolverMixin,TransformerMixin,BaseEstimatorCustom binning transformer for one-dimensional numerical features.
This transformer bins continuous values into discrete intervals, using either a fixed number of equal-width bins or a user-provided array of bin edges. It is compatible with scikit-learn pipelines.
- Parameters:
output_dim (int or array-like) – If int, defines the number of equal-width bins. If array-like, defines the bin edges to use directly. Note that
output_dimhere is the number of bins, not the number of output columns: this transformer always emits a single ordinal column of integer bin indices. The bin count only becomes an output width after a subsequent one-hot encoding.- Variables:
n_features_in (int) – The number of input features seen during
fit(expected to be 1).total_output_dim (int) – Total number of output columns (fitted). Always
1because the output is a single ordinal column.
Notes
This transformer operates on a single feature of shape
(n_samples, 1). Whenoutput_dimis an integer, equal-width bin edges are computed from the data range; when it is an array-like, the provided edges are used directly. The output contains integer bin indices in a single column, so its width is1regardless ofoutput_dim– this is a documented exception to the exact-width contract that the fixed-basis families follow.The input must be numeric: binning is performed with
pandas.cut(), so string / categorical data cannot be processed and raises aPretabDataError. Encode such columns with a categorical method (e.g."int"or"one-hot") before binning.Examples
>>> import numpy as np >>> from pretab.transformers import CustomBinTransformer >>> X = np.linspace(0, 1, 10).reshape(-1, 1) >>> transformer = CustomBinTransformer(output_dim=4) >>> transformer.fit_transform(X).shape (10, 1)- __init__(output_dim=UNSET)[source]
Methods
__init__([output_dim])fit(X[, y])Fit the transformer on the data.
fit_transform(X[, y])Fit to data, then transform it.
get_feature_names_out([input_features])Return the names of the transformed features.
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)Transform the data using the specified binning strategy.
- fit(X, y=None)[source]
Fit the transformer on the data.
- Parameters:
X (array-like of shape (n_samples, 1)) – Input data.
y (Ignored) – Not used, present here for API consistency by convention.
- Returns:
self (object) – Fitted transformer.
- get_feature_names_out(input_features=None)[source]
Return the names of the transformed features.
- Parameters:
input_features (list of str) – The names of the input features.
- Returns:
input_features (ndarray of shape (n_features,)) – The names of the output features after transformation.
- transform(X)[source]
Transform the data using the specified binning strategy.
- Parameters:
X (array-like of shape (n_samples, 1)) – Input data to transform.
- Returns:
X_binned (ndarray of shape (n_samples, 1)) – Binned data with integer bin indices.