Class: Eps::LabelEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/eps/label_encoder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLabelEncoder

Returns a new instance of LabelEncoder.



5
6
7
# File 'lib/eps/label_encoder.rb', line 5

def initialize
  @labels = {}
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.



3
4
5
# File 'lib/eps/label_encoder.rb', line 3

def labels
  @labels
end

Instance Method Details

#fit(y) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/eps/label_encoder.rb', line 9

def fit(y)
  labels = {}
  y.compact.map(&:to_s).uniq.sort.each_with_index do |label, i|
    labels[label] = i
  end
  @labels = labels
end

#fit_transform(y) ⇒ Object



17
18
19
20
# File 'lib/eps/label_encoder.rb', line 17

def fit_transform(y)
  fit(y)
  transform(y)
end

#inverse_transform(y) ⇒ Object



34
35
36
37
38
39
# File 'lib/eps/label_encoder.rb', line 34

def inverse_transform(y)
  inverse = Hash[@labels.map(&:reverse)]
  y.map do |yi|
    inverse[yi.to_i]
  end
end

#transform(y) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eps/label_encoder.rb', line 22

def transform(y)
  y.map do |yi|
    if yi.nil?
      nil
    else
      v = @labels[yi.to_s]
      raise "Unknown label: #{yi}" unless v
      v
    end
  end
end