Class: PMMLConsumer::MLModel

Inherits:
Object
  • Object
show all
Defined in:
lib/pmml_consumer/ml_model.rb

Direct Known Subclasses

RegressionModel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_node, field_type) ⇒ MLModel

Returns a new instance of MLModel.



6
7
8
9
10
# File 'lib/pmml_consumer/ml_model.rb', line 6

def initialize(model_node, field_type)
  @model_node = model_node
  @fields_type = filter_field(field_type)
  @target_fields = target_fields
end

Instance Attribute Details

#fields_typeObject

Returns the value of attribute fields_type.



4
5
6
# File 'lib/pmml_consumer/ml_model.rb', line 4

def fields_type
  @fields_type
end

#model_nodeObject

Returns the value of attribute model_node.



4
5
6
# File 'lib/pmml_consumer/ml_model.rb', line 4

def model_node
  @model_node
end

Instance Method Details

#cast_input(input) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pmml_consumer/ml_model.rb', line 16

def cast_input(input)
  @fields_type.each do |field_name, field_type|
    input[field_name] = if input[field_name].nil? && !field_type["missingValueReplacement"].nil?
      field_type["missingValueReplacement"]
    elsif input[field_name].nil? && field_type["missingValueReplacement"].nil?
      raise "value '#{field_name}' not found and no value replacement set"
    else
      case field_type["dataType"]
      when "double"
        input[field_name].to_f
      when "integer"
        input[field_name].to_i
      when "string"
        input[field_name].to_s
      else
        raise "unknow dataType: #{field_type["dataType"]}"
      end
    end
    if field_type["values"].is_a?(Array) && !field_type["values"].include?(input[field_name])
      raise "not a valid value: #{input[field_name]}"
    end
  end
  input
end

#filter_field(field_type) ⇒ Object



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

def filter_field(field_type)
  @model_node.xpath("xmlns:MiningSchema/xmlns:MiningField").each do |mining_field|
    unless mining_field.attributes["usageType"].nil? || mining_field.attributes["usageType"].value == "active"
      field_type.delete(mining_field.attributes["name"].value)
    end
  end
  field_type
end

#predict(input) ⇒ Object



12
13
14
# File 'lib/pmml_consumer/ml_model.rb', line 12

def predict(input)
  raise "not implemented"
end

#target_fieldsObject



50
51
52
53
54
# File 'lib/pmml_consumer/ml_model.rb', line 50

def target_fields
  @model_node.xpath("xmlns:MiningSchema/xmlns:MiningField[@usageType='target']").map do |mining_field|
    mining_field["name"]
  end
end