Class: RubyLinearRegression
- Inherits:
-
Object
- Object
- RubyLinearRegression
- Defined in:
- lib/ruby_linear_regression.rb
Instance Attribute Summary collapse
-
#mu ⇒ Object
readonly
Returns the value of attribute mu.
-
#sigma ⇒ Object
readonly
Returns the value of attribute sigma.
-
#theta ⇒ Object
readonly
Returns the value of attribute theta.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #compute_cost ⇒ Object
-
#initialize ⇒ RubyLinearRegression
constructor
A new instance of RubyLinearRegression.
- #load_training_data(x_data, y_data) ⇒ Object
- #predict(data) ⇒ Object
- #train_normal_equation ⇒ Object
Constructor Details
#initialize ⇒ RubyLinearRegression
Returns a new instance of RubyLinearRegression.
7 8 9 10 |
# File 'lib/ruby_linear_regression.rb', line 7 def initialize @mu = 0 @sigma = 1 end |
Instance Attribute Details
#mu ⇒ Object (readonly)
Returns the value of attribute mu.
5 6 7 |
# File 'lib/ruby_linear_regression.rb', line 5 def mu @mu end |
#sigma ⇒ Object (readonly)
Returns the value of attribute sigma.
5 6 7 |
# File 'lib/ruby_linear_regression.rb', line 5 def sigma @sigma end |
#theta ⇒ Object (readonly)
Returns the value of attribute theta.
5 6 7 |
# File 'lib/ruby_linear_regression.rb', line 5 def theta @theta end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
5 6 7 |
# File 'lib/ruby_linear_regression.rb', line 5 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
5 6 7 |
# File 'lib/ruby_linear_regression.rb', line 5 def y @y end |
Instance Method Details
#compute_cost ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby_linear_regression.rb', line 27 def compute_cost # Compute the mean squared cost / error function # First use matrix multiplication and vector subtracton to find errors errors = (@x * @theta) - @y # Then square all errors errors = errors.map { |e| e * e } # Find the mean of the square errors mean_square_error = 0.5 * (errors.inject{ |sum, e| sum + e }.to_f / errors.row_size) return mean_square_error end |
#load_training_data(x_data, y_data) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_linear_regression.rb', line 12 def load_training_data x_data, y_data # normalize the x_data x_data = normalize_data( x_data ) # add 1 column to our data x_data = x_data.map { |r| [1].concat(r) } # build our x Matrix & y Vector @x = Matrix.rows( x_data ) @y = Matrix.rows( y_data.collect { |e| [e] } ) @theta = Matrix[[0],[0]] end |
#predict(data) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ruby_linear_regression.rb', line 49 def predict data # normalize data.each_index do |i| data[i] = (data[i] - @mu[i]) / @sigma[i].to_f end # add 1 column to prediction data data = [1].concat( data ) # perform prediction prediction = (Matrix[data] * @theta)[0,0].to_f return prediction end |
#train_normal_equation ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ruby_linear_regression.rb', line 41 def train_normal_equation # Calculate the optimal theta using the normal equation # theta = ( X' * X )^1 * X' * y @theta = (@x.transpose * @x).inverse * @x.transpose * @y return @theta end |