deepbox/preprocess
Scalers
StandardScaler, MinMaxScaler, MaxAbsScaler, RobustScaler, Normalizer, QuantileTransformer and PowerTransformer for numeric feature matrices.
- Scaling
Overview
- Fit on the training split only, then call
transformon every other split. The scalers havefit,transform,fitTransformandinverseTransform.StandardScalerandMinMaxScaleralso havepartialFitfor batches.Normalizeris stateless and has no inverse. - Input is a 2-D tensor, and the output is a
float64tensor whatever the input dtype. Fitted values are getters without scikit-learn's trailing underscore:scaler.mean,scaler.scale,scaler.variance. StandardScalerdivides by the population standard deviation (divisor n), as scikit-learn does. A feature with zero variance is only centered.RobustScalercenters on the median and divides by the interquartile range.PowerTransformerdefaults tostandardize: false, where scikit-learn defaults totrue. Pass{ standardize: true }to match it. The default method is"yeo-johnson", and"box-cox"needs strictly positive data.
MaxAbsScaler
Scale each feature by its maximum absolute value.
The result lies in [-1, 1]. No centering is applied, so zeros stay zeros. Features that are entirely zero are left unchanged.
Fitted attributes (read-only getters): maxAbs, scale, nSamplesSeen, nFeaturesIn.
Members (13)
constructor(options?: { copy?: boolean; })Creates a new MaxAbsScaler.
maxAbs: Tensor | undefinedPer-feature maximum absolute value, or
undefinedif unfitted.scale: Tensor | undefinedPer-feature divisor applied by
transform(1 for all-zero features), orundefinedif unfitted.nSamplesSeen: numberNumber of samples seen so far (0 when unfitted).
nFeaturesIn: numberNumber of features seen during fitting (0 when unfitted).
fit(X: Tensor, _y?: Tensor): thisCompute the maximum absolute value of each feature, discarding any previous fit.
partialFit(X: Tensor, _y?: Tensor): thisUpdate the running maximum absolute values with another batch of samples.
transform(X: Tensor): TensorDivide each feature by its fitted maximum absolute value.
fitTransform(X: Tensor, _y?: Tensor): TensorFit to X, then transform it.
inverseTransform(X: Tensor): TensorMultiply each feature by its fitted maximum absolute value.
getParams(): Record<string, unknown>Constructor options of this scaler.
setParams(_params: Record<string, unknown>): thisThis scaler has no options; present for API consistency.
clone(): MaxAbsScalerCreate an unfitted scaler.
MinMaxScaler
Scale each feature to a given range, by default [0, 1].
Formula: X_scaled = (X - X.min) / (X.max - X.min) * (max - min) + min
A feature with a single distinct value is shifted so that this value maps to featureRange[0]. Values outside the fitted range map outside the target range unless clip is set.
Fitted attributes (read-only getters): dataMin, dataMax, dataRange, scale, nSamplesSeen, nFeaturesIn.
Members (15)
constructor(options?: { featureRange?: [ number, number ]; clip?: boolean; copy?: boolean; })Creates a new MinMaxScaler.
dataMin: Tensor | undefinedPer-feature minimum seen during fitting, or
undefinedif unfitted.dataMax: Tensor | undefinedPer-feature maximum seen during fitting, or
undefinedif unfitted.dataRange: Tensor | undefinedPer-feature range (max - min) seen during fitting, or
undefinedif unfitted.scale: Tensor | undefinedPer-feature multiplier applied by
transform, orundefinedif unfitted.nSamplesSeen: numberNumber of samples seen so far (0 when unfitted).
nFeaturesIn: numberNumber of features seen during fitting (0 when unfitted).
fit(X: Tensor, _y?: Tensor): thisCompute the per-feature minimum and maximum, discarding any previous fit.
partialFit(X: Tensor, _y?: Tensor): thisUpdate the running minimum and maximum with another batch of samples.
transform(X: Tensor): TensorMap X into the feature range using the fitted minimum and maximum.
fitTransform(X: Tensor, _y?: Tensor): TensorFit to X, then transform it.
inverseTransform(X: Tensor): TensorMap data from the feature range back to the original feature space.
getParams(): Record<string, unknown>Constructor options of this scaler.
setParams(params: Record<string, unknown>): thisChange options.
clone(): MinMaxScalerCreate an unfitted scaler with the same options.
Normalizer
Scale each sample (row) to unit norm.
Rows whose norm is zero are left unchanged. The transformer is stateless: fit only validates its input.
import { Normalizer } from 'deepbox/preprocess';import { tensor } from 'deepbox/ndarray'; const X = tensor([[3, 4], [1, 0]]);const Xn = new Normalizer({ norm: 'l2' }).transform(X); // [[0.6, 0.8], [1, 0]]Members (7)
constructor(options?: { norm?: "l1" | "l2" | "max"; copy?: boolean; })Creates a new Normalizer.
fit(X: Tensor, _y?: Tensor): thisValidate X.
transform(X: Tensor): TensorDivide each row by its norm.
fitTransform(X: Tensor, _y?: Tensor): TensorTransform X (there is nothing to fit).
getParams(): Record<string, unknown>Constructor options of this normalizer.
setParams(params: Record<string, unknown>): thisChange options.
clone(): NormalizerCreate a normalizer with the same options.
PowerTransformer
Apply a power transform to make data more Gaussian-like.
Supports Box-Cox (strictly positive data) and Yeo-Johnson (any real data). The exponent λ of each feature is chosen by maximum likelihood on the training data, searching λ in [-5, 5] and widening the search up to [-160, 160] when the optimum lies at the edge. With standardize: true the transformed features are additionally centered and scaled to unit variance.
Difference from scikit-learn: standardize defaults to false here, while scikit-learn's PowerTransformer defaults to True. Pass { standardize: true } to match scikit-learn. The default is kept for compatibility with earlier Deepbox releases.
Fitted attributes (read-only getters): lambdas, mean, scale, nFeaturesIn.
Members (12)
constructor(options?: { method?: "box-cox" | "yeo-johnson"; standardize?: boolean; copy?: boolean; })Creates a new PowerTransformer.
lambdas: Tensor | undefinedFitted exponent of each feature, or
undefinedif unfitted.mean: Tensor | undefinedMean of each transformed feature, or
undefinedif unfitted orstandardizeis false.scale: Tensor | undefinedStandard deviation of each transformed feature (1 if constant), or
undefinedif unfitted orstandardizeis false.nFeaturesIn: numberNumber of features seen during fitting (0 when unfitted).
fit(X: Tensor, _y?: Tensor): thisEstimate the exponent of each feature by maximum likelihood.
transform(X: Tensor): TensorApply the fitted power transform.
inverseTransform(X: Tensor): TensorInverse transform data back to the original feature space.
fitTransform(X: Tensor, _y?: Tensor): TensorFit to X, then transform it.
getParams(): Record<string, unknown>Constructor options of this transformer.
setParams(params: Record<string, unknown>): thisChange options.
clone(): PowerTransformerCreate an unfitted transformer with the same options.
QuantileTransformer
Transform features so that their distribution is uniform or normal.
Each feature is mapped through its empirical quantile function, estimated from nQuantiles evenly spaced quantiles of the training data and interpolated linearly in between. Values outside the training range are mapped to the ends of the output range. Tied training values map to the average of the first and last quantile they cover. With outputDistribution: "normal" the output is clipped to about ±5.2.
Unlike scikit-learn, transform does not snap values lying within an absolute distance of 1e-7 of the training minimum or maximum to the ends of the output range, because that tolerance depends on the data scale. Results differ from scikit-learn only for such values, by at most the local slope times 1e-7.
Fitted attributes (read-only getters): quantiles, references, nFeaturesIn.
Members (11)
constructor(options?: { nQuantiles?: number; outputDistribution?: "uniform" | "normal"; subsample?: number; randomState?: number; copy?: boolean; })Creates a new QuantileTransformer.
quantiles: Tensor | undefinedFitted quantiles as a tensor of shape (n_quantiles, n_features), or
undefinedif unfitted.references: Tensor | undefinedProbabilities that the fitted quantiles correspond to, or
undefinedif unfitted.nFeaturesIn: numberNumber of features seen during fitting (0 when unfitted).
fit(X: Tensor, _y?: Tensor): thisEstimate the quantiles of each feature, discarding any previous fit.
transform(X: Tensor): TensorMap X to the output distribution.
fitTransform(X: Tensor, _y?: Tensor): TensorFit to X, then transform it.
inverseTransform(X: Tensor): TensorInverse transform data back to the original feature space.
getParams(): Record<string, unknown>Constructor options of this transformer.
setParams(params: Record<string, unknown>): thisChange options.
clone(): QuantileTransformerCreate an unfitted transformer with the same options.
RobustScaler
Scale features using statistics that are less sensitive to outliers.
Each feature has its median removed and is divided by an inter-quantile range (the IQR, from the 25th to the 75th percentile, by default). Quantiles use linear interpolation, like numpy.percentile. Features whose range is zero are only centered.
Fitted attributes (read-only getters): center, scale, nFeaturesIn.
Members (11)
constructor(options?: { withCentering?: boolean; withScaling?: boolean; quantileRange?: [ number, number ]; unitVariance?: boolean; copy?: boolean; })Creates a new RobustScaler.
center: Tensor | undefinedPer-feature median, or
undefinedif unfitted orwithCenteringis false.scale: Tensor | undefinedPer-feature scale (1 where the range is zero), or
undefinedif unfitted orwithScalingis false.nFeaturesIn: numberNumber of features seen during fitting (0 when unfitted).
fit(X: Tensor, _y?: Tensor): thisCompute the median and quantile range of each feature.
transform(X: Tensor): TensorRemove the median and divide by the quantile range.
fitTransform(X: Tensor, _y?: Tensor): TensorFit to X, then transform it.
inverseTransform(X: Tensor): TensorUndo the scaling:
x * scale + center.getParams(): Record<string, unknown>Constructor options of this scaler.
setParams(params: Record<string, unknown>): thisChange options.
clone(): RobustScalerCreate an unfitted scaler with the same options.
StandardScaler
Standardize features by removing the mean and scaling to unit variance.
Formula: z = (x - μ) / σ, with σ the population standard deviation (divisor n, as in scikit-learn).
Features with zero variance are only centered; their scale is treated as 1. Statistics are accumulated in a numerically stable way, so StandardScaler.partialFit can be called on successive batches and gives the same result as a single fit on all rows.
Fitted attributes (read-only getters): mean, variance, scale, nSamplesSeen, nFeaturesIn.
import { StandardScaler } from 'deepbox/preprocess';import { tensor } from 'deepbox/ndarray'; const X = tensor([[1, 2], [3, 4], [5, 6]]);const scaler = new StandardScaler();const XScaled = scaler.fitTransform(X);const XBack = scaler.inverseTransform(XScaled);Members (14)
constructor(options?: { withMean?: boolean; withStd?: boolean; copy?: boolean; })Creates a new StandardScaler.
mean: Tensor | undefinedPer-feature mean seen during fitting, or
undefinedif unfitted orwithMeanis false.variance: Tensor | undefinedPer-feature population variance, or
undefinedif unfitted orwithStdis false.scale: Tensor | undefinedPer-feature scale (standard deviation, 1 for constant features), or
undefinedif unfitted orwithStdis false.nSamplesSeen: numberNumber of samples seen so far (0 when unfitted).
nFeaturesIn: numberNumber of features seen during fitting (0 when unfitted).
fit(X: Tensor, _y?: Tensor): thisCompute the mean and standard deviation of each feature, discarding any previous fit.
partialFit(X: Tensor, _y?: Tensor): thisUpdate the running mean and variance with another batch of samples.
transform(X: Tensor): TensorCenter and scale X with the fitted statistics.
fitTransform(X: Tensor, _y?: Tensor): TensorFit to X, then transform it.
inverseTransform(X: Tensor): TensorUndo the scaling:
x * scale + mean.getParams(): Record<string, unknown>Constructor options of this scaler.
setParams(params: Record<string, unknown>): thisChange options.
clone(): StandardScalerCreate an unfitted scaler with the same options.
import { MinMaxScaler, PowerTransformer, RobustScaler, StandardScaler,} from "deepbox/preprocess";import { tensor } from "deepbox/ndarray"; const train = tensor([[1, 10], [2, 20], [3, 30]]);const test = tensor([[4, 40]]); const scaler = new StandardScaler().fit(train);console.log(scaler.mean?.toString(), scaler.scale?.toString());console.log(scaler.transform(test).toString()); // uses the training statisticsconsole.log(scaler.inverseTransform(scaler.transform(train)).toString()); console.log(new MinMaxScaler().fitTransform(train).toString());console.log(new RobustScaler().fitTransform(train).toString());console.log(new PowerTransformer({ standardize: true }).fitTransform(train).toString());Found a mistake? Open an issue.