class LinearRegression

The linear regression class with customizable number of folds for K-fold cross validation.

Attributes

accuracy[R]
folds[R]
precision[R]
theta[R]

Public Class Methods

new(precision = 3, folds = 5) click to toggle source
# File lib/rubyml/linear_regression.rb, line 11
def initialize(precision = 3, folds = 5)
  @precision = precision
  @epsilon = 2.0
  @folds = folds
end

Public Instance Methods

fit(x, y) click to toggle source
# File lib/rubyml/linear_regression.rb, line 17
def fit(x, y)
  x_mat = bias_trick(x)
  @theta = ((x_mat.t * x_mat).inv * x_mat.t) * y
  @theta = @theta.collect { |e| e.round(@precision) }
end
predict(x) click to toggle source
# File lib/rubyml/linear_regression.rb, line 23
def predict(x)
  x_mat = bias_trick(x)
  (x_mat * @theta).collect { |e| e.round(@precision) }
end
visualize(x, y) click to toggle source
# File lib/rubyml/linear_regression.rb, line 28
def visualize(x, y)
  x = mat_to_array(x)
  y = mat_to_array(y)
  theta = mat_to_array(@theta)
  plot_function(x, y, theta)
end