Class: Rumale::Preprocessing::LabelEncoder

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Transformer
Defined in:
lib/rumale/preprocessing/label_encoder.rb

Overview

Encode labels to values between 0 and n_classes - 1.

Examples:

encoder = Rumale::Preprocessing::LabelEncoder.new
labels = Numo::Int32[1, 8, 8, 15, 0]
encoded_labels = encoder.fit_transform(labels)
# > pp encoded_labels
# Numo::Int32#shape=[5]
# [1, 2, 2, 3, 0]
decoded_labels = encoder.inverse_transform(encoded_labels)
# > pp decoded_labels
# [1, 8, 8, 15, 0]

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#params

Instance Method Summary collapse

Constructor Details

#initializeLabelEncoder

Create a new encoder for encoding labels to values between 0 and n_classes - 1.



29
30
31
32
# File 'lib/rumale/preprocessing/label_encoder.rb', line 29

def initialize
  @params = {}
  @classes = nil
end

Instance Attribute Details

#classesArray (readonly)

Return the class labels.

Returns:

  • (Array)

    (size: [n_classes])



26
27
28
# File 'lib/rumale/preprocessing/label_encoder.rb', line 26

def classes
  @classes
end

Instance Method Details

#fit(x) ⇒ LabelEncoder

Fit label-encoder to labels.

Parameters:

  • x (Array)

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

Returns:



40
41
42
43
44
45
# File 'lib/rumale/preprocessing/label_encoder.rb', line 40

def fit(x, _y = nil)
  x = x.to_a if x.is_a?(Numo::NArray)
  check_params_type(Array, x: x)
  @classes = x.sort.uniq
  self
end

#fit_transform(x) ⇒ Numo::Int32

Fit label-encoder to labels, then return encoded labels.

Parameters:

  • x (Array)

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

Returns:

  • (Numo::Int32)

    The encoded labels.



53
54
55
56
57
# File 'lib/rumale/preprocessing/label_encoder.rb', line 53

def fit_transform(x, _y = nil)
  x = x.to_a if x.is_a?(Numo::NArray)
  check_params_type(Array, x: x)
  fit(x).transform(x)
end

#inverse_transform(x) ⇒ Array

Decode encoded labels.

Parameters:

  • x (Numo::Int32)

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

Returns:

  • (Array)

    The decoded labels.



73
74
75
76
# File 'lib/rumale/preprocessing/label_encoder.rb', line 73

def inverse_transform(x)
  check_label_array(x)
  x.to_a.map { |n| @classes[n] }
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about LabelEncoder



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

def marshal_dump
  { params: @params,
    classes: @classes }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


87
88
89
90
91
# File 'lib/rumale/preprocessing/label_encoder.rb', line 87

def marshal_load(obj)
  @params = obj[:params]
  @classes = obj[:classes]
  nil
end

#transform(x) ⇒ Numo::Int32

Encode labels.

Parameters:

  • x (Array)

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

Returns:

  • (Numo::Int32)

    The encoded labels.



63
64
65
66
67
# File 'lib/rumale/preprocessing/label_encoder.rb', line 63

def transform(x)
  x = x.to_a if x.is_a?(Numo::NArray)
  check_params_type(Array, x: x)
  Numo::Int32[*(x.map { |v| @classes.index(v) })]
end