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.
This reference covers non-linear distribution transforms, discretization, custom callable wrappers, feature expansion, and kernel approximation in ochreml.preprocessing and ochreml.kernel_approximation. All classes are also lazily importable from ochreml and support .dit persistence. (o´∀`o)
Overview
| Class | Submodule | Purpose | inverse_transform | .dit Tag |
|---|---|---|---|---|
PowerTransformer | ochreml.preprocessing | Box-Cox / Yeo-Johnson Gaussianizing power transforms | Yes | 16 |
QuantileTransformer | ochreml.preprocessing | Empirical quantile mapping to uniform or normal | Yes | 17 |
FunctionTransformer | ochreml.preprocessing | Wraps an arbitrary Python callable into a transformer | Optional | 35 |
KBinsDiscretizer | ochreml.preprocessing | Bins continuous features into discrete intervals | Yes | 15 |
Binarizer | ochreml.preprocessing | Thresholds numeric features to 0.0 or 1.0 | No | 12 |
PolynomialFeatures | ochreml.preprocessing | Generates polynomial and interaction terms | No | 14 |
SplineTransformer | ochreml.preprocessing | Generates univariate B-spline basis functions | No | 18 |
KernelCenterer | ochreml.preprocessing | Centers a precomputed kernel matrix in feature space | No | 13 |
RBFSampler | ochreml.kernel_approximation | Random Fourier Features approximation of RBF kernel | No | 95 |
PowerTransformer
Applies a parametric, monotonic feature-wise power transform to stabilize variance and reduce skewness. Uses Brent optimization in Rust to estimate lambdas_ via maximum likelihood.
from ochreml.preprocessing import PowerTransformer, power_transform
pt = PowerTransformer(
method="yeo-johnson",
standardize=True,
copy=True,
device=None,
)
X_gauss = pt.fit_transform(X)
X_orig = pt.inverse_transform(X_gauss)
# Stateless helper
X_gauss_once = power_transform(X, method="yeo-johnson", standardize=True, copy=True, device=None)Parameters & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
method | {"yeo-johnson", "box-cox"} | "yeo-johnson" | Power transform family ("box-cox" requires strictly positive input). |
standardize | bool | True | Keyword-only; apply zero-mean, unit-variance scaling after the power transform. |
copy | bool | True | Keyword-only; kept for scikit-learn parity. |
device | str | None | None | Keyword-only; target compute device. |
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),inverse_transform(X),get_feature_names_out(input_features=None) - Attributes:
lambdas_,n_features_in_,device_
QuantileTransformer
Maps each feature non-linearly to a uniform [0, 1] or standard normal distribution using empirical quantiles. All constructor parameters are keyword-only.
from ochreml.preprocessing import QuantileTransformer, quantile_transform
qt = QuantileTransformer(
n_quantiles=1000,
output_distribution="uniform",
ignore_implicit_zeros=False,
subsample=10_000,
random_state=None,
copy=True,
device=None,
)
X_q = qt.fit_transform(X)
# Stateless helper
X_q_once = quantile_transform(X, axis=0, n_quantiles=1000, output_distribution="uniform")Parameters & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
n_quantiles | int | 1000 | Number of quantile landmarks computed per feature. |
output_distribution | {"uniform", "normal"} | "uniform" | Target marginal distribution. |
ignore_implicit_zeros | bool | False | Kept for sparse API parity. |
subsample | int | 10000 | Maximum samples used to estimate empirical quantiles. |
random_state | int | None | None | Seed used when subsampling. |
copy | bool | True | Kept for scikit-learn parity. |
device | str | None | None | Target compute device. |
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),inverse_transform(X),get_feature_names_out(input_features=None) - Attributes:
n_quantiles_,references_,quantiles_,n_features_in_,device_
FunctionTransformer
Wraps any Python callable (and optional inverse callable) into a pipeline-compatible transformer with .dit persistence (tag 35).
import numpy as np
from ochreml.preprocessing import FunctionTransformer
log_tf = FunctionTransformer(
func=np.log1p,
inverse_func=np.expm1,
validate=False,
accept_sparse=False,
check_inverse=True,
feature_names_out="one-to-one",
kw_args=None,
inv_kw_args=None,
)
X_log = log_tf.fit_transform(X)
X_back = log_tf.inverse_transform(X_log)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
func | Callable | None | None | Forward function (identity if None). |
inverse_func | Callable | None | None | Inverse function (identity if None). |
validate | bool | False | Keyword-only; if True, validates that X is a 2D array before calling func. |
accept_sparse | bool | False | Keyword-only; allows sparse matrices when validate=True. |
check_inverse | bool | True | Keyword-only; kept for scikit-learn parity. |
feature_names_out | "one-to-one", Callable, or None | None | Keyword-only; strategy for get_feature_names_out. |
kw_args | dict | None | None | Keyword-only; extra keyword arguments passed to func. |
inv_kw_args | dict | None | None | Keyword-only; extra keyword arguments passed to inverse_func. |
KBinsDiscretizer
Discretizes continuous features into n_bins intervals using uniform width, equal-frequency quantiles, or 1D k-means clustering.
from ochreml.preprocessing import KBinsDiscretizer
kb = KBinsDiscretizer(
n_bins=5,
encode="onehot",
strategy="quantile",
quantile_method="averaged_inverted_cdf",
dtype=None,
subsample=200_000,
random_state=None,
device=None,
)
X_binned = kb.fit_transform(X)Parameters & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
n_bins | int or Sequence[int] | 5 | Number of bins (global or per-feature). |
encode | {"onehot", "onehot-dense", "ordinal"} | "onehot" | Keyword-only; "onehot" returns CSR sparse matrix, "onehot-dense" and "ordinal" return dense arrays. |
strategy | {"uniform", "quantile", "kmeans"} | "quantile" | Keyword-only; strategy for computing bin edges. |
quantile_method | str | "averaged_inverted_cdf" | Keyword-only; quantile interpolation method. |
dtype | NumPy dtype or None | None | Keyword-only; desired output dtype. |
subsample | int | None | 200000 | Keyword-only; maximum samples used to fit bin edges. |
random_state | int | None | None | Keyword-only; seed for subsampling. |
device | str | None | None | Keyword-only; target compute device. |
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),inverse_transform(Xt),get_feature_names_out(input_features=None) - Attributes:
bin_edges_,n_bins_,n_features_in_,device_
Binarizer
Maps values strictly greater than threshold to 1.0 and values less than or equal to threshold to 0.0. All constructor parameters are keyword-only.
from ochreml.preprocessing import Binarizer, binarize
b = Binarizer(threshold=0.0, copy=True, device=None)
X_bin = b.fit_transform(X)
# Stateless helper
X_bin_once = binarize(X, threshold=0.5, copy=True, device=None)- Parameters:
threshold: float = 0.0,copy: bool = True,device: str | None = None - Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),get_feature_names_out(input_features=None) - Attributes:
n_features_in_,device_
PolynomialFeatures
Generates all polynomial combinations of input features with degree less than or equal to degree (or within (min_degree, max_degree)).
from ochreml.preprocessing import PolynomialFeatures, add_dummy_feature
poly = PolynomialFeatures(
degree=2,
interaction_only=False,
include_bias=True,
order="C",
device=None,
)
X_poly = poly.fit_transform(X)
print(poly.get_feature_names_out(["a", "b"]))
# Helper to prepend a constant column (default 1.0)
X_aug = add_dummy_feature(X, value=1.0)Parameters & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
degree | int or tuple[int, int] | 2 | Maximum degree, or (min_degree, max_degree) inclusive range. |
interaction_only | bool | False | Keyword-only; if True, omit powers of individual features (only cross-products). |
include_bias | bool | True | Keyword-only; if True, include the constant power-zero column 1. |
order | {"C", "F"} | "C" | Keyword-only; memory layout of dense output array. |
device | str | None | None | Keyword-only; target compute device. |
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),get_feature_names_out(input_features=None) - Attributes:
powers_,n_output_features_,n_features_in_,device_
SplineTransformer
Generates univariate B-spline basis functions for each input feature, enabling smooth non-linear modeling with linear estimators.
from ochreml.preprocessing import SplineTransformer
sp = SplineTransformer(
n_knots=5,
degree=3,
knots="uniform",
extrapolation="constant",
include_bias=True,
order="C",
sparse_output=False,
device=None,
)
X_sp = sp.fit_transform(X)Parameters & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
n_knots | int | 5 | Number of knots per feature. |
degree | int | 3 | Polynomial degree of the B-spline basis. |
knots | {"uniform", "quantile"} or 2D array | "uniform" | Keyword-only; knot placement strategy or explicit (n_knots, n_features) array. |
extrapolation | {"error", "constant", "linear", "continue", "periodic"} | "constant" | Keyword-only; behavior outside [min_knot, max_knot]. |
include_bias | bool | True | Keyword-only; if True, include the first basis column. |
order | {"C", "F"} | "C" | Keyword-only; memory layout of dense output array. |
sparse_output | bool | False | Keyword-only; if True, returns a scipy.sparse.csr_matrix. |
device | str | None | None | Keyword-only; target compute device. |
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),get_feature_names_out(input_features=None) - Attributes:
bsplines_,n_features_out_,n_features_in_,device_
KernelCenterer
Centers a precomputed kernel matrix K in reproducing kernel Hilbert space without explicitly computing the feature map.
from ochreml.preprocessing import KernelCenterer
kc = KernelCenterer(device=None)
K_centered = kc.fit_transform(K_train)
K_test_centered = kc.transform(K_test)- Parameters:
device: str | None = None - Methods:
fit(K, y=None),transform(K, copy=True),fit_transform(K, y=None) - Attributes:
K_fit_rows_,K_fit_all_,n_features_in_,device_
RBFSampler
Located in ochreml.kernel_approximation (and lazily importable from ochreml), RBFSampler approximates an RBF kernel exp(-gamma * ||x - y||^2) via Monte Carlo Random Fourier Features so linear models can learn non-linear decision boundaries in O(n * n_components) time.
from ochreml.kernel_approximation import RBFSampler
rbf = RBFSampler(
gamma=1.0,
n_components=100,
random_state=42,
device=None,
)
X_rff = rbf.fit_transform(X)Parameters & Attributes
| Parameter | Type | Default | Description |
|---|---|---|---|
gamma | float or "scale" | 1.0 | RBF kernel bandwidth; "scale" computes 1 / (n_features * X.var()). |
n_components | int | 100 | Number of Monte Carlo Fourier features to generate. |
random_state | int | None | None | Seed for deterministic random weights and offsets. |
device | str | None | None | Target compute device. |
- Methods:
fit(X, y=None),transform(X),fit_transform(X, y=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None),to(device) - Attributes:
random_weights_,random_offset_,gamma_,n_features_in_,device_
Categorical & Target Encoders
Rust-accelerated categorical feature and target label encoders in ochreml.preprocessing (OneHotEncoder, OrdinalEncoder, TargetEncoder, LabelEncoder, LabelBinarizer, MultiLabelBinarizer).
Missing Value Imputers
Univariate, k-NN, iterative MICE imputation, and missingness indicators in ochreml.impute (SimpleImputer, MissingIndicator, KNNImputer, IterativeImputer).