Class: Spark::Mllib::RegressionModel

Inherits:
Object
  • Object
show all
Defined in:
lib/spark/mllib/regression/common.rb

Overview

RegressionModel

A linear model that has a vector of coefficients and an intercept.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weights, intercept) ⇒ RegressionModel

Returns a new instance of RegressionModel.



12
13
14
15
# File 'lib/spark/mllib/regression/common.rb', line 12

def initialize(weights, intercept)
  @weights = Spark::Mllib::Vectors.to_vector(weights)
  @intercept = intercept.to_f
end

Instance Attribute Details

#interceptObject (readonly)

Returns the value of attribute intercept.



10
11
12
# File 'lib/spark/mllib/regression/common.rb', line 10

def intercept
  @intercept
end

#weightsObject (readonly)

Returns the value of attribute weights.



10
11
12
# File 'lib/spark/mllib/regression/common.rb', line 10

def weights
  @weights
end

Instance Method Details

#predict(data) ⇒ Object

Predict the value of the dependent variable given a vector data containing values for the independent variables.

Examples:

lm = RegressionModel.new([1.0, 2.0], 0.1)

lm.predict([-1.03, 7.777]) - 14.624 < 1e-6
# => true

lm.predict(SparseVector.new(2, {0 => -1.03, 1 => 7.777})) - 14.624 < 1e-6
# => true


29
30
31
32
# File 'lib/spark/mllib/regression/common.rb', line 29

def predict(data)
  data = Spark::Mllib::Vectors.to_vector(data)
  @weights.dot(data) + @intercept
end