Class: PMMLConsumer::RegressionModel

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

Instance Attribute Summary

Attributes inherited from MLModel

#fields_type, #model_node

Instance Method Summary collapse

Methods inherited from MLModel

#cast_input, #filter_field, #initialize, #target_fields

Constructor Details

This class inherits a constructor from PMMLConsumer::MLModel

Instance Method Details

#cauchit(result) ⇒ Object



85
86
87
88
89
90
# File 'lib/pmml_consumer/regression_model.rb', line 85

def cauchit(result)
  raise "error" if result.count == 1
  result.each do |k, v|
    result[k] = 0.5 + (1 / Math::PI) * Math.atan(v)
  end
end

#cloglog(result) ⇒ Object



71
72
73
74
75
76
# File 'lib/pmml_consumer/regression_model.rb', line 71

def cloglog(result)
  raise "error" if result.count == 1
  result.each do |k, v|
    result[k] = 1 - Math.exp(-Math.exp(v))
  end
end

#exp(result) ⇒ Object



92
93
94
95
96
# File 'lib/pmml_consumer/regression_model.rb', line 92

def exp(result)
  result.each do |k, v|
    result[k] = Math.exp(v)
  end
end

#logit(result) ⇒ Object



65
66
67
68
69
# File 'lib/pmml_consumer/regression_model.rb', line 65

def logit(result)
  result.each do |k, v|
    result[k] = 1 / (1 + Math.exp(-v))
  end
end

#loglog(result) ⇒ Object



78
79
80
81
82
83
# File 'lib/pmml_consumer/regression_model.rb', line 78

def loglog(result)
  raise "error" if result.count == 1
  result.each do |k, v|
    result[k] = Math.exp(-Math.exp(-v))
  end
end

#predict(input) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pmml_consumer/regression_model.rb', line 4

def predict(input)
  cast_input(input)
  res = @model_node.xpath("xmlns:RegressionTable").each_with_object({}) do |regression_table, result|
    res_name = regression_table["targetCategory"]
    res_name ||= @model_node["targetFieldName"]
    res_name ||= @target_fields.first
    result[res_name] = regression_table["intercept"].to_f
    regression_table.element_children.each do |predictor|
      case predictor.name
      when "NumericPredictor"
        coefficient = predictor["coefficient"].to_f
        exponent = (predictor["exponent"] || "1").to_i
        result[res_name] += coefficient * (input[predictor["name"]]**exponent)
      when "CategoricalPredictor"
        if predictor["value"] == input[predictor["name"]]
          result[res_name] += predictor["coefficient"].to_f
        end
      when "PredictorTerm"
        coefficient = predictor["coefficient"].to_f
        predictor.element_children.each do |field_ref|
          coefficient *= input[field_ref["field"]]
        end
        result[res_name] += coefficient
      else
        raise "unknow predictor name: #{predictor.name}"
      end
    end
  end
  case @model_node["normalisationMethod"]
  when "softmax"
    softmax(res)
  when "logit"
    logit(res)
  when "probit"
    probit(res)
  when "cloglog"
    cloglog(res)
  when "loglog"
    loglog(res)
  when "cauchit"
    cauchit(res)
  when "exp"
    exp(res)
  when nil, "none"
  else
    raise "unknow normalisation method: #{@model_node["normalisationMethod"]}"
  end
  res
end

#softmax(result) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/pmml_consumer/regression_model.rb', line 54

def softmax(result)
  if result.count == 1
    1 / (1 + Math.exp(-result.values.first))
  else
    sum1n = result.values.reduce(:+)
    result.each do |k, v|
      result[k] = Math.exp(v) / sum1n
    end
  end
end