ruby_linear_regression 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby_linear_regression.rb +32 -1
  3. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fdd3245c2d76d40b4e6a1992644360e0cab3d74
4
- data.tar.gz: 3c02457473a0695ac8eae4803a8b04799e8ba4b0
3
+ metadata.gz: cfebab034391389c2a317a1ab52e38657c7fa02b
4
+ data.tar.gz: 2846a47266a55f14e0c9ee42a61d36a9c40ac882
5
5
  SHA512:
6
- metadata.gz: bea431b65d547919c489aa894d34a20a4d70b41733c9eedc32fcd6447d8feb4cd951c084f12167281e88a442d99ba799b969437a732327f7fc5ee132eb4954bf
7
- data.tar.gz: e5242100d48114b33da5ec3e729f2b554df7258a7738583440233d85030b00a7771e7946b27f55a9b58a725e29655c3659a27ef80264bec66c49e4fbc88a24e7
6
+ metadata.gz: 88db7a42087da42fcc74cfd8363625269aace47110e7383a2b78add3756ee7599b648331186e583502d506eaa698f2e67006cda949217bbb19ba01dfe748dc7b
7
+ data.tar.gz: cbd925a7eb8f9a1f721c80c37ededd062f9ec885f353ab1425101ec481c268c534efa35958b7692321c3b96fc82dbd506b7093590ff7d987b6b3e5b9c6ac35aa
@@ -26,7 +26,7 @@ class RubyLinearRegression
26
26
  @x = Matrix.rows( x_data )
27
27
  @y = Matrix.rows( y_data.collect { |e| [e] } )
28
28
 
29
- @theta = Matrix[[0],[0]]
29
+ @theta = Matrix.zero(@x.column_count, 1)
30
30
  end
31
31
 
32
32
  # Compute the mean squared cost / error function
@@ -52,6 +52,26 @@ class RubyLinearRegression
52
52
  return @theta
53
53
  end
54
54
 
55
+ # Calculate optimal theta using gradient descent
56
+ # Arguments:
57
+ # alpha: Learning rate
58
+ # iterations: Number of iterations to run gradient descent
59
+ # verbose: If true will output cost after each iteration, can be used to find optimal learning rate (alpha) and iteration
60
+ def train_gradient_descent( alpha = 0.01, iterations = 500, verbose = false )
61
+
62
+ 0.upto( iterations ) do |i|
63
+ @temp_theta = Array.new(@theta.row_size)
64
+ 0.upto(@theta.row_size-1) do |row|
65
+ @temp_theta[row] = @theta[row,0] - alpha * compute_gradient(row)
66
+ end
67
+
68
+ @theta = Matrix.columns([@temp_theta])
69
+
70
+ puts "Cost after #{i} iterations = #{compute_cost}" if verbose
71
+ end
72
+
73
+ end
74
+
55
75
  # Makes a prediction based on your trained model.
56
76
  # train_normal_equation must be called prior to making a prediction.
57
77
  #
@@ -102,4 +122,15 @@ class RubyLinearRegression
102
122
 
103
123
  end
104
124
 
125
+ # Compute the mean squared cost / error function
126
+ def compute_gradient( parameter )
127
+
128
+ # First use matrix multiplication and vector subtracton to find errors
129
+ gradients = ((@x * @theta) - @y).transpose * @x.column(parameter)
130
+
131
+ # Mean the grandient
132
+ mean = gradients.inject{ |sum, e| sum + e } / gradients.size
133
+
134
+ return mean
135
+ end
105
136
  end
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_linear_regression
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soren Blond Daugaard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-10 00:00:00.000000000 Z
11
+ date: 2017-06-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: "An implementation of a linear regression machine learning algorithm
14
- implemented in Ruby.\n This algorithm uses Ruby's Matrix implementation
15
- and the normal equation to train the data to the best fit.\n The
16
- algorithm works with multiple independent variables to predict a dependent variable. "
14
+ in Ruby.\n This algorithm uses Ruby's Matrix implementation and the
15
+ normal equation to train the data to the best fit.\n The algorithm
16
+ works with one independent variable or as a multivariate linear regression with
17
+ multiple variables to predict a dependent variable. "
17
18
  email: sbd@ineptum.dk
18
19
  executables: []
19
20
  extensions: []
@@ -40,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
41
  version: '0'
41
42
  requirements: []
42
43
  rubyforge_project:
43
- rubygems_version: 2.6.8
44
+ rubygems_version: 2.6.12
44
45
  signing_key:
45
46
  specification_version: 4
46
47
  summary: Linear regression implemented in Ruby.