View Source Scholar.Preprocessing.OrdinalEncoder (Scholar v0.3.1)

Implements encoder that converts integer value (substitute of categorical data in tensors) into other integer value. The values assigned starts from 0 and go up to num_classes - 1.They are maintained in sorted manner. This means that for x < y => encoded_value(x) < encoded_value(y).

Currently the module supports only 1D tensors.

Summary

Functions

Fit the ordinal encoder to provided data. The labels are assigned in a sorted manner.

Apply encoding on the provided tensor directly. It's equivalent to fit/2 and then transform/2 on the same data.

Encodes a tensor's values into integers from range 0 to :num_classes - 1 or -1 if the value did not occur in fitting process.

Functions

Fit the ordinal encoder to provided data. The labels are assigned in a sorted manner.

Options

  • :num_classes (pos_integer/0) - Required. Number of classes to be encoded.

Examples

iex> t = Nx.tensor([3, 2, 4, 56, 2, 4, 2])
iex> Scholar.Preprocessing.OrdinalEncoder.fit(t, num_classes: 4)
%Scholar.Preprocessing.OrdinalEncoder{
  encoding_tensor: Nx.tensor(
    [
      [0, 2],
      [1, 3],
      [2, 4],
      [3, 56]
    ]
  )
}
Link to this function

fit_transform(tensor, opts \\ [])

View Source

Apply encoding on the provided tensor directly. It's equivalent to fit/2 and then transform/2 on the same data.

Examples

iex> t = Nx.tensor([3, 2, 4, 56, 2, 4, 2])
iex> Scholar.Preprocessing.OridinalEncoder.fit_transform(t, num_classes: 4)
#Nx.Tensor<
  s64[7]
  [1, 0, 2, 3, 0, 2, 0]
>

Encodes a tensor's values into integers from range 0 to :num_classes - 1 or -1 if the value did not occur in fitting process.

Examples

iex> t = Nx.tensor([3, 2, 4, 56, 2, 4, 2])
iex> enoder = Scholar.Preprocessing.OrdinalEncoder.fit(t, num_classes: 4)
iex> Scholar.Preprocessing.OrdinalEncoder.transform(enoder, t)
#Nx.Tensor<
  s64[7]
  [1, 0, 2, 3, 0, 2, 0]
>

iex> t = Nx.tensor([3, 2, 4, 56, 2, 4, 2])
iex> enoder = Scholar.Preprocessing.OrdinalEncoder.fit(t, num_classes: 4)
iex> new_tensor = Nx.tensor([2, 3, 4, 5, 4, 56, 2])
iex> Scholar.Preprocessing.OrdinalEncoder.transform(enoder, new_tensor)
#Nx.Tensor<
  s64[7]
  [0, 1, 2, -1, 2, 3, 0]
>