Scholar.Impute.SimpleImputer (Scholar v0.4.1)

Copy Markdown View Source

Univariate imputer for completing missing values with simple strategies.

Summary

Functions

Univariate imputer for completing missing values with simple strategies.

Impute all missing values in x using fitted imputer.

Functions

fit(x, opts \\ [])

Univariate imputer for completing missing values with simple strategies.

Options

  • :missing_values - The placeholder for the missing values. All occurrences of :missing_values will be imputed.

    The default value expects there are no NaNs in the input tensor.

    The default value is :nan.

  • :strategy - The imputation strategy.

    • :mean - replace missing values using the mean along each column.

    • :median - replace missing values using the median along each column.

    • :mode - replace missing using the most frequent value along each column. If there is more than one such value, only the smallest is returned.

    • :constant - replace missing values with :fill_value.

    The default value is :mean.

  • :fill_value - When strategy is set to :constant, :fill_value is used to replace all occurrences of :missing_values. The default value is 0.0.

Return Values

The function returns a struct with the following parameters:

  • :missing_values - the same value as in :missing_values

  • :statistics - The imputation fill value for each feature. Computing statistics can result in Nx.Constant.nan/0 values.

Examples

iex> x = Nx.tensor([[1, 2, :nan], [3, 7, :nan], [:nan, 4, 5]])
iex> Scholar.Impute.SimpleImputer.fit(x, strategy: :mean)
%Scholar.Impute.SimpleImputer{
  statistics: Nx.tensor(
    [2.0, 4.333333492279053, 5.0]
  ),
  missing_values: :nan
}

transform(simple_imputer, x)

Impute all missing values in x using fitted imputer.

Return Values

The function returns input tensor with NaN replaced with values saved in fitted imputer.

Examples

iex> x = Nx.tensor([[1, 2, :nan], [3, 7, :nan], [:nan, 4, 5]])
iex> imputer = Scholar.Impute.SimpleImputer.fit(x, strategy: :mean)
iex> Scholar.Impute.SimpleImputer.transform(imputer, x)
Nx.tensor(
  [
    [1.0, 2.0, 5.0],
    [3.0, 7.0, 5.0],
    [2.0, 4.0, 5.0]
  ]
)

iex> x = Nx.tensor([[1, 2, :nan], [3, 7, :nan], [:nan, 4, 5]])
iex> y = Nx.tensor([[7, :nan, 6], [6, 9, :nan], [8, :nan, 1]])
iex> imputer = Scholar.Impute.SimpleImputer.fit(x, strategy: :median)
iex> Scholar.Impute.SimpleImputer.transform(imputer, y)
Nx.tensor(
  [
    [7.0, 4.0, 6.0],
    [6.0, 9.0, 5.0],
    [8.0, 4.0, 1.0]
  ]
)