Class: Uranai::LinearRegression
- Inherits:
-
Object
- Object
- Uranai::LinearRegression
- Defined in:
- lib/uranai/linear_regression.rb
Instance Method Summary collapse
- #compute_cost(theta: default_theta) ⇒ Object
- #fitting_params_with_gradient_descent(options = {}) ⇒ Object (also: #fitting_params)
- #fitting_params_with_normal_equations ⇒ Object
-
#initialize(csv) ⇒ LinearRegression
constructor
A new instance of LinearRegression.
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( = {}) .keep_if { |k,_| [:alpha, :num_iters, :theta, :normalize].include?(k) } FittingParameter.new(gradient_descent()) end |
#fitting_params_with_normal_equations ⇒ Object
25 26 27 |
# File 'lib/uranai/linear_regression.rb', line 25 def fitting_params_with_normal_equations FittingParameter.new(normal_equations) end |