Class: Eps::Evaluators::LinearRegression
- Inherits:
-
Object
- Object
- Eps::Evaluators::LinearRegression
- Defined in:
- lib/eps/evaluators/linear_regression.rb
Instance Attribute Summary collapse
-
#features ⇒ Object
readonly
Returns the value of attribute features.
Instance Method Summary collapse
- #coefficients ⇒ Object
-
#initialize(coefficients:, features:, text_features:) ⇒ LinearRegression
constructor
A new instance of LinearRegression.
- #predict(x, probabilities: false) ⇒ Object
Constructor Details
#initialize(coefficients:, features:, text_features:) ⇒ LinearRegression
Returns a new instance of LinearRegression.
6 7 8 9 10 |
# File 'lib/eps/evaluators/linear_regression.rb', line 6 def initialize(coefficients:, features:, text_features:) @coefficients = coefficients.to_h { |k, v| [k.is_a?(Array) ? [k[0].to_s, k[1]] : k.to_s, v] } @features = features @text_features = text_features || {} end |
Instance Attribute Details
#features ⇒ Object (readonly)
Returns the value of attribute features.
4 5 6 |
# File 'lib/eps/evaluators/linear_regression.rb', line 4 def features @features end |
Instance Method Details
#coefficients ⇒ Object
52 53 54 |
# File 'lib/eps/evaluators/linear_regression.rb', line 52 def coefficients @coefficients.to_h { |k, v| [Array(k).join.to_sym, v] } end |
#predict(x, probabilities: false) ⇒ Object
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 |
# File 'lib/eps/evaluators/linear_regression.rb', line 12 def predict(x, probabilities: false) raise "Probabilities not supported" if probabilities intercept = @coefficients["_intercept"] || 0.0 scores = [intercept] * x.size @features.each do |k, type| raise "Missing data in #{k}" if !x.columns[k] || x.columns[k].any?(&:nil?) case type when "categorical" x.columns[k].each_with_index do |xv, i| # TODO clean up scores[i] += (@coefficients[[k, xv]] || @coefficients[[k, xv.to_s]]).to_f end when "text" encoder = TextEncoder.new(**@text_features[k]) counts = encoder.transform(x.columns[k]) coef = {} @coefficients.each do |k2, v| next unless k2.is_a?(Array) && k2.first == k coef[k2.last] = v end counts.each_with_index do |xc, i| xc.each do |word, count| scores[i] += coef[word] * count if coef[word] end end else coef = @coefficients[k].to_f x.columns[k].each_with_index do |xv, i| scores[i] += coef * xv end end end scores end |