Qu

fit_scaler

Builtin index · Statistics & ML

result = fit_scaler(X, [method=])

Input: X, method · Output: result

What it is

Fits and stores scaling parameters from an N-row, D-column matrix (or vector, or table) X, for reuse on new data — the fit-once path, as opposed to the four one-shot verbs above. method is a string, one of "standard", "minmax", "robust", "quantile" (default "standard").

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = fit_scaler(X, [method=])
X matrix or vectorFits and stores scaling parameters from an N-row, D-column matrix (or vector, or table) X, for reuse on new data — the fit-once path, as opposed to the four one-shot verbs above.
method string, optionalmethod is a string, one of "standard", "minmax", "robust", "quantile" (default "standard").
result Record
Returns a fitted scaler Record carrying the per-column parameters (e.g. mean/std for "standard") plus .transform/.inverse_transform.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

split = train_test_split(X, y, test_size = 0.25, seed = 1)
scaler = fit_scaler(split.X_train, method = "standard")
print("training column means: {round(scaler.mean, 3)}")

Xtr = scaler.transform(split.X_train)
Xte = scaler.transform(split.X_test)
print("train columns after scaling: {round(mean(Xtr, axis = 0), 3)}")
print("test columns after scaling:  {round(mean(Xte, axis = 0), 3)}")
output
training column means: [-0.23, 0.011, 0]
train columns after scaling: [0, 0, 0]
test columns after scaling:  [0.428, 0.053, -0.046]

from statistics-ml

How it works

fit_scaler(X, method="standard"|"minmax"|"robust"|"quantile") — computes and stores whatever parameters that method needs as named fields on a kind="scaler" Value::Model, for reuse via .transform(newX)/.inverse_transform(newX) on data that may be different from X (e.g. a held-out test set fit on the training set's own distribution) — that reuse is the entire point, newX is never re-fit. Field names mirror what each method's scaler::ColumnParams variant stores (mean/std for "standard", min/max for "minmax", median/iqr for "robust", reference — the sorted fitted values, one column per feature — for "quantile"), plus method itself so .transform/.inverse_transform know which formula to replay.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.