lightgbm 0.1.0
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/CHANGELOG.md +3 -0
- data/README.md +42 -0
- data/lib/lightgbm.rb +7 -0
- data/lib/lightgbm/booster.rb +46 -0
- data/lib/lightgbm/ffi.rb +13 -0
- data/lib/lightgbm/version.rb +3 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c7222cd44123f8c35d26b91dbf0288e3d0f7a87e3204a6a501f7590648896d2f
|
4
|
+
data.tar.gz: 2a288731615d9042b0b60184c534bd00d3d64d1d8be1b8dbc2aecb782fd3332b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 290d729343c88d2054082692cff9dc948332933439b11915bd507338dc28354fec936b7eded198c8a6bd18d9821ff32d1dccc146c71db7567ddc6037838c0e4b
|
7
|
+
data.tar.gz: 24cd193a72188d43b71cc223d45cc943d710149d42b7ee0fb11eeab8fdf0a9e006ae021313d0881d91cebfbc510a08864721af42f4976e5cd0663ca1a6534f39
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# LightGBM
|
2
|
+
|
3
|
+
LightGBM for Ruby
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
First, [install LightGBM](https://lightgbm.readthedocs.io/en/latest/Installation-Guide.html).
|
8
|
+
|
9
|
+
Add this line to your application’s Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'lightgbm'
|
13
|
+
```
|
14
|
+
|
15
|
+
Load a model
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
booster = LightGBM::Booster.new(model_file: "model.txt")
|
19
|
+
```
|
20
|
+
|
21
|
+
Predict
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
booster.predict([[1, 2], [3, 4]])
|
25
|
+
```
|
26
|
+
|
27
|
+
## Credits
|
28
|
+
|
29
|
+
Thanks to the [xgboost](https://github.com/PairOnAir/xgboost-ruby) gem for serving as an initial reference.
|
30
|
+
|
31
|
+
## History
|
32
|
+
|
33
|
+
View the [changelog](https://github.com/ankane/lightgbm/blob/master/CHANGELOG.md)
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
38
|
+
|
39
|
+
- [Report bugs](https://github.com/ankane/lightgbm/issues)
|
40
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/lightgbm/pulls)
|
41
|
+
- Write, clarify, or fix documentation
|
42
|
+
- Suggest or add new features
|
data/lib/lightgbm.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module LightGBM
|
2
|
+
class Booster
|
3
|
+
def initialize(model_file:)
|
4
|
+
@handle = ::FFI::MemoryPointer.new(:pointer)
|
5
|
+
if model_file
|
6
|
+
out_num_iterations = ::FFI::MemoryPointer.new(:int)
|
7
|
+
check_result FFI.LGBM_BoosterCreateFromModelfile(model_file, out_num_iterations, @handle)
|
8
|
+
end
|
9
|
+
ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer))
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.finalize(pointer)
|
13
|
+
-> { FFI.LGBM_BoosterFree(pointer) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def predict(input)
|
17
|
+
raise TypeError unless input.is_a?(Array)
|
18
|
+
|
19
|
+
singular = input.first.is_a?(Array)
|
20
|
+
input = [input] unless singular
|
21
|
+
|
22
|
+
data = ::FFI::MemoryPointer.new(:float, input.count * input.first.count)
|
23
|
+
data.put_array_of_float(0, input.flatten)
|
24
|
+
|
25
|
+
out_len = ::FFI::MemoryPointer.new(:int64)
|
26
|
+
out_result = ::FFI::MemoryPointer.new(:double, input.count)
|
27
|
+
parameter = ""
|
28
|
+
check_result FFI.LGBM_BoosterPredictForMat(handle_pointer, data, 0, input.count, input.first.count, 1, 0, 0, parameter, out_len, out_result)
|
29
|
+
out = out_result.read_array_of_double(out_len.read_int64)
|
30
|
+
|
31
|
+
singular ? out : out.first
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def check_result(err)
|
37
|
+
if err != 0
|
38
|
+
raise FFI.LGBM_GetLastError
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def handle_pointer
|
43
|
+
@handle.read_pointer
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/lightgbm/ffi.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module LightGBM
|
2
|
+
module FFI
|
3
|
+
extend ::FFI::Library
|
4
|
+
ffi_lib ["lightgbm", "lib_lightgbm.so"]
|
5
|
+
|
6
|
+
# https://github.com/microsoft/LightGBM/blob/master/include/LightGBM/c_api.h
|
7
|
+
attach_function :LGBM_GetLastError, %i[], :string
|
8
|
+
attach_function :LGBM_BoosterCreate, %i[pointer string pointer], :int
|
9
|
+
attach_function :LGBM_BoosterCreateFromModelfile, %i[string pointer pointer], :int
|
10
|
+
attach_function :LGBM_BoosterFree, %i[pointer], :int
|
11
|
+
attach_function :LGBM_BoosterPredictForMat, %i[pointer pointer int int32 int32 int int int string pointer pointer], :int
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lightgbm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5'
|
69
|
+
description:
|
70
|
+
email: andrew@chartkick.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- CHANGELOG.md
|
76
|
+
- README.md
|
77
|
+
- lib/lightgbm.rb
|
78
|
+
- lib/lightgbm/booster.rb
|
79
|
+
- lib/lightgbm/ffi.rb
|
80
|
+
- lib/lightgbm/version.rb
|
81
|
+
homepage: https://github.com/ankane/lightgbm
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '2.4'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubygems_version: 3.0.4
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: LightGBM for Ruby
|
104
|
+
test_files: []
|