# `Scholar.Impute.SimpleImputer`
[🔗](https://github.com/elixir-nx/scholar/blob/main/lib/scholar/impute/simple_imputer.ex#L1)

Univariate imputer for completing missing values with simple strategies.

# `fit`
[🔗](https://github.com/elixir-nx/scholar/blob/main/lib/scholar/impute/simple_imputer.ex#L75)

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`](https://hexdocs.pm/nx/Nx.Constants.html#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`
[🔗](https://github.com/elixir-nx/scholar/blob/main/lib/scholar/impute/simple_imputer.ex#L241)

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]
      ]
    )

---

*Consult [api-reference.md](api-reference.md) for complete listing*
