Preprocessing standardscaler transform - CyrilB1531/lodestar GitHub Wiki
Development build. This page describes
main, not a released package. The latest published Lodestar.Preprocessing is 0.1.0 — read its documentation.
Home › Preprocessing › Feature scaling
Standardises a row-major sample matrix with the fitted statistics.
public double[] Transform(ReadOnlySpan<double> samples)public CsrMatrix Transform(CsrMatrix samples)The second overload takes a CsrMatrix and returns a new one storing the same positions, each value divided by its column's Scale, or copied unchanged when the scaler does not scale: a zero stays a zero, so nothing absent becomes stored.
Parameters — samples is the matrix to transform, row-major, with FeatureCount values per
row. It need not be the matrix the scaler was fitted on.
Returns — a new array of the same length. The input is never written to.
Exceptions — ArgumentException when samples holds no row, a partial one, or a non-finite value.
The sparse overload throws ArgumentNullException when samples is null, ArgumentException when it holds no row, stores a non-finite value, or its column count is not FeatureCount, and InvalidOperationException when the scaler centres — fit it with WithMean = false, since subtracting a centre would make every absent zero a stored value.
Example — fit on training rows, apply to unseen ones.
using Lodestar.Preprocessing;
double[] training = [1.0, 10.0, 2.0, 10.0, 4.0, 10.0];
StandardScaler scaler = StandardScaler.Fit(training, featureCount: 2);
// A row the scaler never saw, standardised with the training statistics.
double[] unseen = scaler.Transform([3.0, 10.0]);
double centred = unseen[0]; // => 0.5345224838248487
double constant = unseen[1]; // => 0Example — the sparse overload, on a matrix whose second column stores nothing.
using Lodestar.Abstractions;
using Lodestar.Preprocessing;
// Three rows, two columns: 2 and -4 in the first column, nothing in the second.
var matrix = new CsrMatrix(3, 2, [2.0, -4.0], [0, 0], [0, 1, 1, 2]);
StandardScaler scaler = StandardScaler.Fit(matrix);
CsrMatrix scaled = scaler.Transform(matrix);
int stored = scaled.NonZeroCount; // => 2
int[] columns = scaled.ColumnIndices; // same positions as the inputRemarks — the statistics come from the fit and are not recomputed here, which is the whole point of the two-step shape: a validation set standardised with its own mean has been told something about itself that the training set did not know.
What the two steps do is decided by the options, not by which statistics exist. With
WithMean = false the scaler still carries a Mean — see StandardScaler's
table — and this method still does not subtract it.
A feature whose Scale was forced to 1 comes through centred but unscaled. That is the intended
outcome: its spread was below what the variance computation could resolve, so there is nothing to
divide by that would not be noise.
Applies to — net10.0, netstandard2.0.
See also — StandardScaler.Fit,
StandardScaler.InverseTransform.