xgb 0.1.2 → 0.1.3
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 +4 -4
 - data/CHANGELOG.md +6 -0
 - data/LICENSE.txt +22 -0
 - data/README.md +34 -5
 - data/lib/xgb/booster.rb +2 -2
 - data/lib/xgb/dmatrix.rb +18 -5
 - data/lib/xgb/utils.rb +5 -0
 - data/lib/xgb/version.rb +1 -1
 - metadata +3 -16
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 41e23d36cc3404e32f22527f2ddfd112db2abd096fbdbf272bade29d032f0df6
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 5fb377835c2dce3ec1d4f6d734588f3d1e222e9fe35fa9bd127b0eb6198f7f7d
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 6a6551eef35b0c560bccb2e23034cda55ee29bd6dd00840e7d8dc8a86b1df017433d270cbb7b1a812f65d92123c22459722266c7a813b4a9e5184d69ba5abe73
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 4f15acf5555c4a5db81e73113cc5dc0c47f438687d9ea85dae25d818395bd6ed27f162e7a0588c65e347ee51a805eb2d2a673113aad629f6b7903e31f4d5b2ff
         
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2019 Andrew Kane
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 6 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 7 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 8 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 9 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 10 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 11 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 14 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 17 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 18 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 19 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 20 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 21 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -8,7 +8,11 @@ 
     | 
