API Reference / preprocessing / imputation /

simple_imputer.py

SimpleImputer transformer.

Simple imputation strategies for missing values.

Classes

SimpleImputer

class preprocessing.imputation.simple_imputer.SimpleImputer(Transformer)

Impute missing values using simple statistical strategies.

Provides basic univariate strategies for completing missing values, either by using mean/median/mode of columns or a constant value.
Constructor
__init__(
    self,
    strategy: str = 'mean',
    fill_value: Optional[Union[float, str]] = None,
    columns: Optional[List[int]] = None,
)

Parameters

strategy
{"mean", "median", "most_frequent", "constant"} = "mean"

The imputation strategy:

  • "mean": Replace with the average value (numeric only).
  • "median": Replace with the middle value (numeric only).
  • "most_frequent": Replace with the mode (categorical or numeric).
  • "constant": Replace with fill_value.
fill_value
float or str
The value to use when strategy="constant".
columns
list of int
Indices of columns to impute. If None, all columns are processed.

Attributes

statistics_
dict
Mapping of column index to the fill value used for that column.

Notes

Strategies:
  • Mean/Median: Robust to some outliers but can distort the
distribution variance.
  • Most Frequent: Suitable for categorical data.
  • Constant: Useful for marking missingness as a deliberate category
(e.g., "Missing" or -1).

Impute missing values with column means:

python
>>> from tuiml.preprocessing.imputation import SimpleImputer
>>> import numpy as np
>>> X = np.array([[1, 2], [np.nan, 3], [7, 6]])
>>> imputer = SimpleImputer(strategy="mean")
>>> X_imputed = imputer.fit_transform(X)
>>> print(X_imputed[1, 0])
4.0

Methods

get_parameter_schema (cls)
fit (self, X: np.ndarray, y: Optional[np.ndarray]=None, feature_names: Optional[List[str]]=None) -> 'SimpleImputer'
transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str