Sha256: 387384d330ffd6379e57b7567e906369b83cde438f383cbd3ee58f6d89147571

Contents?: true

Size: 695 Bytes

Versions: 1

Compression:

Stored size: 695 Bytes

Contents

module Eps
  class LabelEncoder
    attr_reader :labels

    def initialize
      @labels = {}
    end

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

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

    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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eps-0.3.0 lib/eps/label_encoder.rb