|
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
            ## Installation
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
     | 
    
         
            -
            First, [install XGBoost](https://xgboost.readthedocs.io/en/latest/build.html).  
     | 
| 
      
 11 
     | 
    
         
            +
            First, [install XGBoost](https://xgboost.readthedocs.io/en/latest/build.html). For Homebrew, use:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            ```sh
         
     | 
| 
      
 14 
     | 
    
         
            +
            brew install xgboost
         
     | 
| 
      
 15 
     | 
    
         
            +
            ```
         
     | 
| 
       12 
16 
     | 
    
         | 
| 
       13 
17 
     | 
    
         
             
            Add this line to your application’s Gemfile:
         
     | 
| 
       14 
18 
     | 
    
         | 
| 
         @@ -18,22 +22,29 @@ gem 'xgb' 
     | 
|
| 
       18 
22 
     | 
    
         | 
| 
       19 
23 
     | 
    
         
             
            ## Getting Started
         
     | 
| 
       20 
24 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
            This library follows the [Python API](https://xgboost.readthedocs.io/en/latest/python/python_api.html). Some methods and options are missing at the moment. PRs welcome!
         
     | 
| 
      
 25 
     | 
    
         
            +
            This library follows the [Python API](https://xgboost.readthedocs.io/en/latest/python/python_api.html), with the `get_` and `set_` prefixes removed from methods. Some methods and options are missing at the moment. PRs welcome!
         
     | 
| 
       22 
26 
     | 
    
         | 
| 
       23 
27 
     | 
    
         
             
            ## Learning API
         
     | 
| 
       24 
28 
     | 
    
         | 
| 
      
 29 
     | 
    
         
            +
            Prep your data
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 32 
     | 
    
         
            +
            x = [[1, 2], [3, 4], [5, 6], [7, 8]]
         
     | 
| 
      
 33 
     | 
    
         
            +
            y = [1, 2, 3, 4]
         
     | 
| 
      
 34 
     | 
    
         
            +
            ```
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
       25 
36 
     | 
    
         
             
            Train a model
         
     | 
| 
       26 
37 
     | 
    
         | 
| 
       27 
38 
     | 
    
         
             
            ```ruby
         
     | 
| 
       28 
39 
     | 
    
         
             
            params = {objective: "reg:squarederror"}
         
     | 
| 
       29 
     | 
    
         
            -
            dtrain = Xgb::DMatrix.new( 
     | 
| 
      
 40 
     | 
    
         
            +
            dtrain = Xgb::DMatrix.new(x, label: y)
         
     | 
| 
       30 
41 
     | 
    
         
             
            booster = Xgb.train(params, dtrain)
         
     | 
| 
       31 
42 
     | 
    
         
             
            ```
         
     | 
| 
       32 
43 
     | 
    
         | 
| 
       33 
44 
     | 
    
         
             
            Predict
         
     | 
| 
       34 
45 
     | 
    
         | 
| 
       35 
46 
     | 
    
         
             
            ```ruby
         
     | 
| 
       36 
     | 
    
         
            -
            dtest = Xgb::DMatrix.new( 
     | 
| 
      
 47 
     | 
    
         
            +
            dtest = Xgb::DMatrix.new(x)
         
     | 
| 
       37 
48 
     | 
    
         
             
            booster.predict(dtest)
         
     | 
| 
       38 
49 
     | 
    
         
             
            ```
         
     | 
| 
       39 
50 
     | 
    
         | 
| 
         @@ -137,6 +148,24 @@ Or a Numo NArray 
     | 
|
| 
       137 
148 
     | 
    
         
             
            Numo::DFloat.new(3, 2).seq
         
     | 
| 
       138 
149 
     | 
    
         
             
            ```
         
     | 
| 
       139 
150 
     | 
    
         | 
| 
      
 151 
     | 
    
         
            +
            ## XGBoost Installation
         
     | 
| 
      
 152 
     | 
    
         
            +
             
     | 
| 
      
 153 
     | 
    
         
            +
            There’s an experimental branch that includes XGBoost with the gem for easiest installation.
         
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 156 
     | 
    
         
            +
            gem 'xgb', github: 'ankane/xgb', branch: 'vendor', submodules: true
         
     | 
| 
      
 157 
     | 
    
         
            +
            ```
         
     | 
| 
      
 158 
     | 
    
         
            +
             
     | 
| 
      
 159 
     | 
    
         
            +
            Please file an issue if it doesn’t work for you.
         
     | 
| 
      
 160 
     | 
    
         
            +
             
     | 
| 
      
 161 
     | 
    
         
            +
            You can also specify the path to XGBoost in an initializer:
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 164 
     | 
    
         
            +
            Xgb.ffi_lib << "/path/to/xgboost/lib/libxgboost.so"
         
     | 
| 
      
 165 
     | 
    
         
            +
            ```
         
     | 
| 
      
 166 
     | 
    
         
            +
             
     | 
| 
      
 167 
     | 
    
         
            +
            > Use `libxgboost.dylib` for Mac and `xgboost.dll` for Windows
         
     | 
| 
      
 168 
     | 
    
         
            +
             
     | 
| 
       140 
169 
     | 
    
         
             
            ## Helpful Resources
         
     | 
| 
       141 
170 
     | 
    
         | 
| 
       142 
171 
     | 
    
         
             
            - [Parameters](https://xgboost.readthedocs.io/en/latest/parameter.html)
         
     | 
| 
         @@ -149,7 +178,7 @@ Numo::DFloat.new(3, 2).seq 
     | 
|
| 
       149 
178 
     | 
    
         | 
| 
       150 
179 
     | 
    
         
             
            ## Credits
         
     | 
| 
       151 
180 
     | 
    
         | 
| 
       152 
     | 
    
         
            -
            Thanks to the [xgboost](https://github.com/PairOnAir/xgboost-ruby) gem for serving as an initial reference 
     | 
| 
      
 181 
     | 
    
         
            +
            Thanks to the [xgboost](https://github.com/PairOnAir/xgboost-ruby) gem for serving as an initial reference.
         
     | 
| 
       153 
182 
     | 
    
         | 
| 
       154 
183 
     | 
    
         
             
            ## History
         
     | 
| 
       155 
184 
     | 
    
         | 
    
        data/lib/xgb/booster.rb
    CHANGED
    
    | 
         @@ -53,7 +53,7 @@ module Xgb 
     | 
|
| 
       53 
53 
     | 
    
         
             
                  out_len = ::FFI::MemoryPointer.new(:uint64)
         
     | 
| 
       54 
54 
     | 
    
         
             
                  out_result = ::FFI::MemoryPointer.new(:pointer)
         
     | 
| 
       55 
55 
     | 
    
         
             
                  check_result FFI.XGBoosterPredict(handle_pointer, data.handle_pointer, 0, ntree_limit, out_len, out_result)
         
     | 
| 
       56 
     | 
    
         
            -
                  out = out_result.read_pointer.read_array_of_float(out_len 
     | 
| 
      
 56 
     | 
    
         
            +
                  out = out_result.read_pointer.read_array_of_float(read_uint64(out_len))
         
     | 
| 
       57 
57 
     | 
    
         
             
                  num_class = out.size / data.num_row
         
     | 
| 
       58 
58 
     | 
    
         
             
                  out = out.each_slice(num_class).to_a if num_class > 1
         
     | 
| 
       59 
59 
     | 
    
         
             
                  out
         
     | 
| 
         @@ -68,7 +68,7 @@ module Xgb 
     | 
|
| 
       68 
68 
     | 
    
         
             
                  out_len = ::FFI::MemoryPointer.new(:uint64)
         
     | 
| 
       69 
69 
     | 
    
         
             
                  out_result = ::FFI::MemoryPointer.new(:pointer)
         
     | 
| 
       70 
70 
     | 
    
         
             
                  check_result FFI.XGBoosterDumpModelEx(handle_pointer, fmap, with_stats ? 1 : 0, dump_format, out_len, out_result)
         
     | 
| 
       71 
     | 
    
         
            -
                  out_result.read_pointer.get_array_of_string(0, out_len 
     | 
| 
      
 71 
     | 
    
         
            +
                  out_result.read_pointer.get_array_of_string(0, read_uint64(out_len))
         
     | 
| 
       72 
72 
     | 
    
         
             
                end
         
     | 
| 
       73 
73 
     | 
    
         | 
| 
       74 
74 
     | 
    
         
             
                def dump_model(fout, fmap: "", with_stats: false, dump_format: "text")
         
     | 
    
        data/lib/xgb/dmatrix.rb
    CHANGED
    
    | 
         @@ -14,7 +14,7 @@ module Xgb 
     | 
|
| 
       14 
14 
     | 
    
         
             
                      flat_data = data.to_a.flatten
         
     | 
| 
       15 
15 
     | 
    
         
             
                    elsif daru?(data)
         
     | 
| 
       16 
16 
     | 
    
         
             
                      nrow, ncol = data.shape
         
     | 
| 
       17 
     | 
    
         
            -
                      flat_data = data. 
     | 
| 
      
 17 
     | 
    
         
            +
                      flat_data = data.map_rows(&:to_a).flatten
         
     | 
| 
       18 
18 
     | 
    
         
             
                    elsif narray?(data)
         
     | 
| 
       19 
19 
     | 
    
         
             
                      nrow, ncol = data.shape
         
     | 
| 
       20 
20 
     | 
    
         
             
                      flat_data = data.flatten.to_a
         
     | 
| 
         @@ -24,6 +24,7 @@ module Xgb 
     | 
|
| 
       24 
24 
     | 
    
         
             
                      flat_data = data.flatten
         
     | 
| 
       25 
25 
     | 
    
         
             
                    end
         
     | 
| 
       26 
26 
     | 
    
         | 
| 
      
 27 
     | 
    
         
            +
                    handle_missing(flat_data, missing)
         
     | 
| 
       27 
28 
     | 
    
         
             
                    c_data = ::FFI::MemoryPointer.new(:float, nrow * ncol)
         
     | 
| 
       28 
29 
     | 
    
         
             
                    c_data.put_array_of_float(0, flat_data)
         
     | 
| 
       29 
30 
     | 
    
         
             
                    check_result FFI.XGDMatrixCreateFromMat(c_data, nrow, ncol, missing, @handle)
         
     | 
| 
         @@ -31,8 +32,8 @@ module Xgb 
     | 
|
| 
       31 
32 
     | 
    
         
             
                    ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer))
         
     | 
| 
       32 
33 
     | 
    
         
             
                  end
         
     | 
| 
       33 
34 
     | 
    
         | 
| 
       34 
     | 
    
         
            -
                   
     | 
| 
       35 
     | 
    
         
            -
                   
     | 
| 
      
 35 
     | 
    
         
            +
                  self.label = label if label
         
     | 
| 
      
 36 
     | 
    
         
            +
                  self.weight = weight if weight
         
     | 
| 
       36 
37 
     | 
    
         
             
                end
         
     | 
| 
       37 
38 
     | 
    
         | 
| 
       38 
39 
     | 
    
         
             
                def self.finalize(pointer)
         
     | 
| 
         @@ -48,6 +49,14 @@ module Xgb 
     | 
|
| 
       48 
49 
     | 
    
         
             
                  float_info("weight")
         
     | 
| 
       49 
50 
     | 
    
         
             
                end
         
     | 
| 
       50 
51 
     | 
    
         | 
| 
      
 52 
     | 
    
         
            +
                def label=(label)
         
     | 
| 
      
 53 
     | 
    
         
            +
                  set_float_info("label", label)
         
     | 
| 
      
 54 
     | 
    
         
            +
                end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                def weight=(weight)
         
     | 
| 
      
 57 
     | 
    
         
            +
                  set_float_info("weight", weight)
         
     | 
| 
      
 58 
     | 
    
         
            +
                end
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
       51 
60 
     | 
    
         
             
                def group=(group)
         
     | 
| 
       52 
61 
     | 
    
         
             
                  c_data = ::FFI::MemoryPointer.new(:int, group.size)
         
     | 
| 
       53 
62 
     | 
    
         
             
                  c_data.put_array_of_int(0, group)
         
     | 
| 
         @@ -57,13 +66,13 @@ module Xgb 
     | 
|
| 
       57 
66 
     | 
    
         
             
                def num_row
         
     | 
| 
       58 
67 
     | 
    
         
             
                  out = ::FFI::MemoryPointer.new(:uint64)
         
     | 
| 
       59 
68 
     | 
    
         
             
                  check_result FFI.XGDMatrixNumRow(handle_pointer, out)
         
     | 
| 
       60 
     | 
    
         
            -
                  out 
     | 
| 
      
 69 
     | 
    
         
            +
                  read_uint64(out)
         
     | 
| 
       61 
70 
     | 
    
         
             
                end
         
     | 
| 
       62 
71 
     | 
    
         | 
| 
       63 
72 
     | 
    
         
             
                def num_col
         
     | 
| 
       64 
73 
     | 
    
         
             
                  out = ::FFI::MemoryPointer.new(:uint64)
         
     | 
| 
       65 
74 
     | 
    
         
             
                  check_result FFI.XGDMatrixNumCol(handle_pointer, out)
         
     | 
| 
       66 
     | 
    
         
            -
                  out 
     | 
| 
      
 75 
     | 
    
         
            +
                  read_uint64(out)
         
     | 
| 
       67 
76 
     | 
    
         
             
                end
         
     | 
| 
       68 
77 
     | 
    
         | 
| 
       69 
78 
     | 
    
         
             
                def slice(rindex)
         
     | 
| 
         @@ -115,6 +124,10 @@ module Xgb 
     | 
|
| 
       115 
124 
     | 
    
         
             
                  defined?(Numo::NArray) && data.is_a?(Numo::NArray)
         
     | 
| 
       116 
125 
     | 
    
         
             
                end
         
     | 
| 
       117 
126 
     | 
    
         | 
| 
      
 127 
     | 
    
         
            +
                def handle_missing(data, missing)
         
     | 
| 
      
 128 
     | 
    
         
            +
                  data.map! { |v| v.nil? ? missing : v }
         
     | 
| 
      
 129 
     | 
    
         
            +
                end
         
     | 
| 
      
 130 
     | 
    
         
            +
             
     | 
| 
       118 
131 
     | 
    
         
             
                include Utils
         
     | 
| 
       119 
132 
     | 
    
         
             
              end
         
     | 
| 
       120 
133 
     | 
    
         
             
            end
         
     | 
    
        data/lib/xgb/utils.rb
    CHANGED
    
    
    
        data/lib/xgb/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: xgb
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.3
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Andrew Kane
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2019- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2019-10-28 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: ffi
         
     | 
| 
         @@ -80,20 +80,6 @@ dependencies: 
     | 
|
| 
       80 
80 
     | 
    
         
             
                - - ">="
         
     | 
| 
       81 
81 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       82 
82 
     | 
    
         
             
                    version: '0'
         
     | 
| 
       83 
     | 
    
         
            -
            - !ruby/object:Gem::Dependency
         
     | 
| 
       84 
     | 
    
         
            -
              name: numo-narray
         
     | 
| 
       85 
     | 
    
         
            -
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       86 
     | 
    
         
            -
                requirements:
         
     | 
| 
       87 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       88 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       89 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       90 
     | 
    
         
            -
              type: :development
         
     | 
| 
       91 
     | 
    
         
            -
              prerelease: false
         
     | 
| 
       92 
     | 
    
         
            -
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       93 
     | 
    
         
            -
                requirements:
         
     | 
| 
       94 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       95 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       96 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       97 
83 
     | 
    
         
             
            description: 
         
     | 
| 
       98 
84 
     | 
    
         
             
            email: andrew@chartkick.com
         
     | 
| 
       99 
85 
     | 
    
         
             
            executables: []
         
     | 
| 
         @@ -101,6 +87,7 @@ extensions: [] 
     | 
|
| 
       101 
87 
     | 
    
         
             
            extra_rdoc_files: []
         
     | 
| 
       102 
88 
     | 
    
         
             
            files:
         
     | 
| 
       103 
89 
     | 
    
         
             
            - CHANGELOG.md
         
     | 
| 
      
 90 
     | 
    
         
            +
            - LICENSE.txt
         
     | 
| 
       104 
91 
     | 
    
         
             
            - README.md
         
     | 
| 
       105 
92 
     | 
    
         
             
            - lib/xgb.rb
         
     | 
| 
       106 
93 
     | 
    
         
             
            - lib/xgb/booster.rb
         
     |