Module: PMMLConsumer

Defined in:
lib/pmml_consumer.rb,
lib/pmml_consumer/version.rb,
lib/pmml_consumer/ml_model.rb,
lib/pmml_consumer/regression_model.rb

Defined Under Namespace

Classes: MLModel, RegressionModel

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.field_type(xml) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pmml_consumer.rb', line 26

def self.field_type(xml)
  xml.xpath("/xmlns:PMML/xmlns:DataDictionary/xmlns:DataField").each_with_object({}) do |child, memo|
    memo[child.attributes["name"].value] = case child.attributes["optype"].value
    when "continuous"
      child.to_h.reject! { |key| %w(name optype).include?(key) }
    when "categorical"
      data_field_h = child.to_h.reject! { |key| %w(name optype).include?(key) }
      data_field_h["values"] = child.xpath("xmlns:Value").map do |cat|
        cat.attributes["value"].value
      end
      data_field_h
    else
      raise "unknow optype: #{child.attributes['optype'].value}"
    end
  end
end

.load(pmml_string, model_name: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pmml_consumer.rb', line 13

def self.load(pmml_string, model_name: nil)
  xml = Nokogiri::XML(pmml_string)

  ml_model = model_node(xml, model_name)

  case ml_model.name
  when 'RegressionModel'
    RegressionModel.new(ml_model, field_type(xml))
  else
    MLModel.new(ml_model, field_type(xml))
  end
end

.model_node(xml, model_name) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pmml_consumer.rb', line 43

def self.model_node(xml, model_name)
  if model_name.nil?
    xml.xpath("/xmlns:PMML/*[contains(name(), 'Model') or (name() = 'NeuralNetwork') or (name() = 'Scorecard')]").first
  else
    xml.xpath("/xmlns:PMML/*[@modelName='#{model_name}']").first
  end
end