Class: Rumale::Preprocessing::LabelBinarizer

Inherits:
Base::Estimator show all
Includes:
Base::Transformer
Defined in:
rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb

Overview

Encode labels to binary labels with one-vs-all scheme.

Examples:

require 'rumale/preprocessing/label_binarizer'

encoder = Rumale::Preprocessing::LabelBinarizer.new
label = [0, -1, 3, 3, 1, 1]
p encoder.fit_transform(label)
# Numo::Int32#shape=[6,4]
# [[0, 1, 0, 0],
#  [1, 0, 0, 0],
#  [0, 0, 0, 1],
#  [0, 0, 0, 1],
#  [0, 0, 1, 0],
#  [0, 0, 1, 0]]

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(neg_label: 0, pos_label: 1) ⇒ LabelBinarizer

Create a new encoder for binarizing labels with one-vs-all scheme.

Parameters:

  • neg_label (Integer) (defaults to: 0)

    The value represents negative label.

  • pos_label (Integer) (defaults to: 1)

    The value represents positive label.



34
35
36
37
38
39
40
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 34

def initialize(neg_label: 0, pos_label: 1)
  super()
  @params = {
    neg_label: neg_label,
    pos_label: pos_label
  }
end

Instance Attribute Details

#classesArray (readonly)

Return the class labels.

Returns:

  • (Array)

    (size: [n_classes])



28
29
30
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 28

def classes
  @classes
end

Instance Method Details

#fit(y) ⇒ LabelBinarizer

Fit encoder to labels.

Parameters:

  • y (Numo::NArray/Array)

    (shape: [n_samples]) The labels to fit encoder.

Returns:



47
48
49
50
51
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 47

def fit(y, _not_used = nil)
  y = y.to_a if y.is_a?(Numo::NArray)
  @classes = y.uniq.sort
  self
end

#fit_transform(y) ⇒ Numo::Int32

Fit encoder to labels, then return binarized labels.

Parameters:

  • y (Numo::NArray/Array)

    (shape: [n_samples]) The labels to fit encoder.

Returns:

  • (Numo::Int32)

    (shape: [n_samples, n_classes]) The binarized labels.



58
59
60
61
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 58

def fit_transform(y, _not_used = nil)
  y = y.to_a if y.is_a?(Numo::NArray)
  fit(y).transform(y)
end

#inverse_transform(x) ⇒ Array

Decode binarized labels.

Parameters:

  • x (Numo::Int32)

    (shape: [n_samples, n_classes]) The binarized labels to be decoded.

Returns:

  • (Array)

    (shape: [n_samples]) The decoded labels.



80
81
82
83
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 80

def inverse_transform(x)
  n_samples = x.shape[0]
  Array.new(n_samples) { |n| @classes[x[n, true].ne(@params[:neg_label]).where[0]] }
end

#transform(y) ⇒ Numo::Int32

Encode labels.

Parameters:

  • y (Array)

    (shape: [n_samples]) The labels to be encoded.

Returns:

  • (Numo::Int32)

    (shape: [n_samples, n_classes]) The binarized labels.



67
68
69
70
71
72
73
74
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 67

def transform(y)
  y = y.to_a if y.is_a?(Numo::NArray)
  n_classes = @classes.size
  n_samples = y.size
  codes = Numo::Int32.zeros(n_samples, n_classes) + @params[:neg_label]
  n_samples.times { |n| codes[n, @classes.index(y[n])] = @params[:pos_label] }
  codes
end