Class: Uranai::LinearRegression

Inherits:
Object
  • Object
show all
Defined in:
lib/uranai/linear_regression.rb

Instance Method Summary collapse

Constructor Details

#initialize(csv) ⇒ LinearRegression

Returns a new instance of LinearRegression.



7
8
9
# File 'lib/uranai/linear_regression.rb', line 7

def initialize(csv)
  @data = Uranai::Data.new(csv)
end

Instance Method Details

#compute_cost(theta: default_theta) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/uranai/linear_regression.rb', line 11

def compute_cost(theta: default_theta)
  matrix_theta = matrix_theta(theta)
  ((matrix_x * matrix_theta) - matrix_y)
    .map { |element| element ** 2 }
    .inject(0.0) { |sum,i| sum + i }
    ./ (2 * data.training_example_size)
end

#fitting_params_with_gradient_descent(options = {}) ⇒ Object Also known as: fitting_params



19
20
21
22
# File 'lib/uranai/linear_regression.rb', line 19

def fitting_params_with_gradient_descent(options = {})
  options.keep_if { |k,_| [:alpha, :num_iters, :theta, :normalize].include?(k) }
  FittingParameter.new(gradient_descent(options))
end

#fitting_params_with_normal_equationsObject



25
26
27
# File 'lib/uranai/linear_regression.rb', line 25

def fitting_params_with_normal_equations
  FittingParameter.new(normal_equations)
end