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
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
SimpleImputer<T>
- Implements
- 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
strategyImputationStrategyThe imputation strategy. Defaults to Mean.
fillValueTThe fill value for Constant strategy. Ignored for other strategies.
columnIndicesint[]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
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
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
dataMatrix<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
inputFeatureNamesstring[]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
dataMatrix<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
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The data with missing values imputed.