Skip to content

Preprocessing knnimputer fit

github-actions[bot] edited this page Sep 23, 2026 · 1 revision

Development build. This page describes main, not a released package. The latest published Lodestar.Preprocessing is 0.1.0 — read its documentation.

HomePreprocessingEncoding and imputation

KnnImputer.Fit

Keeps the fitted rows, which are the donors every later call draws from.

public static KnnImputer Fit(ReadOnlySpan<double> samples, int featureCount, KnnImputerOptions options = null)

Parameterssamples is the matrix, row-major, featureCount values per row, where a NaN is a missing value; the span is read, never modified. options chooses how many donors to average and how to weight them; null takes the reference's defaults, five donors weighted equally.

Returns — a fitted KnnImputer.

ExceptionsArgumentOutOfRangeException when featureCount is not positive or the neighbour count is below one. ArgumentException when samples holds no row, a partial one, or an infinity.

Example — a feature missing from every row is dropped rather than filled.

using Lodestar.Preprocessing;

double[] rows =
[
    1.0, double.NaN, 2.0,
    3.0, double.NaN, 4.0,
];

KnnImputer imputer = KnnImputer.Fit(rows, featureCount: 3);

int width = imputer.OutputFeatureCount;                    // => 2
string kept = string.Join(",", imputer.KeptFeatures);      // => 0,2

Remarks — the fit keeps the rows, it does not summarise them. Every donor is held, so the imputer's memory is the fitted matrix itself and the work all happens in Transform. That is the reference's design too, and it is what makes the cost a product of both row counts rather than of one.

Dropping an all-missing feature is the reference's keep_empty_features=False, its default: there is no donor to supply a value and no column statistic to fall back on, so the column is removed from the output rather than filled with a fiction. KeptFeatures is how a caller maps a filled row back onto the original feature indices.

An infinity is refused where a NaN is not. A NaN is the missing marker; an infinity is a value that would make every distance through it infinite, so it is rejected at the door rather than silently deciding which donors are nearest.

Applies to — net10.0, netstandard2.0.

See alsoKnnImputer.Transform, KnnImputerOptions.

Clone this wiki locally