Table of Contents

Class SimpleImputer<T>

Namespace
AiDotNet.Preprocessing.Imputers
Assembly
AiDotNet.dll

Imputes missing values using simple strategies like mean, median, or constant.

public class SimpleImputer<T> : TransformerBase<T, Matrix<T>, Matrix<T>>, IDataTransformer<T, Matrix<T>, Matrix<T>>

Type Parameters

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
SimpleImputer<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
Inherited Members

Remarks

SimpleImputer fills in missing values (represented as NaN) using simple strategies: - Mean: Replace with column mean - Median: Replace with column median - MostFrequent: Replace with most common value - Constant: Replace with a specified value

For Beginners: This transformer fills in gaps in your data: - If you have missing ages, replace them with average age (Mean) - If you have missing incomes with outliers, use median income (Median) - If you have missing categories, use most common category (MostFrequent) - Or fill with a specific value like 0 or -1 (Constant)

Example with Mean strategy: [1, 2, NaN, 4, 5] → [1, 2, 3, 4, 5] (NaN replaced with mean=3)

Constructors

SimpleImputer(ImputationStrategy, T?, int[]?)

Creates a new instance of SimpleImputer<T>.

public SimpleImputer(ImputationStrategy strategy = ImputationStrategy.Mean, T? fillValue = default, int[]? columnIndices = null)

Parameters

strategy ImputationStrategy

The imputation strategy. Defaults to Mean.

fillValue T

The fill value for Constant strategy. Ignored for other strategies.

columnIndices int[]

The column indices to impute, or null for all columns.

Properties

FillValue

Gets the fill value used for Constant strategy.

public T? FillValue { get; }

Property Value

T

MissingValue

Gets the value considered as missing.

public T MissingValue { get; }

Property Value

T

Statistics

Gets the computed statistics for each feature.

public Vector<T>? Statistics { get; }

Property Value

Vector<T>

Strategy

Gets the imputation strategy used.

public ImputationStrategy Strategy { get; }

Property Value

ImputationStrategy

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

Remarks

Inverse transform is not supported because we don't know which values were originally missing.

Methods

FitCore(Matrix<T>)

Computes the statistics for each feature from the training data.

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<T>

The training data matrix where each column is a feature.

GetFeatureNamesOut(string[]?)

Gets the output feature names after transformation.

public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)

Parameters

inputFeatureNames string[]

The input feature names.

Returns

string[]

The same feature names (SimpleImputer doesn't change number of features).

InverseTransformCore(Matrix<T>)

Inverse transformation is not supported for imputation.

protected override Matrix<T> InverseTransformCore(Matrix<T> data)

Parameters

data Matrix<T>

The imputed data.

Returns

Matrix<T>

Never returns - always throws.

Exceptions

NotSupportedException

Always thrown because we don't track which values were missing.

TransformCore(Matrix<T>)

Transforms the data by imputing missing values.

protected override Matrix<T> TransformCore(Matrix<T> data)

Parameters

data Matrix<T>

The data to transform.

Returns

Matrix<T>

The data with missing values imputed.