ruby_linear_regression 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/ruby_linear_regression.rb +94 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fd88f39c66fb2efe50b976fe3b1f04e06b179d18
|
4
|
+
data.tar.gz: 83842d112a3d92d239a08422e750f616923b2942
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a57cf9ee217bebd8ef47d417068c2abef876592d909fcf3359c05d11b12ebc7a4891cb583cd96193637c0cdbe6fe14278c92649377d860c8e32c33217a1d7a6e
|
7
|
+
data.tar.gz: 8f8aa53d404036c3ee657f94ad9d15f99f2ba9bf3da6b9854e10144fea7f87432e7ea2cf3bc9d8fe87a52c84121f242e472afc7a582407d6acfd0e088cd6fd0a
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
class RubyLinearRegression
|
4
|
+
|
5
|
+
attr_reader :x,:y,:theta,:mu,:sigma
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@mu = 0
|
9
|
+
@sigma = 1
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_training_data x_data, y_data
|
13
|
+
|
14
|
+
# normalize the x_data
|
15
|
+
x_data = normalize_data( x_data )
|
16
|
+
|
17
|
+
# add 1 column to our data
|
18
|
+
x_data = x_data.map { |r| [1].concat(r) }
|
19
|
+
|
20
|
+
# build our x Matrix & y Vector
|
21
|
+
@x = Matrix.rows( x_data )
|
22
|
+
@y = Matrix.rows( y_data.collect { |e| [e] } )
|
23
|
+
|
24
|
+
@theta = Matrix[[0],[0]]
|
25
|
+
end
|
26
|
+
|
27
|
+
def compute_cost
|
28
|
+
# Compute the mean squared cost / error function
|
29
|
+
# First use matrix multiplication and vector subtracton to find errors
|
30
|
+
errors = (@x * @theta) - @y
|
31
|
+
|
32
|
+
# Then square all errors
|
33
|
+
errors = errors.map { |e| e * e }
|
34
|
+
|
35
|
+
# Find the mean of the square errors
|
36
|
+
mean_square_error = 0.5 * (errors.inject{ |sum, e| sum + e }.to_f / errors.row_size)
|
37
|
+
|
38
|
+
return mean_square_error
|
39
|
+
end
|
40
|
+
|
41
|
+
def train_normal_equation
|
42
|
+
# Calculate the optimal theta using the normal equation
|
43
|
+
# theta = ( X' * X )^1 * X' * y
|
44
|
+
@theta = (@x.transpose * @x).inverse * @x.transpose * @y
|
45
|
+
|
46
|
+
return @theta
|
47
|
+
end
|
48
|
+
|
49
|
+
def predict data
|
50
|
+
|
51
|
+
# normalize
|
52
|
+
data.each_index do |i|
|
53
|
+
data[i] = (data[i] - @mu[i]) / @sigma[i].to_f
|
54
|
+
end
|
55
|
+
|
56
|
+
# add 1 column to prediction data
|
57
|
+
data = [1].concat( data )
|
58
|
+
|
59
|
+
# perform prediction
|
60
|
+
prediction = (Matrix[data] * @theta)[0,0].to_f
|
61
|
+
|
62
|
+
return prediction
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
def normalize_data x_data
|
68
|
+
|
69
|
+
row_size = x_data.size
|
70
|
+
column_count = x_data[0].is_a?( Array) ? x_data[0].size : 1
|
71
|
+
|
72
|
+
x_norm = Array.new(row_size)
|
73
|
+
@mu = Array.new(column_count)
|
74
|
+
@sigma = Array.new(column_count)
|
75
|
+
|
76
|
+
0.upto(column_count - 1) do |column|
|
77
|
+
column_data = x_data.map{ |e| e[column] }
|
78
|
+
@mu[column] = column_data.inject{ |sum, e| sum + e } / row_size
|
79
|
+
@sigma[column] = (column_data.max - column_data.min)
|
80
|
+
end
|
81
|
+
|
82
|
+
0.upto(row_size-1) do |row|
|
83
|
+
row_data = x_data[row]
|
84
|
+
x_norm[row] = Array.new(column_count)
|
85
|
+
row_data.each_index do |i|
|
86
|
+
x_norm[row][i] = (row_data[i] - @mu[i]) / @sigma[i].to_f
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
return x_norm
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_linear_regression
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Soren Blond Daugaard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
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. "
|
17
|
+
email: [email protected]
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/ruby_linear_regression.rb
|
23
|
+
homepage: https://github.com/daugaard/linear-regression
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.6.8
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Linear regression implemented in Ruby.
|
47
|
+
test_files: []
|