Class: Eps::LabelEncoder
- Inherits:
-
Object
- Object
- Eps::LabelEncoder
- Defined in:
- lib/eps/label_encoder.rb
Instance Attribute Summary collapse
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
Instance Method Summary collapse
- #fit(y) ⇒ Object
- #fit_transform(y) ⇒ Object
-
#initialize ⇒ LabelEncoder
constructor
A new instance of LabelEncoder.
- #inverse_transform(y) ⇒ Object
- #transform(y) ⇒ Object
Constructor Details
#initialize ⇒ LabelEncoder
Returns a new instance of LabelEncoder.
5 6 7 |
# File 'lib/eps/label_encoder.rb', line 5 def initialize @labels = {} end |
Instance Attribute Details
#labels ⇒ Object (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
38 39 40 41 42 43 |
# File 'lib/eps/label_encoder.rb', line 38 def inverse_transform(y) inverse = @labels.map(&:reverse).to_h y.map do |yi| inverse[yi.to_i] end end |
#transform(y) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/eps/label_encoder.rb', line 22 def transform(y) y.map do |yi| if yi.nil? nil else # use an additional label for unseen values # this is only used during training for the LightGBM eval_set # LightGBM ignores them (only uses seen categories for predictions) # https://github.com/microsoft/LightGBM/issues/1936 # the evaluator also ignores them (to be consistent with LightGBM) # but doesn't use this code @labels[yi.to_s] || @labels.size end end end |