ruby_linear_regression 0.1.4 → 0.1.6
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 +5 -5
 - data/lib/ruby_linear_regression.rb +26 -6
 - metadata +34 -7
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 27d2da5667e20ab5feb1ebdd6e5fb9f4257c74084f0c71a9ef57fd4db6350202
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 75059d3f1dbf63006f5da0099e98eb583cf51cb0a4c9264b81dcd00e28e19e61
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 26f57a6d1e4450dc27b87479c97634d089382f9a59431bb2344f195d4132522ab06be9ce541810ad3b93913d8aeb448b5d318fa2109b8bbcad6aaa4b9986d596
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 8daa2f8bcd9c7b0ca74016d5494ac18c50eebba6bd3ee6ba5baaa9635aa630e47bb6069972fdc4519a5d2c484c48541ebfa49cdc288a0ba72598bef80f0782f2
         
     | 
| 
         @@ -32,12 +32,26 @@ class RubyLinearRegression 
     | 
|
| 
       32 
32 
     | 
    
         
             
              end
         
     | 
| 
       33 
33 
     | 
    
         | 
| 
       34 
34 
     | 
    
         
             
              # Compute the mean squared cost / error function
         
     | 
| 
       35 
     | 
    
         
            -
              def compute_cost
         
     | 
| 
      
 35 
     | 
    
         
            +
              def compute_cost test_x = nil, test_y = nil
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                if not test_x.nil?
         
     | 
| 
      
 38 
     | 
    
         
            +
                  test_x.each_index do |row|
         
     | 
| 
      
 39 
     | 
    
         
            +
                    test_x[row].each_index do |i|
         
     | 
| 
      
 40 
     | 
    
         
            +
                      test_x[row][i] = (test_x[row][i] - @mu[i]) / @sigma[i].to_f
         
     | 
| 
      
 41 
     | 
    
         
            +
                    end
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end if @normalize
         
     | 
| 
      
 43 
     | 
    
         
            +
                  test_x = test_x.map { |r| [1].concat(r) }
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                # per default use training data to compute cost if no data is given
         
     | 
| 
      
 47 
     | 
    
         
            +
                cost_x = test_x.nil? ? @x : Matrix.rows( test_x )
         
     | 
| 
      
 48 
     | 
    
         
            +
                cost_y = test_y.nil? ? @y : Matrix.rows( test_y.collect { |e| [e] } )
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       36 
50 
     | 
    
         
             
                # First use matrix multiplication and vector subtracton to find errors
         
     | 
