Sha256: b538c4d08f1a6da59a664777abb531420511544d5ac32580b30ea63f532503d2

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module Eps
  class Model
    def initialize(data = nil, y = nil, estimator: nil, **options)
      if estimator
        @estimator = estimator
      elsif data
        train(data, y, **options)
      end
    end

    # pmml

    def self.load_pmml(data)
      if data.is_a?(String)
        data = Nokogiri::XML(data) { |config| config.strict }
      end

      estimator_class =
        if data.css("Segmentation").any?
          Eps::LightGBM
        elsif data.css("RegressionModel").any?
          Eps::LinearRegression
        elsif data.css("NaiveBayesModel").any?
          Eps::NaiveBayes
        else
          raise "Unknown model"
        end

      new(estimator: estimator_class.load_pmml(data))
    end

    private

    def train(data, y = nil, target: nil, algorithm: :lightgbm, **options)
      estimator_class =
        case algorithm
        when :lightgbm
          Eps::LightGBM
        when :linear_regression
          Eps::LinearRegression
        when :naive_bayes
          Eps::NaiveBayes
        else
          raise ArgumentError, "Unknown algorithm: #{algorithm}"
        end

      @estimator = estimator_class.new(data, y, target: target, **options)
    end

    def respond_to_missing?(name, include_private = false)
      if @estimator
        @estimator.respond_to?(name, include_private)
      else
        super
      end
    end

    def method_missing(method, *args, &block)
      if @estimator && @estimator.respond_to?(method)
        @estimator.public_send(method, *args, &block)
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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