lib/eps/model.rb in eps-0.2.1 vs lib/eps/model.rb in eps-0.3.0

- old
+ new

@@ -1,86 +1,52 @@ module Eps class Model - def initialize(data = nil, y = nil, target: nil, estimator: nil, **options) - @options = options - + def initialize(data = nil, y = nil, estimator: nil, **options) if estimator @estimator = estimator - elsif data # legacy - train(data, y, target: target) + elsif data + train(data, y, **options) end end # pmml def self.load_pmml(data) if data.is_a?(String) - require "nokogiri" data = Nokogiri::XML(data) { |config| config.strict } end estimator_class = - if data.css("RegressionModel").any? + 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 - # ruby - legacy - - def self.load(data) - new(estimator: Eps::LinearRegression.load(data)) - end - - # json - legacy - - def self.load_json(data) - new(estimator: Eps::LinearRegression.load_json(data)) - end - - def to_json - @estimator ? @estimator.to_json : super - end - - # pfa - legacy - - def self.load_pfa(data) - new(estimator: Eps::LinearRegression.load_pfa(data)) - end - - # metrics - - def self.metrics(actual, estimated) - estimator_class = - if numeric?(actual) - Eps::LinearRegression - else - Eps::NaiveBayes - end - - estimator_class.metrics(actual, estimated) - end - private - def train(data, y = nil, target: nil) - y ||= daru?(data) ? data[target].to_a : data.map { |r| r[target] } - + def train(data, y = nil, target: nil, algorithm: :lightgbm, **options) estimator_class = - if self.class.numeric?(y) + case algorithm + when :lightgbm + Eps::LightGBM + when :linear_regression Eps::LinearRegression - else + when :naive_bayes Eps::NaiveBayes + else + raise ArgumentError, "Unknown algorithm: #{algorithm}" end - @estimator = estimator_class.new(**@options) - @estimator.train(data, y, target: target) + @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) @@ -88,21 +54,13 @@ super end end def method_missing(method, *args, &block) - if @estimator + if @estimator && @estimator.respond_to?(method) @estimator.public_send(method, *args, &block) else super end - end - - def self.numeric?(y) - y.first.is_a?(Numeric) - end - - def daru?(x) - defined?(Daru) && x.is_a?(Daru::DataFrame) end end end