| 
       37 
     | 
    
         
            -
                errors = ( 
     | 
| 
      
 51 
     | 
    
         
            +
                errors = (cost_x * @theta) - cost_y
         
     | 
| 
       38 
52 
     | 
    
         | 
| 
       39 
53 
     | 
    
         
             
                # Then square all errors
         
     | 
| 
       40 
     | 
    
         
            -
                errors = errors.map { |e| e 
     | 
| 
      
 54 
     | 
    
         
            +
                errors = errors.map { |e| (e.to_f**2)  }
         
     | 
| 
       41 
55 
     | 
    
         | 
| 
       42 
56 
     | 
    
         
             
                # Find the mean of the square errors
         
     | 
| 
       43 
57 
     | 
    
         
             
                mean_square_error = 0.5 * (errors.inject{ |sum, e| sum + e }.to_f / errors.row_size)
         
     | 
| 
         @@ -46,10 +60,16 @@ class RubyLinearRegression 
     | 
|
| 
       46 
60 
     | 
    
         
             
              end
         
     | 
| 
       47 
61 
     | 
    
         | 
| 
       48 
62 
     | 
    
         
             
              # Calculate the optimal theta using the normal equation
         
     | 
| 
       49 
     | 
    
         
            -
              def train_normal_equation
         
     | 
| 
      
 63 
     | 
    
         
            +
              def train_normal_equation l = 0
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
                @lambda = l
         
     | 
| 
      
 66 
     | 
    
         
            +
                lambda_matrix = Matrix.build(@theta.row_size,@theta.row_size) do |c,r|
         
     | 
| 
      
 67 
     | 
    
         
            +
                    (( c == 0 && r == 0) || c != r) ? 0 : 1;
         
     | 
| 
      
 68 
     | 
    
         
            +
                  end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
       50 
70 
     | 
    
         
             
                # Calculate the optimal theta using the normal equation
         
     | 
| 
       51 
71 
     | 
    
         
             
                # theta = ( X' * X )^1 * X' * y
         
     | 
| 
       52 
     | 
    
         
            -
                @theta = (@x.transpose * @x).inverse * @x.transpose * @y
         
     | 
| 
      
 72 
     | 
    
         
            +
                @theta = (@x.transpose * @x + @lambda * lambda_matrix ).inverse * @x.transpose * @y
         
     | 
| 
       53 
73 
     | 
    
         | 
| 
       54 
74 
     | 
    
         
             
                return @theta
         
     | 
| 
       55 
75 
     | 
    
         
             
              end
         
     | 
| 
         @@ -97,7 +117,7 @@ class RubyLinearRegression 
     | 
|
| 
       97 
117 
     | 
    
         
             
              end
         
     | 
| 
       98 
118 
     | 
    
         | 
| 
       99 
119 
     | 
    
         
             
              private
         
     | 
| 
       100 
     | 
    
         
            -
                def normalize_data  
     | 
| 
      
 120 
     | 
    
         
            +
                def normalize_data(x_data, mu = nil, sigma = nil)
         
     | 
| 
       101 
121 
     | 
    
         | 
| 
       102 
122 
     | 
    
         
             
                  row_size = x_data.size
         
     | 
| 
       103 
123 
     | 
    
         
             
                  column_count = x_data[0].is_a?( Array) ? x_data[0].size : 1
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,15 +1,43 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: ruby_linear_regression
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.6
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Soren Blond Daugaard
         
     | 
| 
       8 
     | 
    
         
            -
            autorequire: 
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2024-09-04 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
      
 13 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 14 
     | 
    
         
            +
              name: csv
         
     | 
| 
      
 15 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 16 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 17 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 18 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 19 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 20 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 21 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 22 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 23 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 24 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 25 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 26 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 27 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 28 
     | 
    
         
            +
              name: matrix
         
     | 
| 
      
 29 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 30 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 31 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 32 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 33 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 34 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 35 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 36 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 37 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 38 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 39 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 40 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
       13 
41 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
42 
     | 
    
         
             
              name: minitest
         
     | 
| 
       15 
43 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
         @@ -45,7 +73,7 @@ homepage: https://github.com/daugaard/linear-regression 
     | 
|
| 
       45 
73 
     | 
    
         
             
            licenses:
         
     | 
| 
       46 
74 
     | 
    
         
             
            - MIT
         
     | 
| 
       47 
75 
     | 
    
         
             
            metadata: {}
         
     | 
| 
       48 
     | 
    
         
            -
            post_install_message: 
     | 
| 
      
 76 
     | 
    
         
            +
            post_install_message:
         
     | 
| 
       49 
77 
     | 
    
         
             
            rdoc_options: []
         
     | 
| 
       50 
78 
     | 
    
         
             
            require_paths:
         
     | 
| 
       51 
79 
     | 
    
         
             
            - lib
         
     | 
| 
         @@ -60,9 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       60 
88 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       61 
89 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       62 
90 
     | 
    
         
             
            requirements: []
         
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
            signing_key: 
         
     | 
| 
      
 91 
     | 
    
         
            +
            rubygems_version: 3.5.16
         
     | 
| 
      
 92 
     | 
    
         
            +
            signing_key:
         
     | 
| 
       66 
93 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       67 
94 
     | 
    
         
             
            summary: Linear regression implemented in Ruby.
         
     | 
| 
       68 
95 
     | 
    
         
             
            test_files: []
         
     |