├── .gitattributes
├── .github
└── workflows
│ └── maven.yml
├── .gitignore
├── LICENSE
├── README.md
├── generate_checksums.sh
├── pom.xml
├── src
├── main
│ ├── java
│ │ ├── com
│ │ │ └── microsoft
│ │ │ │ └── ml
│ │ │ │ └── lightgbm
│ │ │ │ ├── CSRDirect.java
│ │ │ │ ├── PredictionType.java
│ │ │ │ ├── SWIGTYPE_p_ArrowArray.java
│ │ │ │ ├── SWIGTYPE_p_ArrowSchema.java
│ │ │ │ ├── SWIGTYPE_p_double.java
│ │ │ │ ├── SWIGTYPE_p_f_p_q_const__char__void.java
│ │ │ │ ├── SWIGTYPE_p_float.java
│ │ │ │ ├── SWIGTYPE_p_int.java
│ │ │ │ ├── SWIGTYPE_p_long.java
│ │ │ │ ├── SWIGTYPE_p_long_long.java
│ │ │ │ ├── SWIGTYPE_p_p_char.java
│ │ │ │ ├── SWIGTYPE_p_p_double.java
│ │ │ │ ├── SWIGTYPE_p_p_float.java
│ │ │ │ ├── SWIGTYPE_p_p_int.java
│ │ │ │ ├── SWIGTYPE_p_p_void.java
│ │ │ │ ├── SWIGTYPE_p_size_t.java
│ │ │ │ ├── SWIGTYPE_p_unsigned_char.java
│ │ │ │ ├── SWIGTYPE_p_void.java
│ │ │ │ ├── doubleChunkedArray.java
│ │ │ │ ├── floatChunkedArray.java
│ │ │ │ ├── int32ChunkedArray.java
│ │ │ │ ├── lightgbmlib.java
│ │ │ │ ├── lightgbmlibConstants.java
│ │ │ │ ├── lightgbmlibJAVA_wrap.cxx
│ │ │ │ └── lightgbmlibJNI.java
│ │ └── io
│ │ │ └── github
│ │ │ └── metarank
│ │ │ └── lightgbm4j
│ │ │ ├── LGBMBooster.java
│ │ │ ├── LGBMDataset.java
│ │ │ └── LGBMException.java
│ └── resources
│ │ └── lightgbm4j
│ │ ├── linux
│ │ ├── aarch64
│ │ │ ├── lib_lightgbm.so
│ │ │ ├── lib_lightgbm.so.md5
│ │ │ ├── lib_lightgbm_swig.so
│ │ │ └── lib_lightgbm_swig.so.md5
│ │ └── x86_64
│ │ │ ├── lib_lightgbm.so
│ │ │ ├── lib_lightgbm.so.md5
│ │ │ ├── lib_lightgbm_swig.so
│ │ │ └── lib_lightgbm_swig.so.md5
│ │ ├── osx
│ │ ├── aarch64
│ │ │ ├── lib_lightgbm.dylib
│ │ │ ├── lib_lightgbm.dylib.md5
│ │ │ ├── lib_lightgbm_swig.dylib
│ │ │ └── lib_lightgbm_swig.dylib.md5
│ │ └── x86_64
│ │ │ ├── lib_lightgbm.dylib
│ │ │ ├── lib_lightgbm.dylib.md5
│ │ │ ├── lib_lightgbm_swig.dylib
│ │ │ └── lib_lightgbm_swig.dylib.md5
│ │ └── windows
│ │ └── x86_64
│ │ ├── lib_lightgbm.dll
│ │ ├── lib_lightgbm.dll.md5
│ │ ├── lib_lightgbm_swig.dll
│ │ └── lib_lightgbm_swig.dll.md5
└── test
│ ├── java
│ └── io
│ │ └── github
│ │ └── metarank
│ │ └── lightgbm4j
│ │ ├── CancerIntegrationTest.java
│ │ ├── CategoricalIntegrationTest.java
│ │ ├── CustomObjectiveTest.java
│ │ ├── LGBMBoosterTest.java
│ │ ├── LGBMDatasetTest.java
│ │ ├── MultithreadTest.java
│ │ └── RankingIntegrationTest.java
│ └── resources
│ ├── cancer.csv
│ ├── categorical.data
│ └── mq2008
│ ├── test.txt.gz
│ └── train.txt.gz
└── swig-build
├── Dockerfile.amd64
├── Dockerfile.arm64
├── build_linux_amd64.sh
├── build_linux_arm64.sh
└── build_macos.sh
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.jar filter=lfs diff=lfs merge=lfs -text
2 | *.data filter=lfs diff=lfs merge=lfs -text
3 | *.so filter=lfs diff=lfs merge=lfs -text
4 | *.dll filter=lfs diff=lfs merge=lfs -text
5 | *.dylib filter=lfs diff=lfs merge=lfs -text
6 | *.gz filter=lfs diff=lfs merge=lfs -text
7 |
--------------------------------------------------------------------------------
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Maven
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3 |
4 | name: Java CI with Maven
5 |
6 | on:
7 | push:
8 | branches: [ main ]
9 | pull_request:
10 | branches: [ main ]
11 |
12 | jobs:
13 | build:
14 | runs-on: ${{ matrix.platform }}
15 | strategy:
16 | matrix:
17 | include:
18 | - jvm: 11
19 | platform: ubuntu-22.04
20 | architecture: x64
21 | - jvm: 17
22 | platform: ubuntu-22.04
23 | architecture: x64
24 | - jvm: 21
25 | platform: ubuntu-22.04
26 | architecture: x64
27 | - jvm: 11
28 | platform: macos-12
29 | architecture: x64
30 | - jvm: 11
31 | platform: macos-14
32 | architecture: aarch64
33 | - jvm: 11
34 | platform: windows-2022
35 | architecture: x64
36 | - jvm: 17
37 | platform: windows-2022
38 | architecture: x64
39 | - jvm: 21
40 | platform: windows-2022
41 | architecture: x64
42 | steps:
43 | - name: install libomp
44 | run: brew install libomp
45 | if: runner.os == 'macOS'
46 |
47 | - uses: actions/checkout@v2
48 | with:
49 | lfs: true
50 |
51 | - name: Checkout LFS objects
52 | run: git lfs checkout
53 |
54 | - name: Set up JDK
55 | uses: actions/setup-java@v2
56 | with:
57 | java-version: ${{ matrix.jvm }}
58 | architecture: ${{ matrix.architecture }}
59 | distribution: 'temurin'
60 |
61 | - name: Cache maven packages
62 | uses: actions/cache@v2
63 | env:
64 | cache-name: cache-mvn2
65 | with:
66 | path: ~/.m2
67 | key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }}
68 | restore-keys: |
69 | ${{ runner.os }}-build-${{ env.cache-name }}-
70 | ${{ runner.os }}-build-
71 | ${{ runner.os }}-
72 |
73 | - name: Build with Maven
74 | run: mvn -B compile --file pom.xml
75 |
76 | - name: Run tests
77 | run: mvn -B test --file pom.xml
78 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .git
2 | .idea
3 | target
4 | project/project
5 | project/target
6 | .testcontainers*
7 | *.iml
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) Microsoft Corporation
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LightGBM4j: a java wrapper for LightGBM
2 |
3 | [](https://github.com/metarank/lightgbm4j/actions)
4 | [](https://maven-badges.herokuapp.com/maven-central/io.github.metarank/lightgbm4j)
5 | [](https://opensource.org/licenses/MIT)
6 |
7 | **LightGBM4j** is a zero-dependency Java wrapper for the LightGBM project. Its main goal is to provide a 1-1 mapping for all
8 | LightGBM API methods in a Java-friendly flavor.
9 |
10 | ## Purpose
11 |
12 | LightGBM itself has a SWIG-generated JNI interface, which is possible to use directly from Java. The problem with SWIG wrappers
13 | is that they are extremely low-level. For example, to pass a java array thru SWIG, you need to do something horrible:
14 | ```java
15 | SWIGTYPE_p_float dataBuffer = new_floatArray(input.length);
16 | for (int i = 0; i < input.length; i++) {
17 | floatArray_setitem(dataBuffer, i, input[i]);
18 | }
19 | int result = <...>
20 | if (result < 0) {
21 | delete_floatArray(dataBuffer);
22 | throw new Exception(LGBM_GetLastError());
23 | } else {
24 | delete_floatArray(dataBuffer);
25 | <...>
26 | }
27 | ```
28 | This wrapper does all the dirty job for you:
29 | * exposes native java types for all supported API methods (so `float[]` instead `SWIGTYPE_p_float`)
30 | * handles manual memory management internally (so you don't need to care about JNI memory leaks)
31 | * supports both `float[]` and `double[]` API flavours.
32 | * reduces the amount of boilerplate for basic tasks.
33 |
34 | The library is in an early development stage and does not cover all 100% of LightGBM API, but the eventual future
35 | goal will be merging with the upstream LightGBM and becoming an official Java binding for the project.
36 |
37 | ## Installation
38 |
39 | To install, use the following maven coordinates:
40 | ```xml
41 |
42 | io.github.metarank
43 | lightgbm4j
44 | 4.4.0-1
45 |
46 | ```
47 |
48 | Versioning schema attempts to match the upstream, but with extra `-N` suffix, if there were a couple of extra lightgbm4j-specific
49 | changes released on top.
50 |
51 | ### MacOS & Linux native library dependencies installation
52 |
53 | LightGBM native library requires the `libomp` dependency for OpenMP support, but this library is often missing on some systems by default.
54 |
55 | For MacOS:
56 | ```
57 | brew install libomp
58 | ```
59 |
60 | For Debian Linux:
61 | ```
62 | apt install libgomp1
63 | ```
64 |
65 | ### GPU support
66 |
67 | It is possible to force GPU support for a training:
68 | * rebuild the [LightGBM with GPU support]: use `-DUSE_CUDA=1 -DUSE_SWIG=ON` CMake options. You should also match the native/JNI versions precisely.
69 | * LightGBM4j loads native libraries by default from bundled resources. This can be overridden by setting the `LIGHTGBM_NATIVE_LIB_PATH` environment variable. It should point to a directory with `lib_lightgbm.so` and `lib_lightgbm_swig.so` files (or with `dll`/`dylib` extensions on Windows/MacOS).
70 |
71 | If the native override was able to successfully load a custom library you've built, then you'll see the following line in logs:
72 | ```
73 | LIGHTGBM_NATIVE_LIB_PATH is set: loading /home/user/code/LightGBM/lib_lightgbm.so
74 | LIGHTGBM_NATIVE_LIB_PATH is set: loading /home/user/code/LightGBM/lib_lightgbm_swig.so
75 | ```
76 |
77 | ## Usage
78 |
79 | There are two main classes available:
80 | 1. `LGBMDataset` to manage input training and validation data.
81 | 2. `LGBMBooster` to do training and inference.
82 |
83 | All the public API methods in these classes should map to the [LightGBM C API](https://lightgbm.readthedocs.io/en/latest/C-API.html) methods directly.
84 |
85 | Note that both `LGBMBooster` and `LGBMDataset` classes contain handles of native memory
86 | data structures from the LightGBM, so you need to explicitly call `.close()` when they are not used. Otherwise, you may catch
87 | a native code memory leak.
88 |
89 | To load an existing model and run it:
90 | ```java
91 | LGBMBooster loaded = LGBMBooster.loadModelFromString(model);
92 | float[] input = new float[] {1.0f, 1.0f, 1.0f, 1.0f};
93 | double[] pred = booster.predictForMat(input, 2, 2, true);
94 | ```
95 |
96 | To load a dataset from a java matrix:
97 | ```java
98 | float[] matrix = new float[] {1.0f, 1.0f, 1.0f, 1.0f};
99 | LGBMDataset ds = LGBMDataset.createFromMat(matrix, 2, 2, true, "", null);
100 | ```
101 |
102 | There are some rough parts in the LightGBM API in loading the dataset from matrices:
103 | * `createFromMat` parameters cannot set the [label](https://lightgbm.readthedocs.io/en/latest/Parameters.html#label_column) or [weight](https://lightgbm.readthedocs.io/en/latest/Parameters.html#weight_column) column.
104 | So if you do `parameters = "label=some_column_name"`, it will be ignored by the LightGBM.
105 | * label/weight/group columns are magical and should NOT be included in the input matrix for
106 | `createFromMat`
107 | * to set these magical columns, you need to explicitly call `LGBMDataset.setField()` method.
108 | * `label` and `weight` columns [must be](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetSetField) `float[]`
109 | * `group` and `position` column [must be](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetSetField) `int[]`
110 |
111 | A full example of loading dataset from a matrix for a cancer dataset:
112 | ```java
113 | String[] columns = new String[] {
114 | "Age","BMI","Glucose","Insulin","HOMA","Leptin","Adiponectin","Resistin","MCP.1"
115 | };
116 | double[] values = new double[] {
117 | 71,30.3,102,8.34,2.098344,56.502,8.13,4.2989,200.976,
118 | 66,27.7,90,6.042,1.341324,24.846,7.652055,6.7052,225.88,
119 | 75,25.7,94,8.079,1.8732508,65.926,3.74122,4.49685,206.802,
120 | 78,25.3,60,3.508,0.519184,6.633,10.567295,4.6638,209.749,
121 | 69,29.4,89,10.704,2.3498848,45.272,8.2863,4.53,215.769,
122 | 85,26.6,96,4.462,1.0566016,7.85,7.9317,9.6135,232.006,
123 | 76,27.1,110,26.211,7.111918,21.778,4.935635,8.49395,45.843,
124 | 77,25.9,85,4.58,0.960273333,13.74,9.75326,11.774,488.829,
125 | 45,21.30394858,102,13.852,3.4851632,7.6476,21.056625,23.03408,552.444,
126 | 45,20.82999519,74,4.56,0.832352,7.7529,8.237405,28.0323,382.955,
127 | 49,20.9566075,94,12.305,2.853119333,11.2406,8.412175,23.1177,573.63,
128 | 34,24.24242424,92,21.699,4.9242264,16.7353,21.823745,12.06534,481.949,
129 | 42,21.35991456,93,2.999,0.6879706,19.0826,8.462915,17.37615,321.919,
130 | 68,21.08281329,102,6.2,1.55992,9.6994,8.574655,13.74244,448.799,
131 | 51,19.13265306,93,4.364,1.0011016,11.0816,5.80762,5.57055,90.6,
132 | 62,22.65625,92,3.482,0.790181867,9.8648,11.236235,10.69548,703.973
133 | };
134 |
135 | float[] labels = new float[] {
136 | 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
137 | };
138 | LGBMDataset dataset = LGBMDataset.createFromMat(values, 16, columns.length, true, "", null);
139 | dataset.setFeatureNames(columns);
140 | dataset.setField("label", labels);
141 | return dataset;
142 | ```
143 |
144 | Also, see [a working example](https://github.com/metarank/lightgbm4j/blob/main/src/test/java/io/github/metarank/lightgbm4j/CancerIntegrationTest.java)
145 | of different ways to deal with input datasets in the LightGBM4j tests.
146 | ## Example
147 |
148 | ```java
149 | // cancer dataset from https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Coimbra
150 | // with labels altered to fit the [0,1] range
151 | LGBMDataset train = LGBMDataset.createFromFile("cancer.csv", "header=true label=name:Classification", null);
152 | LGBMDataset test = LGBMDataset.createFromFile("cancer-test.csv", "header=true label=name:Classification", train);
153 | LGBMBooster booster = LGBMBooster.create(train, "objective=binary label=name:Classification");
154 | booster.addValidData(test);
155 |
156 | for (int i=0; i<10; i++) {
157 | booster.updateOneIter();
158 | double[] evalTrain = booster.getEval(0);
159 | double[] evalTest = booster.getEval(1);
160 | System.out.println("train: " + eval[0] + " test: " + );
161 | }
162 | booster.close();
163 | train.close();
164 | test.close();
165 | ```
166 |
167 | ### Categorical features
168 |
169 | LightGBM supports defining features as categorical. To make this work with LightGBM4j, you need to do the following:
170 |
171 | * Set their names with `setFeatureNames` so you can reference them later in options
172 | * Mark them as `categorical_feature` in booster options.
173 |
174 | Given the dataset file in the LibSVM format, where categories are index-encoded:
175 |
176 | ```
177 | 1 0:7 1:2 2:3 3:20 4:15 5:38 6:29 7:201
178 | 0 0:5 1:15 2:2 3:1859 4:1 5:156 6:164 7:2475
179 | 0 0:2 1:12 2:6 3:648 4:13 5:29 6:38 7:201
180 | 1 0:10 1:26 2:5 3:1235 4:14 5:82 6:205 7:931
181 | 0 0:6 1:18 2:1 3:737 4:12 5:224 6:162 7:2176
182 | 0 0:4 1:12 3:1845 4:18 5:83 6:49 7:1491
183 | 0 0:3 2:3 3:1652 4:20 5:2 6:180 7:332
184 | 0 0:3 1:21 2:3 3:2010 4:16 5:216 6:69 7:911
185 | 0 0:3 1:3 3:1555 4:1 5:84 6:81 7:1192
186 | 0 0:8 1:2 2:6 3:1008 4:16 5:216 6:228 7:130
187 | ```
188 |
189 | You can load and use them in the following way:
190 |
191 | ```java
192 | LGBMDataset ds = LGBMDataset.createFromFile("./src/test/resources/categorical.data", "", null);
193 | ds.setFeatureNames(new String[]{"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7"});
194 | String params = "objective=binary label=name:Classification categorical_feature=f0,f1,f2,f3,f4,f5,f6,f7";
195 | LGBMBooster booster = LGBMBooster.create(ds, params);
196 | for (int i=0; i<10; i++) {
197 | booster.updateOneIter();
198 | double[] eval1 = booster.getEval(0);
199 | System.out.println("train " + eval1[0]);
200 | }
201 | ```
202 |
203 | ### Position bias removal
204 |
205 | LightGBM 4.1+ can perform a [position-bias aware LTR/LambdaMART](https://lightgbm.readthedocs.io/en/latest/Advanced-Topics.html#support-for-position-bias-treatment) training. To perform it with lightgbm4j you need to explicitly define the `position` field as described in the upstream LightGBM docs:
206 |
207 | ```java
208 | float[] matrix = new float[] {
209 | // query group 1
210 | 1.0f, 2.0f, // doc1
211 | 3.0f, 4.0f, // doc2
212 | // query group 2
213 | 1.0f, 2.0f, // doc1
214 | 3.0f, 4.0f}; // doc2
215 | LGBMDataset ds = LGBMDataset.createFromMat(matrix, 4, 2, true, "", null);
216 | ds.setField("label", new float[] {1.0, 0.0, 1.0, 0.0}); // set relevance labels
217 | ds.setField("group", new int[] {2, 2}); // set document-to-group mapping
218 | ds.setField("position", new int[] {0, 1, 2, 3, 0, 1, 2, 3}); // bias classes
219 | LGBMBooster booster = LGBMBooster.create(ds, "objective=lambdarank");
220 | ```
221 |
222 | ### Custom objectives
223 |
224 | LightGBM4j supports using custom objective functions, but it doesn't provide any high-level wrappers as python API does.
225 |
226 | LightGBM needs a tuple of 1st and 2nd order derivatives (gradients and hessians) computed for each datapoint. With LightGBM4j it looks like this for an MSE metric:
227 |
228 | ```java
229 | LGBMDataset dataset = LGBMDataset.createFromFile("cancer.csv", "header=true label=name:Classification", null);
230 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=none metric=rmse label=name:Classification");
231 | // actual ground truth label values
232 | float y[] = dataset.getFieldFloat("label");
233 |
234 | for (int it=0; it<10; it++) {
235 | // predictions for current iteration
236 | double[] yhat = booster.getPredict(0); // 0 - training dataset
237 | float[] grad = new float[y.length];
238 | float[] hess = new float[y.length];
239 | for (int i=0; i 0);
276 | double pred2 = booster.predictForMatSingleRow(new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_NORMAL);
277 | assertTrue(pred2 > 0);
278 | }
279 | dataset.close();
280 | booster.close();
281 | ```
282 |
283 | #### Single-row fast prediction
284 |
285 | ```java
286 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
287 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
288 | booster.updateOneIter();
289 | booster.updateOneIter();
290 | booster.updateOneIter();
291 | LGBMBooster.FastConfig config = booster.predictForMatSingleRowFastInit(PredictionType.C_API_PREDICT_NORMAL, C_API_DTYPE_FLOAT32,9, "");
292 | double pred = booster.predictForMatSingleRowFast(config, new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_NORMAL);
293 | assertTrue(Double.isFinite(pred));
294 | config.close();
295 | dataset.close();
296 | booster.close();
297 |
298 | ```
299 |
300 | ## Supported platforms
301 |
302 | This code is tested to work well with Linux (Ubuntu 20.04), Windows (Server 2019) and MacOS 10.15/11. Mac M1 is also supported.
303 | Supported Java versions are 11, 17 and 21.
304 |
305 | ## LightGBM API Coverage
306 |
307 | Not all LightGBM API methods are covered in this wrapper. PRs are welcome!
308 |
309 | Supported methods:
310 | * [LGBM_BoosterAddValidData](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterAddValidData)
311 | * [LGBM_BoosterCreate](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterCreate)
312 | * [LGBM_BoosterCreateFromModelfile](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterCreateFromModelfile)
313 | * [LGBM_BoosterFree](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterFree)
314 | * [LGBM_BoosterGetEval](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetEval)
315 | * [LGBM_BoosterGetFeatureNames](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetFeatureNames)
316 | * [LGBM_BoosterFeatureImportance](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterFeatureImportance)
317 | * [LGBM_BoosterGetEvalNames](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetEvalNames)
318 | * [LGBM_BoosterGetNumFeature](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetNumFeature)
319 | * [LGBM_BoosterGetNumClasses](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetNumClasses)
320 | * [LGBM_BoosterGetNumPredict](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetNumPredict)
321 | * [LGBM_BoosterGetPredict](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetPredict)
322 | * [LGBM_BoosterLoadModelFromString](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterLoadModelFromString)
323 | * [LGBM_BoosterPredictForMat](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForMat)
324 | * [LGBM_BoosterPredictForMatSingleRow](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForMatSingleRow)
325 | * [LGBM_BoosterPredictForMatSingleRowFast](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForMatSingleRowFast)
326 | * [LGBM_BoosterPredictForMatSingleRowFastInit](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForMatSingleRowFastInit)
327 | * [LGBM_BoosterSaveModel](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterSaveModel)
328 | * [LGBM_BoosterSaveModelToString](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterSaveModelToString)
329 | * [LGBM_BoosterUpdateOneIter](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterUpdateOneIter)
330 | * [LGBM_BoosterUpdateOneIterCustom](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterUpdateOneIterCustom)
331 | * [LGBM_DatasetCreateFromFile](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromFile)
332 | * [LGBM_DatasetCreateFromMat](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromMat)
333 | * [LGBM_DatasetFree](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetFree)
334 | * [LGBM_DatasetGetFeatureNames](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetGetFeatureNames)
335 | * [LGBM_DatasetGetNumData](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetGetNumData)
336 | * [LGBM_DatasetGetNumFeature](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetGetNumFeature)
337 | * [LGBM_GetLastError](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_GetLastError)
338 | * [LGBM_DatasetSetFeatureNames](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetSetFeatureNames)
339 | * [LGBM_DatasetSetField](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetSetField)
340 | * [LGBM_DatasetDumpText](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetDumpText)
341 |
342 | Not yet supported:
343 | * [LGBM_BoosterCalcNumPredict](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterCalcNumPredict)
344 | * [LGBM_BoosterDumpModel](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterDumpModel)
345 | * [LGBM_BoosterFreePredictSparse](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterFreePredictSparse)
346 | * [LGBM_BoosterGetCurrentIteration](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetCurrentIteration)
347 | * [LGBM_BoosterGetEvalCounts](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetEvalCounts)
348 | * [LGBM_BoosterGetLeafValue](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetLeafValue)
349 | * [LGBM_BoosterGetLowerBoundValue](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetLowerBoundValue)
350 | * [LGBM_BoosterGetUpperBoundValue](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterGetUpperBoundValue)
351 | * [LGBM_BoosterMerge](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterMerge)
352 | * [LGBM_BoosterNumberOfTotalModel](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterNumberOfTotalModel)
353 | * [LGBM_BoosterNumModelPerIteration](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterNumModelPerIteration)
354 | * [LGBM_BoosterPredictForCSC](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForCSC)
355 | * [LGBM_BoosterPredictForCSR](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForCSR)
356 | * [LGBM_BoosterPredictForCSRSingleRow](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForCSRSingleRow)
357 | * [LGBM_BoosterPredictForCSRSingleRowFast](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForCSRSingleRowFast)
358 | * [LGBM_BoosterPredictForCSRSingleRowFastInit](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForCSRSingleRowFastInit)
359 | * [LGBM_BoosterPredictForFile](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForFile)
360 | * [LGBM_BoosterPredictForMats](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictForMats)
361 | * [LGBM_BoosterPredictSparseOutput](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterPredictSparseOutput)
362 | * [LGBM_BoosterRefit](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterRefit)
363 | * [LGBM_BoosterResetParameter](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterResetParameter)
364 | * [LGBM_BoosterResetTrainingData](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterResetTrainingData)
365 | * [LGBM_BoosterRollbackOneIter](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterRollbackOneIter)
366 | * [LGBM_BoosterSetLeafValue](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterSetLeafValue)
367 | * [LGBM_BoosterShuffleModels](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_BoosterShuffleModels)
368 | * [LGBM_DatasetAddFeaturesFrom](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetAddFeaturesFrom)
369 | * [LGBM_DatasetCreateByReference](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateByReference)
370 | * [LGBM_DatasetCreateFromCSC](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromCSC)
371 | * [LGBM_DatasetCreateFromCSR](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromCSR)
372 | * [LGBM_DatasetCreateFromCSRFunc](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromCSRFunc)
373 | * [LGBM_DatasetCreateFromMats](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromMats)
374 | * [LGBM_DatasetCreateFromSampledColumn](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetCreateFromSampledColumn)
375 | * [LGBM_DatasetGetField](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetGetField)
376 | * [LGBM_DatasetGetSubset](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetGetSubset)
377 | * [LGBM_DatasetPushRows](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetPushRows)
378 | * [LGBM_DatasetPushRowsByCSR](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetPushRowsByCSR)
379 | * [LGBM_DatasetSaveBinary](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetSaveBinary)
380 | * [LGBM_DatasetUpdateParamChecking](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_DatasetUpdateParamChecking)
381 | * [LGBM_FastConfigFree](https://lightgbm.readthedocs.io/en/latest/C-API.html#c.LGBM_FastConfigFree)
382 |
383 |
384 | ## License
385 |
386 | As LightGBM4j repackages bits of SWIG wrapper code from original LightGBM authors, it
387 | also uses exactly the [same license](https://github.com/microsoft/LightGBM/blob/master/LICENSE).
388 |
389 | ```
390 | The MIT License (MIT)
391 |
392 | Copyright (c) Microsoft Corporation
393 |
394 | Permission is hereby granted, free of charge, to any person obtaining a copy
395 | of this software and associated documentation files (the "Software"), to deal
396 | in the Software without restriction, including without limitation the rights
397 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
398 | copies of the Software, and to permit persons to whom the Software is
399 | furnished to do so, subject to the following conditions:
400 |
401 | The above copyright notice and this permission notice shall be included in all
402 | copies or substantial portions of the Software.
403 |
404 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
405 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
406 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
407 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
408 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
409 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
410 | SOFTWARE.
411 | ```
412 |
--------------------------------------------------------------------------------
/generate_checksums.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | echo "removing all checksums"
4 | find . -name "*.md5" -exec rm {} \;
5 |
6 | for f in `find src/main/resources/ -type f`; do
7 | cat $f | md5sum | awk '{ printf $1 }'> $f.md5
8 | echo "processed $f"
9 | done
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.metarank
8 | lightgbm4j
9 | 4.4.0-1
10 |
11 | LightGBM4j: Java LightGBM wrapper
12 | A high-level wrapper for LightGBM toolkit
13 | https://github.com/metarank/lightgbm4j
14 |
15 |
16 |
17 | Roman Grebennikov
18 | grv@dfdx.me
19 | Metarank
20 | https://github.com/metarank
21 |
22 |
23 |
24 |
25 |
26 | Apache License, Version 2.0
27 | http://www.apache.org/licenses/LICENSE-2.0.txt
28 | repo
29 |
30 |
31 |
32 |
33 | https://github.com/metarank/lightgbm4j.git
34 |
35 |
36 |
37 | 11
38 | 11
39 |
40 |
41 |
42 | org.junit.platform
43 | junit-platform-commons
44 | 1.10.1
45 | test
46 |
47 |
48 | org.junit.jupiter
49 | junit-jupiter
50 | 5.10.1
51 | test
52 |
53 |
54 | org.junit.jupiter
55 | junit-jupiter-params
56 | 5.10.1
57 | test
58 |
59 |
60 | org.slf4j
61 | slf4j-api
62 | 2.0.12
63 |
64 |
65 | ch.qos.logback
66 | logback-classic
67 | 1.5.6
68 | test
69 |
70 |
71 |
72 |
73 |
74 | ${project.basedir}/src/test/resources
75 |
76 |
77 | ${project.basedir}/src/main/resources
78 |
79 |
80 |
81 |
82 | org.apache.maven.plugins
83 | maven-surefire-plugin
84 | 2.22.2
85 |
86 |
87 | org.sonatype.plugins
88 | nexus-staging-maven-plugin
89 | 1.6.13
90 | true
91 |
92 | ossrh
93 | https://oss.sonatype.org/
94 | true
95 |
96 |
97 |
98 | org.apache.maven.plugins
99 | maven-compiler-plugin
100 |
101 | 1.8
102 | 1.8
103 |
104 |
105 |
106 | org.apache.maven.plugins
107 | maven-source-plugin
108 | 3.2.1
109 |
110 |
111 | attach-sources
112 |
113 | jar-no-fork
114 |
115 |
116 |
117 |
118 |
119 | org.apache.maven.plugins
120 | maven-javadoc-plugin
121 | 3.4.1
122 |
123 |
124 | attach-javadocs
125 |
126 | jar
127 |
128 |
129 |
130 |
131 |
132 | org.apache.maven.plugins
133 | maven-gpg-plugin
134 | 3.0.1
135 |
136 |
137 | sign-artifacts
138 | verify
139 |
140 | sign
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 | maven_central
150 | Maven Central
151 | https://repo.maven.apache.org/maven2/
152 |
153 |
154 |
155 |
156 |
157 | ossrh
158 | https://oss.sonatype.org/content/repositories/snapshots
159 |
160 |
161 | ossrh
162 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
163 |
164 |
165 |
166 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/CSRDirect.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class CSRDirect {
12 | private transient long swigCPtr;
13 | protected transient boolean swigCMemOwn;
14 |
15 | protected CSRDirect(long cPtr, boolean cMemoryOwn) {
16 | swigCMemOwn = cMemoryOwn;
17 | swigCPtr = cPtr;
18 | }
19 |
20 | protected static long getCPtr(CSRDirect obj) {
21 | return (obj == null) ? 0 : obj.swigCPtr;
22 | }
23 |
24 | protected static long swigRelease(CSRDirect obj) {
25 | long ptr = 0;
26 | if (obj != null) {
27 | if (!obj.swigCMemOwn)
28 | throw new RuntimeException("Cannot release ownership as memory is not owned");
29 | ptr = obj.swigCPtr;
30 | obj.swigCMemOwn = false;
31 | obj.delete();
32 | }
33 | return ptr;
34 | }
35 |
36 | @SuppressWarnings({"deprecation", "removal"})
37 | protected void finalize() {
38 | delete();
39 | }
40 |
41 | public synchronized void delete() {
42 | if (swigCPtr != 0) {
43 | if (swigCMemOwn) {
44 | swigCMemOwn = false;
45 | lightgbmlibJNI.delete_CSRDirect(swigCPtr);
46 | }
47 | swigCPtr = 0;
48 | }
49 | }
50 |
51 | public void setIndices(int[] value) {
52 | lightgbmlibJNI.CSRDirect_indices_set(swigCPtr, this, value);
53 | }
54 |
55 | public int[] getIndices() {
56 | return lightgbmlibJNI.CSRDirect_indices_get(swigCPtr, this);
57 | }
58 |
59 | public void setValues(double[] value) {
60 | lightgbmlibJNI.CSRDirect_values_set(swigCPtr, this, value);
61 | }
62 |
63 | public double[] getValues() {
64 | return lightgbmlibJNI.CSRDirect_values_get(swigCPtr, this);
65 | }
66 |
67 | public void setIndices0(SWIGTYPE_p_int value) {
68 | lightgbmlibJNI.CSRDirect_indices0_set(swigCPtr, this, SWIGTYPE_p_int.getCPtr(value));
69 | }
70 |
71 | public SWIGTYPE_p_int getIndices0() {
72 | long cPtr = lightgbmlibJNI.CSRDirect_indices0_get(swigCPtr, this);
73 | return (cPtr == 0) ? null : new SWIGTYPE_p_int(cPtr, false);
74 | }
75 |
76 | public void setValues0(SWIGTYPE_p_double value) {
77 | lightgbmlibJNI.CSRDirect_values0_set(swigCPtr, this, SWIGTYPE_p_double.getCPtr(value));
78 | }
79 |
80 | public SWIGTYPE_p_double getValues0() {
81 | long cPtr = lightgbmlibJNI.CSRDirect_values0_get(swigCPtr, this);
82 | return (cPtr == 0) ? null : new SWIGTYPE_p_double(cPtr, false);
83 | }
84 |
85 | public void setSize(int value) {
86 | lightgbmlibJNI.CSRDirect_size_set(swigCPtr, this, value);
87 | }
88 |
89 | public int getSize() {
90 | return lightgbmlibJNI.CSRDirect_size_get(swigCPtr, this);
91 | }
92 |
93 | public CSRDirect() {
94 | this(lightgbmlibJNI.new_CSRDirect(), true);
95 | }
96 |
97 | }
98 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/PredictionType.java:
--------------------------------------------------------------------------------
1 | package com.microsoft.ml.lightgbm;
2 |
3 | public enum PredictionType {
4 |
5 | C_API_PREDICT_NORMAL(lightgbmlibJNI.C_API_PREDICT_NORMAL_get(), "Normal prediction"),
6 | C_API_PREDICT_RAW_SCORE(lightgbmlibJNI.C_API_PREDICT_RAW_SCORE_get(), "Raw score"),
7 | C_API_PREDICT_LEAF_INDEX(lightgbmlibJNI.C_API_PREDICT_LEAF_INDEX_get(), "Leaf index"),
8 | C_API_PREDICT_CONTRIB(lightgbmlibJNI.C_API_PREDICT_CONTRIB_get(), "Feature contributions (SHAP values)");
9 |
10 | private final int type;
11 | private final String description;
12 |
13 | PredictionType(int type, String description) {
14 | this.type = type;
15 | this.description = description;
16 | }
17 |
18 | public int getType() {
19 | return type;
20 | }
21 |
22 | public String getDescription() {
23 | return description;
24 | }
25 |
26 | public boolean equals(PredictionType that) {
27 | return this.type == that.type;
28 | }
29 |
30 | @Override
31 | public String toString() {
32 | return "PredictionType{" +
33 | "type=" + type +
34 | ", description='" + description + '\'' +
35 | '}';
36 | }
37 | }
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_ArrowArray.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_ArrowArray {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_ArrowArray(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_ArrowArray() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_ArrowArray obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_ArrowArray obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_ArrowSchema.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_ArrowSchema {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_ArrowSchema(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_ArrowSchema() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_ArrowSchema obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_ArrowSchema obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_double.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_double {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_double(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_double() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_double obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_double obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_f_p_q_const__char__void.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_f_p_q_const__char__void {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_f_p_q_const__char__void(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_f_p_q_const__char__void() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_f_p_q_const__char__void obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_f_p_q_const__char__void obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_float.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_float {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_float(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_float() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_float obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_float obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_int.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_int {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_int(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_int() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_int obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_int obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_long.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_long {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_long(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_long() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_long obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_long obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_long_long.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_long_long {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_long_long(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_long_long() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_long_long obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_long_long obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_p_char.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_p_char {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_p_char(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_p_char() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_p_char obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_p_char obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_p_double.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_p_double {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_p_double(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_p_double() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_p_double obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_p_double obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_p_float.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_p_float {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_p_float(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_p_float() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_p_float obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_p_float obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_p_int.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_p_int {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_p_int(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_p_int() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_p_int obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_p_int obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_p_void.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_p_void {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_p_void(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_p_void() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_p_void obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_p_void obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_size_t.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_size_t {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_size_t(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_size_t() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_size_t obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_size_t obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_unsigned_char.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_unsigned_char {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_unsigned_char(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_unsigned_char() {
19 | swigCPtr = 0;
20 | }
21 |
22 | protected static long getCPtr(SWIGTYPE_p_unsigned_char obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_unsigned_char obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/SWIGTYPE_p_void.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class SWIGTYPE_p_void {
12 | private transient long swigCPtr;
13 |
14 | protected SWIGTYPE_p_void(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
15 | swigCPtr = cPtr;
16 | }
17 |
18 | protected SWIGTYPE_p_void() {
19 | swigCPtr = 0;
20 | }
21 |
22 | public static long getCPtr(SWIGTYPE_p_void obj) {
23 | return (obj == null) ? 0 : obj.swigCPtr;
24 | }
25 |
26 | protected static long swigRelease(SWIGTYPE_p_void obj) {
27 | return (obj == null) ? 0 : obj.swigCPtr;
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/doubleChunkedArray.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class doubleChunkedArray {
12 | private transient long swigCPtr;
13 | protected transient boolean swigCMemOwn;
14 |
15 | protected doubleChunkedArray(long cPtr, boolean cMemoryOwn) {
16 | swigCMemOwn = cMemoryOwn;
17 | swigCPtr = cPtr;
18 | }
19 |
20 | protected static long getCPtr(doubleChunkedArray obj) {
21 | return (obj == null) ? 0 : obj.swigCPtr;
22 | }
23 |
24 | protected static long swigRelease(doubleChunkedArray obj) {
25 | long ptr = 0;
26 | if (obj != null) {
27 | if (!obj.swigCMemOwn)
28 | throw new RuntimeException("Cannot release ownership as memory is not owned");
29 | ptr = obj.swigCPtr;
30 | obj.swigCMemOwn = false;
31 | obj.delete();
32 | }
33 | return ptr;
34 | }
35 |
36 | @SuppressWarnings({"deprecation", "removal"})
37 | protected void finalize() {
38 | delete();
39 | }
40 |
41 | public synchronized void delete() {
42 | if (swigCPtr != 0) {
43 | if (swigCMemOwn) {
44 | swigCMemOwn = false;
45 | lightgbmlibJNI.delete_doubleChunkedArray(swigCPtr);
46 | }
47 | swigCPtr = 0;
48 | }
49 | }
50 |
51 | public doubleChunkedArray(long chunk_size) {
52 | this(lightgbmlibJNI.new_doubleChunkedArray(chunk_size), true);
53 | }
54 |
55 | public void add(double value) {
56 | lightgbmlibJNI.doubleChunkedArray_add(swigCPtr, this, value);
57 | }
58 |
59 | public long get_add_count() {
60 | return lightgbmlibJNI.doubleChunkedArray_get_add_count(swigCPtr, this);
61 | }
62 |
63 | public long get_chunks_count() {
64 | return lightgbmlibJNI.doubleChunkedArray_get_chunks_count(swigCPtr, this);
65 | }
66 |
67 | public long get_last_chunk_add_count() {
68 | return lightgbmlibJNI.doubleChunkedArray_get_last_chunk_add_count(swigCPtr, this);
69 | }
70 |
71 | public long get_chunk_size() {
72 | return lightgbmlibJNI.doubleChunkedArray_get_chunk_size(swigCPtr, this);
73 | }
74 |
75 | public SWIGTYPE_p_p_double data() {
76 | long cPtr = lightgbmlibJNI.doubleChunkedArray_data(swigCPtr, this);
77 | return (cPtr == 0) ? null : new SWIGTYPE_p_p_double(cPtr, false);
78 | }
79 |
80 | public SWIGTYPE_p_p_void data_as_void() {
81 | long cPtr = lightgbmlibJNI.doubleChunkedArray_data_as_void(swigCPtr, this);
82 | return (cPtr == 0) ? null : new SWIGTYPE_p_p_void(cPtr, false);
83 | }
84 |
85 | public void coalesce_to(SWIGTYPE_p_double other, boolean all_valid_addresses) {
86 | lightgbmlibJNI.doubleChunkedArray_coalesce_to__SWIG_0(swigCPtr, this, SWIGTYPE_p_double.getCPtr(other), all_valid_addresses);
87 | }
88 |
89 | public void coalesce_to(SWIGTYPE_p_double other) {
90 | lightgbmlibJNI.doubleChunkedArray_coalesce_to__SWIG_1(swigCPtr, this, SWIGTYPE_p_double.getCPtr(other));
91 | }
92 |
93 | public double getitem(long chunk_index, long index_within_chunk, double on_fail_value) {
94 | return lightgbmlibJNI.doubleChunkedArray_getitem(swigCPtr, this, chunk_index, index_within_chunk, on_fail_value);
95 | }
96 |
97 | public int setitem(long chunk_index, long index_within_chunk, double value) {
98 | return lightgbmlibJNI.doubleChunkedArray_setitem(swigCPtr, this, chunk_index, index_within_chunk, value);
99 | }
100 |
101 | public void clear() {
102 | lightgbmlibJNI.doubleChunkedArray_clear(swigCPtr, this);
103 | }
104 |
105 | public void release() {
106 | lightgbmlibJNI.doubleChunkedArray_release(swigCPtr, this);
107 | }
108 |
109 | public boolean within_bounds(long chunk_index, long index_within_chunk) {
110 | return lightgbmlibJNI.doubleChunkedArray_within_bounds(swigCPtr, this, chunk_index, index_within_chunk);
111 | }
112 |
113 | public void new_chunk() {
114 | lightgbmlibJNI.doubleChunkedArray_new_chunk(swigCPtr, this);
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/floatChunkedArray.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class floatChunkedArray {
12 | private transient long swigCPtr;
13 | protected transient boolean swigCMemOwn;
14 |
15 | protected floatChunkedArray(long cPtr, boolean cMemoryOwn) {
16 | swigCMemOwn = cMemoryOwn;
17 | swigCPtr = cPtr;
18 | }
19 |
20 | protected static long getCPtr(floatChunkedArray obj) {
21 | return (obj == null) ? 0 : obj.swigCPtr;
22 | }
23 |
24 | protected static long swigRelease(floatChunkedArray obj) {
25 | long ptr = 0;
26 | if (obj != null) {
27 | if (!obj.swigCMemOwn)
28 | throw new RuntimeException("Cannot release ownership as memory is not owned");
29 | ptr = obj.swigCPtr;
30 | obj.swigCMemOwn = false;
31 | obj.delete();
32 | }
33 | return ptr;
34 | }
35 |
36 | @SuppressWarnings({"deprecation", "removal"})
37 | protected void finalize() {
38 | delete();
39 | }
40 |
41 | public synchronized void delete() {
42 | if (swigCPtr != 0) {
43 | if (swigCMemOwn) {
44 | swigCMemOwn = false;
45 | lightgbmlibJNI.delete_floatChunkedArray(swigCPtr);
46 | }
47 | swigCPtr = 0;
48 | }
49 | }
50 |
51 | public floatChunkedArray(long chunk_size) {
52 | this(lightgbmlibJNI.new_floatChunkedArray(chunk_size), true);
53 | }
54 |
55 | public void add(float value) {
56 | lightgbmlibJNI.floatChunkedArray_add(swigCPtr, this, value);
57 | }
58 |
59 | public long get_add_count() {
60 | return lightgbmlibJNI.floatChunkedArray_get_add_count(swigCPtr, this);
61 | }
62 |
63 | public long get_chunks_count() {
64 | return lightgbmlibJNI.floatChunkedArray_get_chunks_count(swigCPtr, this);
65 | }
66 |
67 | public long get_last_chunk_add_count() {
68 | return lightgbmlibJNI.floatChunkedArray_get_last_chunk_add_count(swigCPtr, this);
69 | }
70 |
71 | public long get_chunk_size() {
72 | return lightgbmlibJNI.floatChunkedArray_get_chunk_size(swigCPtr, this);
73 | }
74 |
75 | public SWIGTYPE_p_p_float data() {
76 | long cPtr = lightgbmlibJNI.floatChunkedArray_data(swigCPtr, this);
77 | return (cPtr == 0) ? null : new SWIGTYPE_p_p_float(cPtr, false);
78 | }
79 |
80 | public SWIGTYPE_p_p_void data_as_void() {
81 | long cPtr = lightgbmlibJNI.floatChunkedArray_data_as_void(swigCPtr, this);
82 | return (cPtr == 0) ? null : new SWIGTYPE_p_p_void(cPtr, false);
83 | }
84 |
85 | public void coalesce_to(SWIGTYPE_p_float other, boolean all_valid_addresses) {
86 | lightgbmlibJNI.floatChunkedArray_coalesce_to__SWIG_0(swigCPtr, this, SWIGTYPE_p_float.getCPtr(other), all_valid_addresses);
87 | }
88 |
89 | public void coalesce_to(SWIGTYPE_p_float other) {
90 | lightgbmlibJNI.floatChunkedArray_coalesce_to__SWIG_1(swigCPtr, this, SWIGTYPE_p_float.getCPtr(other));
91 | }
92 |
93 | public float getitem(long chunk_index, long index_within_chunk, float on_fail_value) {
94 | return lightgbmlibJNI.floatChunkedArray_getitem(swigCPtr, this, chunk_index, index_within_chunk, on_fail_value);
95 | }
96 |
97 | public int setitem(long chunk_index, long index_within_chunk, float value) {
98 | return lightgbmlibJNI.floatChunkedArray_setitem(swigCPtr, this, chunk_index, index_within_chunk, value);
99 | }
100 |
101 | public void clear() {
102 | lightgbmlibJNI.floatChunkedArray_clear(swigCPtr, this);
103 | }
104 |
105 | public void release() {
106 | lightgbmlibJNI.floatChunkedArray_release(swigCPtr, this);
107 | }
108 |
109 | public boolean within_bounds(long chunk_index, long index_within_chunk) {
110 | return lightgbmlibJNI.floatChunkedArray_within_bounds(swigCPtr, this, chunk_index, index_within_chunk);
111 | }
112 |
113 | public void new_chunk() {
114 | lightgbmlibJNI.floatChunkedArray_new_chunk(swigCPtr, this);
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/int32ChunkedArray.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class int32ChunkedArray {
12 | private transient long swigCPtr;
13 | protected transient boolean swigCMemOwn;
14 |
15 | protected int32ChunkedArray(long cPtr, boolean cMemoryOwn) {
16 | swigCMemOwn = cMemoryOwn;
17 | swigCPtr = cPtr;
18 | }
19 |
20 | protected static long getCPtr(int32ChunkedArray obj) {
21 | return (obj == null) ? 0 : obj.swigCPtr;
22 | }
23 |
24 | protected static long swigRelease(int32ChunkedArray obj) {
25 | long ptr = 0;
26 | if (obj != null) {
27 | if (!obj.swigCMemOwn)
28 | throw new RuntimeException("Cannot release ownership as memory is not owned");
29 | ptr = obj.swigCPtr;
30 | obj.swigCMemOwn = false;
31 | obj.delete();
32 | }
33 | return ptr;
34 | }
35 |
36 | @SuppressWarnings({"deprecation", "removal"})
37 | protected void finalize() {
38 | delete();
39 | }
40 |
41 | public synchronized void delete() {
42 | if (swigCPtr != 0) {
43 | if (swigCMemOwn) {
44 | swigCMemOwn = false;
45 | lightgbmlibJNI.delete_int32ChunkedArray(swigCPtr);
46 | }
47 | swigCPtr = 0;
48 | }
49 | }
50 |
51 | public int32ChunkedArray(long chunk_size) {
52 | this(lightgbmlibJNI.new_int32ChunkedArray(chunk_size), true);
53 | }
54 |
55 | public void add(int value) {
56 | lightgbmlibJNI.int32ChunkedArray_add(swigCPtr, this, value);
57 | }
58 |
59 | public long get_add_count() {
60 | return lightgbmlibJNI.int32ChunkedArray_get_add_count(swigCPtr, this);
61 | }
62 |
63 | public long get_chunks_count() {
64 | return lightgbmlibJNI.int32ChunkedArray_get_chunks_count(swigCPtr, this);
65 | }
66 |
67 | public long get_last_chunk_add_count() {
68 | return lightgbmlibJNI.int32ChunkedArray_get_last_chunk_add_count(swigCPtr, this);
69 | }
70 |
71 | public long get_chunk_size() {
72 | return lightgbmlibJNI.int32ChunkedArray_get_chunk_size(swigCPtr, this);
73 | }
74 |
75 | public SWIGTYPE_p_p_int data() {
76 | long cPtr = lightgbmlibJNI.int32ChunkedArray_data(swigCPtr, this);
77 | return (cPtr == 0) ? null : new SWIGTYPE_p_p_int(cPtr, false);
78 | }
79 |
80 | public SWIGTYPE_p_p_void data_as_void() {
81 | long cPtr = lightgbmlibJNI.int32ChunkedArray_data_as_void(swigCPtr, this);
82 | return (cPtr == 0) ? null : new SWIGTYPE_p_p_void(cPtr, false);
83 | }
84 |
85 | public void coalesce_to(SWIGTYPE_p_int other, boolean all_valid_addresses) {
86 | lightgbmlibJNI.int32ChunkedArray_coalesce_to__SWIG_0(swigCPtr, this, SWIGTYPE_p_int.getCPtr(other), all_valid_addresses);
87 | }
88 |
89 | public void coalesce_to(SWIGTYPE_p_int other) {
90 | lightgbmlibJNI.int32ChunkedArray_coalesce_to__SWIG_1(swigCPtr, this, SWIGTYPE_p_int.getCPtr(other));
91 | }
92 |
93 | public int getitem(long chunk_index, long index_within_chunk, int on_fail_value) {
94 | return lightgbmlibJNI.int32ChunkedArray_getitem(swigCPtr, this, chunk_index, index_within_chunk, on_fail_value);
95 | }
96 |
97 | public int setitem(long chunk_index, long index_within_chunk, int value) {
98 | return lightgbmlibJNI.int32ChunkedArray_setitem(swigCPtr, this, chunk_index, index_within_chunk, value);
99 | }
100 |
101 | public void clear() {
102 | lightgbmlibJNI.int32ChunkedArray_clear(swigCPtr, this);
103 | }
104 |
105 | public void release() {
106 | lightgbmlibJNI.int32ChunkedArray_release(swigCPtr, this);
107 | }
108 |
109 | public boolean within_bounds(long chunk_index, long index_within_chunk) {
110 | return lightgbmlibJNI.int32ChunkedArray_within_bounds(swigCPtr, this, chunk_index, index_within_chunk);
111 | }
112 |
113 | public void new_chunk() {
114 | lightgbmlibJNI.int32ChunkedArray_new_chunk(swigCPtr, this);
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/lightgbmlibConstants.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public interface lightgbmlibConstants {
12 | public final static int C_API_DTYPE_FLOAT32 = lightgbmlibJNI.C_API_DTYPE_FLOAT32_get();
13 | public final static int C_API_DTYPE_FLOAT64 = lightgbmlibJNI.C_API_DTYPE_FLOAT64_get();
14 | public final static int C_API_DTYPE_INT32 = lightgbmlibJNI.C_API_DTYPE_INT32_get();
15 | public final static int C_API_DTYPE_INT64 = lightgbmlibJNI.C_API_DTYPE_INT64_get();
16 | public final static int C_API_PREDICT_NORMAL = lightgbmlibJNI.C_API_PREDICT_NORMAL_get();
17 | public final static int C_API_PREDICT_RAW_SCORE = lightgbmlibJNI.C_API_PREDICT_RAW_SCORE_get();
18 | public final static int C_API_PREDICT_LEAF_INDEX = lightgbmlibJNI.C_API_PREDICT_LEAF_INDEX_get();
19 | public final static int C_API_PREDICT_CONTRIB = lightgbmlibJNI.C_API_PREDICT_CONTRIB_get();
20 | public final static int C_API_MATRIX_TYPE_CSR = lightgbmlibJNI.C_API_MATRIX_TYPE_CSR_get();
21 | public final static int C_API_MATRIX_TYPE_CSC = lightgbmlibJNI.C_API_MATRIX_TYPE_CSC_get();
22 | public final static int C_API_FEATURE_IMPORTANCE_SPLIT = lightgbmlibJNI.C_API_FEATURE_IMPORTANCE_SPLIT_get();
23 | public final static int C_API_FEATURE_IMPORTANCE_GAIN = lightgbmlibJNI.C_API_FEATURE_IMPORTANCE_GAIN_get();
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/ml/lightgbm/lightgbmlibJNI.java:
--------------------------------------------------------------------------------
1 | /* ----------------------------------------------------------------------------
2 | * This file was automatically generated by SWIG (https://www.swig.org).
3 | * Version 4.2.1
4 | *
5 | * Do not make changes to this file unless you know what you are doing - modify
6 | * the SWIG interface file instead.
7 | * ----------------------------------------------------------------------------- */
8 |
9 | package com.microsoft.ml.lightgbm;
10 |
11 | public class lightgbmlibJNI {
12 | public final static native int C_API_DTYPE_FLOAT32_get();
13 | public final static native int C_API_DTYPE_FLOAT64_get();
14 | public final static native int C_API_DTYPE_INT32_get();
15 | public final static native int C_API_DTYPE_INT64_get();
16 | public final static native int C_API_PREDICT_NORMAL_get();
17 | public final static native int C_API_PREDICT_RAW_SCORE_get();
18 | public final static native int C_API_PREDICT_LEAF_INDEX_get();
19 | public final static native int C_API_PREDICT_CONTRIB_get();
20 | public final static native int C_API_MATRIX_TYPE_CSR_get();
21 | public final static native int C_API_MATRIX_TYPE_CSC_get();
22 | public final static native int C_API_FEATURE_IMPORTANCE_SPLIT_get();
23 | public final static native int C_API_FEATURE_IMPORTANCE_GAIN_get();
24 | public final static native String LGBM_GetLastError();
25 | public final static native int LGBM_DumpParamAliases(long jarg1, long jarg2, String jarg3);
26 | public final static native int LGBM_RegisterLogCallback(long jarg1);
27 | public final static native int LGBM_GetSampleCount(int jarg1, String jarg2, long jarg3);
28 | public final static native int LGBM_SampleIndices(int jarg1, String jarg2, long jarg3, long jarg4);
29 | public final static native int LGBM_ByteBufferGetAt(long jarg1, int jarg2, long jarg3);
30 | public final static native int LGBM_ByteBufferFree(long jarg1);
31 | public final static native int LGBM_DatasetCreateFromFile(String jarg1, String jarg2, long jarg3, long jarg4);
32 | public final static native int LGBM_DatasetCreateFromSampledColumn(long jarg1, long jarg2, int jarg3, long jarg4, int jarg5, int jarg6, long jarg7, String jarg8, long jarg9);
33 | public final static native int LGBM_DatasetCreateByReference(long jarg1, long jarg2, long jarg3);
34 | public final static native int LGBM_DatasetInitStreaming(long jarg1, int jarg2, int jarg3, int jarg4, int jarg5, int jarg6, int jarg7);
35 | public final static native int LGBM_DatasetCreateFromSerializedReference(long jarg1, int jarg2, long jarg3, int jarg4, String jarg5, long jarg6);
36 | public final static native int LGBM_DatasetPushRows(long jarg1, long jarg2, int jarg3, int jarg4, int jarg5, int jarg6);
37 | public final static native int LGBM_DatasetPushRowsWithMetadata(long jarg1, long jarg2, int jarg3, int jarg4, int jarg5, int jarg6, long jarg7, long jarg8, long jarg9, long jarg10, int jarg11);
38 | public final static native int LGBM_DatasetPushRowsByCSR(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9, long jarg10);
39 | public final static native int LGBM_DatasetPushRowsByCSRWithMetadata(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9, long jarg10, long jarg11, long jarg12, long jarg13, int jarg14);
40 | public final static native int LGBM_DatasetSetWaitForManualFinish(long jarg1, int jarg2);
41 | public final static native int LGBM_DatasetMarkFinished(long jarg1);
42 | public final static native int LGBM_DatasetCreateFromCSR(long jarg1, int jarg2, long jarg3, long jarg4, int jarg5, long jarg6, long jarg7, long jarg8, String jarg9, long jarg10, long jarg11);
43 | public final static native int LGBM_DatasetCreateFromCSRFunc(long jarg1, int jarg2, long jarg3, String jarg4, long jarg5, long jarg6);
44 | public final static native int LGBM_DatasetCreateFromCSC(long jarg1, int jarg2, long jarg3, long jarg4, int jarg5, long jarg6, long jarg7, long jarg8, String jarg9, long jarg10, long jarg11);
45 | public final static native int LGBM_DatasetCreateFromMat(long jarg1, int jarg2, int jarg3, int jarg4, int jarg5, String jarg6, long jarg7, long jarg8);
46 | public final static native int LGBM_DatasetCreateFromMats(int jarg1, long jarg2, int jarg3, long jarg4, int jarg5, int jarg6, String jarg7, long jarg8, long jarg9);
47 | public final static native int LGBM_DatasetCreateFromArrow(long jarg1, long jarg2, long jarg3, String jarg4, long jarg5, long jarg6);
48 | public final static native int LGBM_DatasetGetSubset(long jarg1, long jarg2, int jarg3, String jarg4, long jarg5);
49 | public final static native int LGBM_DatasetSetFeatureNames(long jarg1, String[] jarg2, int jarg3);
50 | public final static native int LGBM_DatasetGetFeatureNames(long jarg1, int jarg2, long jarg3, long jarg4, long jarg5, String[] jarg6);
51 | public final static native int LGBM_DatasetFree(long jarg1);
52 | public final static native int LGBM_DatasetSaveBinary(long jarg1, String jarg2);
53 | public final static native int LGBM_DatasetSerializeReferenceToBinary(long jarg1, long jarg2, long jarg3);
54 | public final static native int LGBM_DatasetDumpText(long jarg1, String jarg2);
55 | public final static native int LGBM_DatasetSetField(long jarg1, String jarg2, long jarg3, int jarg4, int jarg5);
56 | public final static native int LGBM_DatasetSetFieldFromArrow(long jarg1, String jarg2, long jarg3, long jarg4, long jarg5);
57 | public final static native int LGBM_DatasetGetField(long jarg1, String jarg2, long jarg3, long jarg4, long jarg5);
58 | public final static native int LGBM_DatasetUpdateParamChecking(String jarg1, String jarg2);
59 | public final static native int LGBM_DatasetGetNumData(long jarg1, long jarg2);
60 | public final static native int LGBM_DatasetGetNumFeature(long jarg1, long jarg2);
61 | public final static native int LGBM_DatasetGetFeatureNumBin(long jarg1, int jarg2, long jarg3);
62 | public final static native int LGBM_DatasetAddFeaturesFrom(long jarg1, long jarg2);
63 | public final static native int LGBM_BoosterGetLinear(long jarg1, long jarg2);
64 | public final static native int LGBM_BoosterCreate(long jarg1, String jarg2, long jarg3);
65 | public final static native int LGBM_BoosterCreateFromModelfile(String jarg1, long jarg2, long jarg3);
66 | public final static native int LGBM_BoosterLoadModelFromString(String jarg1, long jarg2, long jarg3);
67 | public final static native int LGBM_BoosterGetLoadedParam(long jarg1, long jarg2, long jarg3, String jarg4);
68 | public final static native int LGBM_BoosterFree(long jarg1);
69 | public final static native int LGBM_BoosterShuffleModels(long jarg1, int jarg2, int jarg3);
70 | public final static native int LGBM_BoosterMerge(long jarg1, long jarg2);
71 | public final static native int LGBM_BoosterAddValidData(long jarg1, long jarg2);
72 | public final static native int LGBM_BoosterResetTrainingData(long jarg1, long jarg2);
73 | public final static native int LGBM_BoosterResetParameter(long jarg1, String jarg2);
74 | public final static native int LGBM_BoosterGetNumClasses(long jarg1, long jarg2);
75 | public final static native int LGBM_BoosterUpdateOneIter(long jarg1, long jarg2);
76 | public final static native int LGBM_BoosterRefit(long jarg1, long jarg2, int jarg3, int jarg4);
77 | public final static native int LGBM_BoosterUpdateOneIterCustom(long jarg1, long jarg2, long jarg3, long jarg4);
78 | public final static native int LGBM_BoosterRollbackOneIter(long jarg1);
79 | public final static native int LGBM_BoosterGetCurrentIteration(long jarg1, long jarg2);
80 | public final static native int LGBM_BoosterNumModelPerIteration(long jarg1, long jarg2);
81 | public final static native int LGBM_BoosterNumberOfTotalModel(long jarg1, long jarg2);
82 | public final static native int LGBM_BoosterGetEvalCounts(long jarg1, long jarg2);
83 | public final static native int LGBM_BoosterValidateFeatureNames(long jarg1, long jarg2, int jarg3);
84 | public final static native int LGBM_BoosterGetNumFeature(long jarg1, long jarg2);
85 | public final static native int LGBM_BoosterGetEval(long jarg1, int jarg2, long jarg3, long jarg4);
86 | public final static native int LGBM_BoosterGetNumPredict(long jarg1, int jarg2, long jarg3);
87 | public final static native int LGBM_BoosterGetPredict(long jarg1, int jarg2, long jarg3, long jarg4);
88 | public final static native int LGBM_BoosterPredictForFile(long jarg1, String jarg2, int jarg3, int jarg4, int jarg5, int jarg6, String jarg7, String jarg8);
89 | public final static native int LGBM_BoosterCalcNumPredict(long jarg1, int jarg2, int jarg3, int jarg4, int jarg5, long jarg6);
90 | public final static native int LGBM_FastConfigFree(long jarg1);
91 | public final static native int LGBM_BoosterPredictForCSR(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9, int jarg10, int jarg11, int jarg12, String jarg13, long jarg14, long jarg15);
92 | public final static native int LGBM_BoosterPredictSparseOutput(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9, int jarg10, int jarg11, int jarg12, String jarg13, int jarg14, long jarg15, long jarg16, long jarg17, long jarg18);
93 | public final static native int LGBM_BoosterFreePredictSparse(long jarg1, long jarg2, long jarg3, int jarg4, int jarg5);
94 | public final static native int LGBM_BoosterPredictForCSRSingleRow(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9, int jarg10, int jarg11, int jarg12, String jarg13, long jarg14, long jarg15);
95 | public final static native int LGBM_BoosterPredictForCSRSingleRowFastInit(long jarg1, int jarg2, int jarg3, int jarg4, int jarg5, long jarg6, String jarg7, long jarg8);
96 | public final static native int LGBM_BoosterPredictForCSRSingleRowFast(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, long jarg6, long jarg7, long jarg8, long jarg9);
97 | public final static native int LGBM_BoosterPredictForCSC(long jarg1, long jarg2, int jarg3, long jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9, int jarg10, int jarg11, int jarg12, String jarg13, long jarg14, long jarg15);
98 | public final static native int LGBM_BoosterPredictForMat(long jarg1, long jarg2, int jarg3, int jarg4, int jarg5, int jarg6, int jarg7, int jarg8, int jarg9, String jarg10, long jarg11, long jarg12);
99 | public final static native int LGBM_BoosterPredictForMatSingleRow(long jarg1, long jarg2, int jarg3, int jarg4, int jarg5, int jarg6, int jarg7, int jarg8, String jarg9, long jarg10, long jarg11);
100 | public final static native int LGBM_BoosterPredictForMatSingleRowFastInit(long jarg1, int jarg2, int jarg3, int jarg4, int jarg5, int jarg6, String jarg7, long jarg8);
101 | public final static native int LGBM_BoosterPredictForMatSingleRowFast(long jarg1, long jarg2, long jarg3, long jarg4);
102 | public final static native int LGBM_BoosterPredictForMats(long jarg1, long jarg2, int jarg3, int jarg4, int jarg5, int jarg6, int jarg7, int jarg8, String jarg9, long jarg10, long jarg11);
103 | public final static native int LGBM_BoosterPredictForArrow(long jarg1, long jarg2, long jarg3, long jarg4, int jarg5, int jarg6, int jarg7, String jarg8, long jarg9, long jarg10);
104 | public final static native int LGBM_BoosterSaveModel(long jarg1, int jarg2, int jarg3, int jarg4, String jarg5);
105 | public final static native int LGBM_BoosterDumpModel(long jarg1, int jarg2, int jarg3, int jarg4, long jarg5, long jarg6, String jarg7);
106 | public final static native int LGBM_BoosterGetLeafValue(long jarg1, int jarg2, int jarg3, long jarg4);
107 | public final static native int LGBM_BoosterSetLeafValue(long jarg1, int jarg2, int jarg3, double jarg4);
108 | public final static native int LGBM_BoosterFeatureImportance(long jarg1, int jarg2, int jarg3, long jarg4);
109 | public final static native int LGBM_BoosterGetUpperBoundValue(long jarg1, long jarg2);
110 | public final static native int LGBM_BoosterGetLowerBoundValue(long jarg1, long jarg2);
111 | public final static native int LGBM_NetworkInit(String jarg1, int jarg2, int jarg3, int jarg4);
112 | public final static native int LGBM_NetworkFree();
113 | public final static native int LGBM_NetworkInitWithFunctions(int jarg1, int jarg2, long jarg3, long jarg4);
114 | public final static native int LGBM_SetMaxThreads(int jarg1);
115 | public final static native int LGBM_GetMaxThreads(long jarg1);
116 | public final static native String LastErrorMsg();
117 | public final static native void LGBM_SetLastError(String jarg1);
118 | public final static native String LGBM_BoosterSaveModelToStringSWIG(long jarg1, int jarg2, int jarg3, int jarg4, long jarg5, long jarg6);
119 | public final static native String LGBM_BoosterDumpModelSWIG(long jarg1, int jarg2, int jarg3, int jarg4, long jarg5, long jarg6);
120 | public final static native int LGBM_BoosterPredictForMatSingle(double[] jarg2, long jarg3, int jarg4, int jarg5, int jarg6, int jarg7, int jarg8, int jarg9, String jarg10, long jarg11, long jarg12);
121 | public final static native int LGBM_BoosterPredictForMatSingleRowFastCriticalSWIG(double[] jarg2, long jarg3, long jarg4, long jarg5);
122 | public final static native int LGBM_BoosterPredictForCSRSingle(int[] jarg2, double[] jarg3, int jarg4, long jarg5, int jarg6, int jarg7, long jarg8, long jarg9, int jarg10, int jarg11, int jarg12, String jarg13, long jarg14, long jarg15);
123 | public final static native int LGBM_BoosterPredictForCSRSingleRowFastCriticalSWIG(int[] jarg2, double[] jarg3, int jarg4, long jarg5, int jarg6, long jarg7, long jarg8, long jarg9);
124 | public final static native void CSRDirect_indices_set(long jarg1, CSRDirect jarg1_, int[] jarg2);
125 | public final static native int[] CSRDirect_indices_get(long jarg1, CSRDirect jarg1_);
126 | public final static native void CSRDirect_values_set(long jarg1, CSRDirect jarg1_, double[] jarg2);
127 | public final static native double[] CSRDirect_values_get(long jarg1, CSRDirect jarg1_);
128 | public final static native void CSRDirect_indices0_set(long jarg1, CSRDirect jarg1_, long jarg2);
129 | public final static native long CSRDirect_indices0_get(long jarg1, CSRDirect jarg1_);
130 | public final static native void CSRDirect_values0_set(long jarg1, CSRDirect jarg1_, long jarg2);
131 | public final static native long CSRDirect_values0_get(long jarg1, CSRDirect jarg1_);
132 | public final static native void CSRDirect_size_set(long jarg1, CSRDirect jarg1_, int jarg2);
133 | public final static native int CSRDirect_size_get(long jarg1, CSRDirect jarg1_);
134 | public final static native long new_CSRDirect();
135 | public final static native void delete_CSRDirect(long jarg1);
136 | public final static native int LGBM_DatasetCreateFromCSRSpark(java.lang.Object[] jarg2, int jarg3, long jarg4, String jarg5, long jarg6, long jarg7);
137 | public final static native long new_bytep();
138 | public final static native long copy_bytep(short jarg1);
139 | public final static native void delete_bytep(long jarg1);
140 | public final static native void bytep_assign(long jarg1, short jarg2);
141 | public final static native short bytep_value(long jarg1);
142 | public final static native long new_intp();
143 | public final static native long copy_intp(int jarg1);
144 | public final static native void delete_intp(long jarg1);
145 | public final static native void intp_assign(long jarg1, int jarg2);
146 | public final static native int intp_value(long jarg1);
147 | public final static native long new_longp();
148 | public final static native long copy_longp(int jarg1);
149 | public final static native void delete_longp(long jarg1);
150 | public final static native void longp_assign(long jarg1, int jarg2);
151 | public final static native int longp_value(long jarg1);
152 | public final static native long new_doublep();
153 | public final static native long copy_doublep(double jarg1);
154 | public final static native void delete_doublep(long jarg1);
155 | public final static native void doublep_assign(long jarg1, double jarg2);
156 | public final static native double doublep_value(long jarg1);
157 | public final static native long new_floatp();
158 | public final static native long copy_floatp(float jarg1);
159 | public final static native void delete_floatp(long jarg1);
160 | public final static native void floatp_assign(long jarg1, float jarg2);
161 | public final static native float floatp_value(long jarg1);
162 | public final static native long new_int64_tp();
163 | public final static native long copy_int64_tp(long jarg1);
164 | public final static native void delete_int64_tp(long jarg1);
165 | public final static native void int64_tp_assign(long jarg1, long jarg2);
166 | public final static native long int64_tp_value(long jarg1);
167 | public final static native long new_int32_tp();
168 | public final static native long copy_int32_tp(int jarg1);
169 | public final static native void delete_int32_tp(long jarg1);
170 | public final static native void int32_tp_assign(long jarg1, int jarg2);
171 | public final static native int int32_tp_value(long jarg1);
172 | public final static native long new_size_tp();
173 | public final static native long copy_size_tp(long jarg1);
174 | public final static native void delete_size_tp(long jarg1);
175 | public final static native void size_tp_assign(long jarg1, long jarg2);
176 | public final static native long size_tp_value(long jarg1);
177 | public final static native long int64_t_to_long_ptr(long jarg1);
178 | public final static native long int64_t_to_double_ptr(long jarg1);
179 | public final static native long int32_t_to_int_ptr(long jarg1);
180 | public final static native long long_to_int64_t_ptr(long jarg1);
181 | public final static native long double_to_int64_t_ptr(long jarg1);
182 | public final static native long int_to_int32_t_ptr(long jarg1);
183 | public final static native long double_to_voidp_ptr(long jarg1);
184 | public final static native long float_to_voidp_ptr(long jarg1);
185 | public final static native long int_to_voidp_ptr(long jarg1);
186 | public final static native long byte_to_voidp_ptr(long jarg1);
187 | public final static native long int32_t_to_voidp_ptr(long jarg1);
188 | public final static native long int64_t_to_voidp_ptr(long jarg1);
189 | public final static native long void_to_doublep_ptr(long jarg1);
190 | public final static native long new_byteArray(long jarg1);
191 | public final static native void delete_byteArray(long jarg1);
192 | public final static native short byteArray_getitem(long jarg1, long jarg2);
193 | public final static native void byteArray_setitem(long jarg1, long jarg2, short jarg3);
194 | public final static native long new_doubleArray(long jarg1);
195 | public final static native void delete_doubleArray(long jarg1);
196 | public final static native double doubleArray_getitem(long jarg1, long jarg2);
197 | public final static native void doubleArray_setitem(long jarg1, long jarg2, double jarg3);
198 | public final static native long new_floatArray(long jarg1);
199 | public final static native void delete_floatArray(long jarg1);
200 | public final static native float floatArray_getitem(long jarg1, long jarg2);
201 | public final static native void floatArray_setitem(long jarg1, long jarg2, float jarg3);
202 | public final static native long new_intArray(long jarg1);
203 | public final static native void delete_intArray(long jarg1);
204 | public final static native int intArray_getitem(long jarg1, long jarg2);
205 | public final static native void intArray_setitem(long jarg1, long jarg2, int jarg3);
206 | public final static native long new_longArray(long jarg1);
207 | public final static native void delete_longArray(long jarg1);
208 | public final static native long longArray_getitem(long jarg1, long jarg2);
209 | public final static native void longArray_setitem(long jarg1, long jarg2, long jarg3);
210 | public final static native long new_voidPtrArray(long jarg1);
211 | public final static native void delete_voidPtrArray(long jarg1);
212 | public final static native long voidPtrArray_getitem(long jarg1, long jarg2);
213 | public final static native void voidPtrArray_setitem(long jarg1, long jarg2, long jarg3);
214 | public final static native long new_doublePtrArray(long jarg1);
215 | public final static native void delete_doublePtrArray(long jarg1);
216 | public final static native long doublePtrArray_getitem(long jarg1, long jarg2);
217 | public final static native void doublePtrArray_setitem(long jarg1, long jarg2, long jarg3);
218 | public final static native long new_intPtrArray(long jarg1);
219 | public final static native void delete_intPtrArray(long jarg1);
220 | public final static native long intPtrArray_getitem(long jarg1, long jarg2);
221 | public final static native void intPtrArray_setitem(long jarg1, long jarg2, long jarg3);
222 | public final static native long new_voidpp();
223 | public final static native void delete_voidpp(long jarg1);
224 | public final static native long voidpp_value(long jarg1);
225 | public final static native long voidpp_handle();
226 | public final static native long StringArrayHandle_create(long jarg1, long jarg2);
227 | public final static native void StringArrayHandle_free(long jarg1);
228 | public final static native String[] StringArrayHandle_get_strings(long jarg1);
229 | public final static native String StringArrayHandle_get_string(long jarg1, int jarg2);
230 | public final static native int StringArrayHandle_set_string(long jarg1, long jarg2, String jarg3);
231 | public final static native long StringArrayHandle_get_num_elements(long jarg1);
232 | public final static native long LGBM_BoosterGetEvalNamesSWIG(long jarg1);
233 | public final static native long LGBM_BoosterGetFeatureNamesSWIG(long jarg1);
234 | public final static native long LGBM_DatasetGetFeatureNamesSWIG(long jarg1);
235 | public final static native long new_int32ChunkedArray(long jarg1);
236 | public final static native void delete_int32ChunkedArray(long jarg1);
237 | public final static native void int32ChunkedArray_add(long jarg1, int32ChunkedArray jarg1_, int jarg2);
238 | public final static native long int32ChunkedArray_get_add_count(long jarg1, int32ChunkedArray jarg1_);
239 | public final static native long int32ChunkedArray_get_chunks_count(long jarg1, int32ChunkedArray jarg1_);
240 | public final static native long int32ChunkedArray_get_last_chunk_add_count(long jarg1, int32ChunkedArray jarg1_);
241 | public final static native long int32ChunkedArray_get_chunk_size(long jarg1, int32ChunkedArray jarg1_);
242 | public final static native long int32ChunkedArray_data(long jarg1, int32ChunkedArray jarg1_);
243 | public final static native long int32ChunkedArray_data_as_void(long jarg1, int32ChunkedArray jarg1_);
244 | public final static native void int32ChunkedArray_coalesce_to__SWIG_0(long jarg1, int32ChunkedArray jarg1_, long jarg2, boolean jarg3);
245 | public final static native void int32ChunkedArray_coalesce_to__SWIG_1(long jarg1, int32ChunkedArray jarg1_, long jarg2);
246 | public final static native int int32ChunkedArray_getitem(long jarg1, int32ChunkedArray jarg1_, long jarg2, long jarg3, int jarg4);
247 | public final static native int int32ChunkedArray_setitem(long jarg1, int32ChunkedArray jarg1_, long jarg2, long jarg3, int jarg4);
248 | public final static native void int32ChunkedArray_clear(long jarg1, int32ChunkedArray jarg1_);
249 | public final static native void int32ChunkedArray_release(long jarg1, int32ChunkedArray jarg1_);
250 | public final static native boolean int32ChunkedArray_within_bounds(long jarg1, int32ChunkedArray jarg1_, long jarg2, long jarg3);
251 | public final static native void int32ChunkedArray_new_chunk(long jarg1, int32ChunkedArray jarg1_);
252 | public final static native long new_floatChunkedArray(long jarg1);
253 | public final static native void delete_floatChunkedArray(long jarg1);
254 | public final static native void floatChunkedArray_add(long jarg1, floatChunkedArray jarg1_, float jarg2);
255 | public final static native long floatChunkedArray_get_add_count(long jarg1, floatChunkedArray jarg1_);
256 | public final static native long floatChunkedArray_get_chunks_count(long jarg1, floatChunkedArray jarg1_);
257 | public final static native long floatChunkedArray_get_last_chunk_add_count(long jarg1, floatChunkedArray jarg1_);
258 | public final static native long floatChunkedArray_get_chunk_size(long jarg1, floatChunkedArray jarg1_);
259 | public final static native long floatChunkedArray_data(long jarg1, floatChunkedArray jarg1_);
260 | public final static native long floatChunkedArray_data_as_void(long jarg1, floatChunkedArray jarg1_);
261 | public final static native void floatChunkedArray_coalesce_to__SWIG_0(long jarg1, floatChunkedArray jarg1_, long jarg2, boolean jarg3);
262 | public final static native void floatChunkedArray_coalesce_to__SWIG_1(long jarg1, floatChunkedArray jarg1_, long jarg2);
263 | public final static native float floatChunkedArray_getitem(long jarg1, floatChunkedArray jarg1_, long jarg2, long jarg3, float jarg4);
264 | public final static native int floatChunkedArray_setitem(long jarg1, floatChunkedArray jarg1_, long jarg2, long jarg3, float jarg4);
265 | public final static native void floatChunkedArray_clear(long jarg1, floatChunkedArray jarg1_);
266 | public final static native void floatChunkedArray_release(long jarg1, floatChunkedArray jarg1_);
267 | public final static native boolean floatChunkedArray_within_bounds(long jarg1, floatChunkedArray jarg1_, long jarg2, long jarg3);
268 | public final static native void floatChunkedArray_new_chunk(long jarg1, floatChunkedArray jarg1_);
269 | public final static native long new_doubleChunkedArray(long jarg1);
270 | public final static native void delete_doubleChunkedArray(long jarg1);
271 | public final static native void doubleChunkedArray_add(long jarg1, doubleChunkedArray jarg1_, double jarg2);
272 | public final static native long doubleChunkedArray_get_add_count(long jarg1, doubleChunkedArray jarg1_);
273 | public final static native long doubleChunkedArray_get_chunks_count(long jarg1, doubleChunkedArray jarg1_);
274 | public final static native long doubleChunkedArray_get_last_chunk_add_count(long jarg1, doubleChunkedArray jarg1_);
275 | public final static native long doubleChunkedArray_get_chunk_size(long jarg1, doubleChunkedArray jarg1_);
276 | public final static native long doubleChunkedArray_data(long jarg1, doubleChunkedArray jarg1_);
277 | public final static native long doubleChunkedArray_data_as_void(long jarg1, doubleChunkedArray jarg1_);
278 | public final static native void doubleChunkedArray_coalesce_to__SWIG_0(long jarg1, doubleChunkedArray jarg1_, long jarg2, boolean jarg3);
279 | public final static native void doubleChunkedArray_coalesce_to__SWIG_1(long jarg1, doubleChunkedArray jarg1_, long jarg2);
280 | public final static native double doubleChunkedArray_getitem(long jarg1, doubleChunkedArray jarg1_, long jarg2, long jarg3, double jarg4);
281 | public final static native int doubleChunkedArray_setitem(long jarg1, doubleChunkedArray jarg1_, long jarg2, long jarg3, double jarg4);
282 | public final static native void doubleChunkedArray_clear(long jarg1, doubleChunkedArray jarg1_);
283 | public final static native void doubleChunkedArray_release(long jarg1, doubleChunkedArray jarg1_);
284 | public final static native boolean doubleChunkedArray_within_bounds(long jarg1, doubleChunkedArray jarg1_, long jarg2, long jarg3);
285 | public final static native void doubleChunkedArray_new_chunk(long jarg1, doubleChunkedArray jarg1_);
286 | }
287 |
--------------------------------------------------------------------------------
/src/main/java/io/github/metarank/lightgbm4j/LGBMBooster.java:
--------------------------------------------------------------------------------
1 | package io.github.metarank.lightgbm4j;
2 |
3 | import com.microsoft.ml.lightgbm.*;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import java.io.*;
8 | import java.math.BigInteger;
9 | import java.security.MessageDigest;
10 | import java.security.NoSuchAlgorithmException;
11 | import java.util.Locale;
12 |
13 | import static com.microsoft.ml.lightgbm.lightgbmlib.*;
14 |
15 | public class LGBMBooster implements AutoCloseable {
16 | private int iterations;
17 | private SWIGTYPE_p_p_void handle;
18 |
19 | private static final long MODEL_SAVE_BUFFER_SIZE = 10 * 1024 * 1024L;
20 | private static final long EVAL_RESULTS_BUFFER_SIZE = 1024;
21 |
22 | private static final Logger logger = LoggerFactory.getLogger(LGBMBooster.class);
23 | private static volatile boolean nativeLoaded = false;
24 |
25 | private volatile boolean isClosed = false;
26 |
27 | static {
28 | try {
29 | LGBMBooster.loadNative();
30 | } catch (IOException e) {
31 | logger.info("Cannot load native library for your platform");
32 | }
33 | }
34 |
35 | /**
36 | * Called from tests.
37 | *
38 | * @return true if JNI libraries were loaded successfully.
39 | */
40 | public static boolean isNativeLoaded() {
41 | return nativeLoaded;
42 | }
43 |
44 | /**
45 | * Loads all corresponsing native libraries for current platform. Called from the class initializer,
46 | * so usually there is no need to call it directly.
47 | *
48 | * @throws IOException
49 | */
50 | public synchronized static void loadNative() throws IOException {
51 | if (!nativeLoaded) {
52 | String os = System.getProperty("os.name");
53 | String arch = System.getProperty("os.arch", "generic").toLowerCase(Locale.ENGLISH);
54 | if (os.startsWith("Linux") || os.startsWith("LINUX")) {
55 | try {
56 | if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
57 | loadNative("lightgbm4j/linux/x86_64/", "lib_lightgbm.so");
58 | loadNative("lightgbm4j/linux/x86_64/", "lib_lightgbm_swig.so");
59 | nativeLoaded = true;
60 | } else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
61 | loadNative("lightgbm4j/linux/aarch64/", "lib_lightgbm.so");
62 | loadNative("lightgbm4j/linux/aarch64/", "lib_lightgbm_swig.so");
63 | nativeLoaded = true;
64 | }
65 | } catch (UnsatisfiedLinkError err) {
66 | String message = err.getMessage();
67 | if (message.contains("libgomp")) {
68 | logger.warn("\n\n\n");
69 | logger.warn("****************************************************");
70 | logger.warn("Your Linux system probably has no 'libgomp' library installed!");
71 | logger.warn("Please double-check the lightgbm4j install instructions:");
72 | logger.warn("- https://github.com/metarank/lightgbm4j/");
73 | logger.warn("- or just install the libgomp with your package manager");
74 | logger.warn("****************************************************");
75 | logger.warn("\n\n\n");
76 | }
77 | }
78 | } else if (os.startsWith("Mac")) {
79 | try {
80 | if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
81 | loadNative("lightgbm4j/osx/x86_64/", "lib_lightgbm.dylib");
82 | loadNative("lightgbm4j/osx/x86_64/", "lib_lightgbm_swig.dylib");
83 | nativeLoaded = true;
84 | } else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
85 | loadNative("lightgbm4j/osx/aarch64/", "lib_lightgbm.dylib");
86 | loadNative("lightgbm4j/osx/aarch64/", "lib_lightgbm_swig.dylib");
87 | nativeLoaded = true;
88 | } else {
89 | logger.warn("arch " + arch + " is not supported");
90 | throw new UnsatisfiedLinkError("no native lightgbm library found for your OS "+os);
91 | }
92 | } catch (UnsatisfiedLinkError err) {
93 | String message = err.getMessage();
94 | if (message.contains("libomp.dylib")) {
95 | logger.warn("\n\n\n");
96 | logger.warn("****************************************************");
97 | logger.warn("Your MacOS system probably has no 'libomp' library installed!");
98 | logger.warn("Please double-check the lightgbm4j install instructions:");
99 | logger.warn("- https://github.com/metarank/lightgbm4j/");
100 | logger.warn("- or just do 'brew install libomp'");
101 | logger.warn("****************************************************");
102 | logger.warn("\n\n\n");
103 |
104 | }
105 | throw err;
106 | }
107 | } else if (os.startsWith("Windows")) {
108 | loadNative("lightgbm4j/windows/x86_64/", "lib_lightgbm.dll");
109 | loadNative("lightgbm4j/windows/x86_64/", "lib_lightgbm_swig.dll");
110 | nativeLoaded = true;
111 | } else {
112 | logger.error("Only Linux@x86_64, Windows@x86_64, Mac@x86_64 and Mac@aarch are supported");
113 | }
114 | }
115 | }
116 |
117 | private static void loadNative(String path, String name) throws IOException, UnsatisfiedLinkError {
118 | String nativePathOverride = System.getenv("LIGHTGBM_NATIVE_LIB_PATH");
119 | if (nativePathOverride != null) {
120 | if (!nativePathOverride.endsWith("/")) {
121 | nativePathOverride = nativePathOverride + "/";
122 | }
123 | String libFile = nativePathOverride + name;
124 | logger.info("LIGHTGBM_NATIVE_LIB_PATH is set: loading " + libFile);
125 | try {
126 | System.load(libFile);
127 | } catch (UnsatisfiedLinkError err) {
128 | logger.error("Cannot load library:" + err.getMessage(), err);
129 | throw err;
130 | }
131 | } else {
132 | logger.info("Loading native lib from resource " + path + "/" + name);
133 | String tmp = System.getProperty("java.io.tmpdir");
134 | File libFile = new File(tmp + File.separator + name);
135 | if (libFile.exists()) {
136 | logger.info(libFile + " already exists");
137 | extractResource(path + name, name, libFile);
138 | } else {
139 | extractResource(path + name, name, libFile);
140 | }
141 | logger.info("Extracted file: exists=" + libFile.exists() + " path=" + libFile);
142 | try {
143 | System.load(libFile.toString());
144 | } catch (UnsatisfiedLinkError err) {
145 | logger.error("Cannot load library:" + err.getMessage(), err);
146 | throw err;
147 | }
148 | }
149 | }
150 |
151 | private static void extractResource(String path, String name, File dest) throws IOException {
152 | logger.info("Extracting native lib " + dest);
153 | InputStream libStream = LGBMBooster.class.getClassLoader().getResourceAsStream(path);
154 | ByteArrayOutputStream libByteStream = new ByteArrayOutputStream();
155 | copyStream(libStream, libByteStream);
156 | libStream.close();
157 |
158 | InputStream md5Stream = LGBMBooster.class.getClassLoader().getResourceAsStream(path + ".md5");
159 | ByteArrayOutputStream md5ByteStream = new ByteArrayOutputStream();
160 | copyStream(md5Stream, md5ByteStream);
161 | md5Stream.close();
162 | String expectedDigest = md5ByteStream.toString();
163 | try {
164 | byte[] digest = MessageDigest.getInstance("MD5").digest(libByteStream.toByteArray());
165 | String checksum = new BigInteger(1, digest).toString(16);
166 | for (int i=0; i<32 - checksum.length(); i++) {
167 | checksum = "0" + checksum;
168 | }
169 |
170 |
171 | if (!checksum.equals(expectedDigest)) {
172 | logger.warn("\n\n\n");
173 | logger.warn("****************************************************");
174 | logger.warn("Hash mismatch between expected and real LightGBM native library in classpath!");
175 | logger.warn("Your JVM classpath has "+name+" with md5="+checksum+" and we expect "+expectedDigest);
176 | logger.warn("This usually means that you have another LightGBM wrapper in classpath");
177 | logger.warn("- MMLSpark/SynapseML is the main suspect");
178 | logger.warn("****************************************************");
179 | logger.warn("\n\n\n");
180 | //throw new IOException("hash mismatch");
181 | }
182 | ByteArrayInputStream source = new ByteArrayInputStream(libByteStream.toByteArray());
183 | OutputStream fileStream = new FileOutputStream(dest);
184 | copyStream(source, fileStream);
185 | source.close();
186 | fileStream.close();
187 | } catch (NoSuchAlgorithmException ex) {
188 | throw new IOException("md5 algorithm not supported, cannot check digest");
189 | }
190 | }
191 |
192 | private static void copyStream(InputStream source, OutputStream target) throws IOException {
193 | byte[] buf = new byte[8192];
194 | int length;
195 | int bytesCopied = 0;
196 | while ((length = source.read(buf)) > 0) {
197 | target.write(buf, 0, length);
198 | bytesCopied += length;
199 | }
200 | logger.info("Copied " + bytesCopied + " bytes");
201 | }
202 |
203 | /**
204 | * Constructor is private because you need to have a JNI handle for native LightGBM instance.
205 | *
206 | * @param iterations
207 | * @param handle
208 | */
209 | LGBMBooster(int iterations, SWIGTYPE_p_p_void handle) {
210 | this.iterations = iterations;
211 | this.handle = handle;
212 | }
213 |
214 | /**
215 | * Load an existing booster from model file.
216 | *
217 | * @param file Filename of model
218 | * @return Booster instance.
219 | * @throws LGBMException
220 | */
221 | public static LGBMBooster createFromModelfile(String file) throws LGBMException {
222 | SWIGTYPE_p_p_void handle = new_voidpp();
223 | SWIGTYPE_p_int outIterations = new_intp();
224 | int result = LGBM_BoosterCreateFromModelfile(file, outIterations, handle);
225 | if (result < 0) {
226 | throw new LGBMException(LGBM_GetLastError());
227 | } else {
228 | int iterations = intp_value(outIterations);
229 | delete_intp(outIterations);
230 | return new LGBMBooster(iterations, handle);
231 | }
232 | }
233 |
234 | /**
235 | * Load an existing booster from string.
236 | *
237 | * @param model Model string
238 | * @return Booster instance.
239 | * @throws LGBMException
240 | */
241 | public static LGBMBooster loadModelFromString(String model) throws LGBMException {
242 | SWIGTYPE_p_p_void handle = new_voidpp();
243 | SWIGTYPE_p_int outIterations = new_intp();
244 | int result = LGBM_BoosterLoadModelFromString(model, outIterations, handle);
245 | if (result < 0) {
246 | throw new LGBMException(LGBM_GetLastError());
247 | } else {
248 | int iterations = intp_value(outIterations);
249 | delete_intp(outIterations);
250 | return new LGBMBooster(iterations, handle);
251 | }
252 | }
253 |
254 | /**
255 | * Deallocate all native memory for the LightGBM model.
256 | *
257 | * @throws LGBMException
258 | */
259 | @Override
260 | public void close() throws LGBMException {
261 | if (!isClosed) {
262 | isClosed = true;
263 | int result = LGBM_BoosterFree(voidpp_value(handle));
264 | if (result < 0) {
265 | throw new LGBMException(LGBM_GetLastError());
266 | }
267 | } else {
268 | throw new LGBMException("Booster was already closed");
269 | }
270 | }
271 |
272 | /**
273 | * Make prediction for a new float[] dataset.
274 | *
275 | * @param input input matrix, as a 1D array. Size should be rows * cols.
276 | * @param rows number of rows
277 | * @param cols number of cols
278 | * @param isRowMajor is the 1d encoding a row-major?
279 | * @param predictionType the prediction type
280 | * @param parameter prediction options
281 | * @return array of predictions
282 | * @throws LGBMException
283 | */
284 | public double[] predictForMat(float[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType, String parameter) throws LGBMException {
285 | if (!isClosed) {
286 | SWIGTYPE_p_float dataBuffer = new_floatArray(input.length);
287 | for (int i = 0; i < input.length; i++) {
288 | floatArray_setitem(dataBuffer, i, input[i]);
289 | }
290 | SWIGTYPE_p_long_long outLength = new_int64_tp();
291 | long outSize = outBufferSize(rows, cols, predictionType);
292 | SWIGTYPE_p_double outBuffer = new_doubleArray(outSize);
293 | int result = LGBM_BoosterPredictForMat(
294 | voidpp_value(handle),
295 | float_to_voidp_ptr(dataBuffer),
296 | C_API_DTYPE_FLOAT32,
297 | rows,
298 | cols,
299 | isRowMajor ? 1 : 0,
300 | predictionType.getType(),
301 | 0,
302 | iterations,
303 | parameter,
304 | outLength,
305 | outBuffer);
306 | if (result < 0) {
307 | delete_floatArray(dataBuffer);
308 | delete_int64_tp(outLength);
309 | delete_doubleArray(outBuffer);
310 | throw new LGBMException(LGBM_GetLastError());
311 | } else {
312 | long length = int64_tp_value(outLength);
313 | double[] values = new double[(int) length];
314 | for (int i = 0; i < length; i++) {
315 | values[i] = doubleArray_getitem(outBuffer, i);
316 | }
317 | delete_floatArray(dataBuffer);
318 | delete_int64_tp(outLength);
319 | delete_doubleArray(outBuffer);
320 | return values;
321 | }
322 | } else {
323 | throw new LGBMException("Booster was already closed");
324 | }
325 | }
326 |
327 | public double[] predictForMat(float[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType) throws LGBMException {
328 | return predictForMat(input, rows, cols, isRowMajor, predictionType, "");
329 | }
330 |
331 | /**
332 | * Make prediction for a new double[] dataset.
333 | *
334 | * @param input input matrix, as a 1D array. Size should be rows * cols.
335 | * @param rows number of rows
336 | * @param cols number of cols
337 | * @param isRowMajor is the 1 d encoding a row-major?
338 | * @param predictionType the prediction type
339 | * @param parameter prediction options
340 | * @return array of predictions
341 | * @throws LGBMException
342 | */
343 |
344 | public double[] predictForMat(double[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType, String parameter) throws LGBMException {
345 | if (!isClosed) {
346 | SWIGTYPE_p_double dataBuffer = new_doubleArray(input.length);
347 | for (int i = 0; i < input.length; i++) {
348 | doubleArray_setitem(dataBuffer, i, input[i]);
349 | }
350 | SWIGTYPE_p_long_long outLength = new_int64_tp();
351 | long outSize = outBufferSize(rows, cols, predictionType);
352 | SWIGTYPE_p_double outBuffer = new_doubleArray(outSize);
353 | int result = LGBM_BoosterPredictForMat(
354 | voidpp_value(handle),
355 | double_to_voidp_ptr(dataBuffer),
356 | C_API_DTYPE_FLOAT64,
357 | rows,
358 | cols,
359 | isRowMajor ? 1 : 0,
360 | predictionType.getType(),
361 | 0,
362 | iterations,
363 | parameter,
364 | outLength,
365 | outBuffer);
366 | if (result < 0) {
367 | delete_doubleArray(dataBuffer);
368 | delete_int64_tp(outLength);
369 | delete_doubleArray(outBuffer);
370 | throw new LGBMException(LGBM_GetLastError());
371 | } else {
372 | long length = int64_tp_value(outLength);
373 | double[] values = new double[(int) length];
374 | for (int i = 0; i < length; i++) {
375 | values[i] = doubleArray_getitem(outBuffer, i);
376 | }
377 | delete_doubleArray(dataBuffer);
378 | delete_int64_tp(outLength);
379 | delete_doubleArray(outBuffer);
380 | return values;
381 | }
382 | } else {
383 | throw new LGBMException("Booster was already closed");
384 | }
385 | }
386 | public double[] predictForMat(double[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType) throws LGBMException {
387 | return predictForMat(input, rows, cols, isRowMajor, predictionType, "");
388 | }
389 |
390 | /**
391 | * Create a new boosting learner.
392 | *
393 | * @param dataset a LGBMDataset with the training data.
394 | * @param parameters Parameters in format ‘key1=value1 key2=value2’
395 | * @return
396 | * @throws LGBMException
397 | */
398 | public static LGBMBooster create(LGBMDataset dataset, String parameters) throws LGBMException {
399 | SWIGTYPE_p_p_void handle = new_voidpp();
400 | int result = LGBM_BoosterCreate(dataset.handle, parameters, handle);
401 | if (result < 0) {
402 | throw new LGBMException(LGBM_GetLastError());
403 | } else {
404 | return new LGBMBooster(0, handle);
405 | }
406 | }
407 |
408 | /**
409 | * Update the model for one iteration.
410 | *
411 | * @return true if there are no more splits possible, so training is finished.
412 | * @throws LGBMException
413 | */
414 | public boolean updateOneIter() throws LGBMException {
415 | if (!isClosed) {
416 | SWIGTYPE_p_int isFinishedP = new_intp();
417 | int result = LGBM_BoosterUpdateOneIter(voidpp_value(handle), isFinishedP);
418 | iterations++;
419 | if (result < 0) {
420 | throw new LGBMException(LGBM_GetLastError());
421 | } else {
422 | int isFinished = intp_value(isFinishedP);
423 | delete_intp(isFinishedP);
424 | return isFinished == 1;
425 | }
426 | } else {
427 | throw new LGBMException("Booster was already closed");
428 | }
429 | }
430 |
431 | public enum FeatureImportanceType {
432 | SPLIT,
433 | GAIN
434 | }
435 |
436 |
437 | /**
438 | * Save model to string.
439 | *
440 | * @param startIteration Start index of the iteration that should be saved
441 | * @param numIteration Index of the iteration that should be saved, 0 and negative means save all
442 | * @param featureImportance Type of feature importance, can be FeatureImportanceType.SPLIT or FeatureImportanceType.GAIN
443 | * @return
444 | */
445 | public String saveModelToString(int startIteration, int numIteration, FeatureImportanceType featureImportance) throws LGBMException {
446 | if (!isClosed) {
447 | SWIGTYPE_p_long_long outLength = new_int64_tp();
448 | String result = LGBM_BoosterSaveModelToStringSWIG(
449 | voidpp_value(handle),
450 | startIteration,
451 | numIteration,
452 | importanceType(featureImportance),
453 | MODEL_SAVE_BUFFER_SIZE,
454 | outLength
455 | );
456 | delete_int64_tp(outLength);
457 | return result;
458 | } else {
459 | throw new LGBMException("Booster was already closed");
460 | }
461 | }
462 |
463 | /**
464 | * Get names of features.
465 | *
466 | * @return a list of feature names.
467 | */
468 | public String[] getFeatureNames() throws LGBMException {
469 | if (!isClosed) {
470 | SWIGTYPE_p_void buffer = LGBM_BoosterGetFeatureNamesSWIG(voidpp_value(handle));
471 | String[] result = StringArrayHandle_get_strings(buffer);
472 | StringArrayHandle_free(buffer);
473 | return result;
474 | } else {
475 | throw new LGBMException("Booster was already closed");
476 | }
477 | }
478 |
479 | /**
480 | * Add new validation data to booster.
481 | *
482 | * @param dataset dataset to validate
483 | * @throws LGBMException
484 | */
485 | public void addValidData(LGBMDataset dataset) throws LGBMException {
486 | if (!isClosed) {
487 | int result = LGBM_BoosterAddValidData(voidpp_value(handle), dataset.handle);
488 | if (result < 0) {
489 | throw new LGBMException(LGBM_GetLastError());
490 | }
491 | } else {
492 | throw new LGBMException("Booster was already closed");
493 | }
494 | }
495 |
496 | /**
497 | * Get evaluation for training data and validation data.
498 | *
499 | * @param dataIndex Index of data, 0: training data, 1: 1st validation data, 2: 2nd validation data and so on
500 | * @return
501 | * @throws LGBMException
502 | */
503 | public double[] getEval(int dataIndex) throws LGBMException {
504 | if (!isClosed) {
505 | SWIGTYPE_p_int outLength = new_int32_tp();
506 | SWIGTYPE_p_double outBuffer = new_doubleArray(EVAL_RESULTS_BUFFER_SIZE);
507 | int result = LGBM_BoosterGetEval(voidpp_value(handle), dataIndex, outLength, outBuffer);
508 | if (result < 0) {
509 | delete_intp(outLength);
510 | delete_doubleArray(outBuffer);
511 | throw new LGBMException(LGBM_GetLastError());
512 | } else {
513 | double[] evals = new double[intp_value(outLength)];
514 | for (int i = 0; i < evals.length; i++) {
515 | evals[i] = doubleArray_getitem(outBuffer, i);
516 | }
517 | delete_intp(outLength);
518 | delete_doubleArray(outBuffer);
519 | return evals;
520 | }
521 | } else {
522 | throw new LGBMException("Booster was already closed");
523 | }
524 | }
525 |
526 | /**
527 | * Get names of evaluation datasets.
528 | *
529 | * @return array of eval dataset names.
530 | * @throws LGBMException
531 | */
532 | public String[] getEvalNames() throws LGBMException {
533 | if (!isClosed) {
534 | SWIGTYPE_p_void namesP = LGBM_BoosterGetEvalNamesSWIG(voidpp_value(handle));
535 | String[] names = StringArrayHandle_get_strings(namesP);
536 | StringArrayHandle_free(namesP);
537 | return names;
538 | } else {
539 | throw new LGBMException("Booster was already closed");
540 | }
541 | }
542 |
543 | /**
544 | * Get model feature importance.
545 | *
546 | * @param numIteration Number of iterations for which feature importance is calculated, 0 or less means use all
547 | * @param importanceType GAIN or SPLIT
548 | * @return Result array with feature importance
549 | * @throws LGBMException
550 | */
551 | public double[] featureImportance(int numIteration, FeatureImportanceType importanceType) throws LGBMException {
552 | if (!isClosed) {
553 | int numFeatures = getNumFeature();
554 | SWIGTYPE_p_double outBuffer = new_doubleArray(numFeatures);
555 | int result = LGBM_BoosterFeatureImportance(
556 | voidpp_value(handle),
557 | numIteration,
558 | importanceType(importanceType),
559 | outBuffer
560 | );
561 | if (result < 0) {
562 | delete_doubleArray(outBuffer);
563 | throw new LGBMException(LGBM_GetLastError());
564 | } else {
565 | double[] importance = new double[numFeatures];
566 | for (int i = 0; i < numFeatures; i++) {
567 | importance[i] = doubleArray_getitem(outBuffer, i);
568 | }
569 | delete_doubleArray(outBuffer);
570 | return importance;
571 | }
572 | } else {
573 | throw new LGBMException("Booster was already closed");
574 | }
575 | }
576 |
577 | /**
578 | * Get number of features.
579 | *
580 | * @return number of features
581 | * @throws LGBMException
582 | */
583 | public int getNumFeature() throws LGBMException {
584 | if (!isClosed) {
585 | SWIGTYPE_p_int outNum = new_int32_tp();
586 | int result = LGBM_BoosterGetNumFeature(voidpp_value(handle), outNum);
587 | if (result < 0) {
588 | delete_intp(outNum);
589 | throw new LGBMException(LGBM_GetLastError());
590 | } else {
591 | int num = intp_value(outNum);
592 | delete_intp(outNum);
593 | return num;
594 | }
595 | } else {
596 | throw new LGBMException("Booster was already closed");
597 | }
598 | }
599 |
600 | /**
601 | * Make prediction for a new double[] row dataset. This method re-uses the internal predictor structure from previous calls
602 | * and is optimized for single row invocation.
603 | *
604 | * @param data input vector
605 | * @param predictionType the prediction type
606 | * @return score
607 | * @throws LGBMException
608 | */
609 | public double predictForMatSingleRow(double[] data, PredictionType predictionType) throws LGBMException {
610 | if (!isClosed) {
611 | SWIGTYPE_p_double dataBuffer = new_doubleArray(data.length);
612 | for (int i = 0; i < data.length; i++) {
613 | doubleArray_setitem(dataBuffer, i, data[i]);
614 | }
615 | SWIGTYPE_p_long_long outLength = new_int64_tp();
616 | long outBufferSize = outBufferSize(1, data.length, predictionType);
617 | SWIGTYPE_p_double outBuffer = new_doubleArray(outBufferSize);
618 |
619 | int result = LGBM_BoosterPredictForMatSingleRow(
620 | voidpp_value(handle),
621 | double_to_voidp_ptr(dataBuffer),
622 | C_API_DTYPE_FLOAT64,
623 | data.length,
624 | 1,
625 | predictionType.getType(),
626 | 0,
627 | iterations,
628 | "",
629 | outLength,
630 | outBuffer
631 | );
632 | if (result < 0) {
633 | delete_doubleArray(dataBuffer);
634 | delete_doubleArray(outBuffer);
635 | delete_int64_tp(outLength);
636 | throw new LGBMException(LGBM_GetLastError());
637 | } else {
638 | long length = int64_tp_value(outLength);
639 | double[] values = new double[(int) length];
640 | for (int i = 0; i < length; i++) {
641 | values[i] = doubleArray_getitem(outBuffer, i);
642 | }
643 | delete_doubleArray(dataBuffer);
644 | delete_int64_tp(outLength);
645 | delete_doubleArray(outBuffer);
646 | return values[0];
647 | }
648 | } else {
649 | throw new LGBMException("Booster was already closed");
650 | }
651 | }
652 |
653 | /**
654 | * Make prediction for a new float[] row dataset. This method re-uses the internal predictor structure from previous calls
655 | * and is optimized for single row invocation.
656 | *
657 | * @param data input vector
658 | * @param predictionType the prediction type
659 | * @return score
660 | * @throws LGBMException
661 | */
662 | public double predictForMatSingleRow(float[] data, PredictionType predictionType) throws LGBMException {
663 | if (!isClosed) {
664 | SWIGTYPE_p_float dataBuffer = new_floatArray(data.length);
665 | for (int i = 0; i < data.length; i++) {
666 | floatArray_setitem(dataBuffer, i, data[i]);
667 | }
668 | SWIGTYPE_p_long_long outLength = new_int64_tp();
669 | long outBufferSize = outBufferSize(1, data.length, predictionType);
670 | SWIGTYPE_p_double outBuffer = new_doubleArray(outBufferSize);
671 |
672 | int result = LGBM_BoosterPredictForMatSingleRow(
673 | voidpp_value(handle),
674 | float_to_voidp_ptr(dataBuffer),
675 | C_API_DTYPE_FLOAT32,
676 | data.length,
677 | 1,
678 | predictionType.getType(),
679 | 0,
680 | iterations,
681 | "",
682 | outLength,
683 | outBuffer
684 | );
685 | if (result < 0) {
686 | delete_floatArray(dataBuffer);
687 | delete_doubleArray(outBuffer);
688 | delete_int64_tp(outLength);
689 | throw new LGBMException(LGBM_GetLastError());
690 | } else {
691 | long length = int64_tp_value(outLength);
692 | double[] values = new double[(int) length];
693 | for (int i = 0; i < length; i++) {
694 | values[i] = doubleArray_getitem(outBuffer, i);
695 | }
696 | delete_floatArray(dataBuffer);
697 | delete_int64_tp(outLength);
698 | delete_doubleArray(outBuffer);
699 | return values[0];
700 | }
701 | } else {
702 | throw new LGBMException("Booster was already closed");
703 | }
704 | }
705 |
706 | public FastConfig predictForMatSingleRowFastInit(PredictionType predictionType, int dtype, int ncols, String parameter) throws LGBMException {
707 | if (!isClosed) {
708 | SWIGTYPE_p_p_void out = voidpp_handle();
709 |
710 | int result = LGBM_BoosterPredictForMatSingleRowFastInit(
711 | voidpp_value(handle),
712 | predictionType.getType(),
713 | 0,
714 | iterations,
715 | dtype,
716 | ncols,
717 | parameter,
718 | out
719 | );
720 | if (result < 0) {
721 | delete_voidpp(out);
722 | throw new LGBMException(LGBM_GetLastError());
723 | } else {
724 | return new FastConfig(out);
725 | }
726 | } else {
727 | throw new LGBMException("Booster was already closed");
728 | }
729 | }
730 |
731 | public static class FastConfig implements AutoCloseable {
732 | public SWIGTYPE_p_p_void handle;
733 | public FastConfig(SWIGTYPE_p_p_void handle) {
734 | this.handle = handle;
735 | }
736 |
737 | @Override
738 | public void close() throws Exception {
739 | delete_voidpp(handle);
740 | }
741 | }
742 | public double predictForMatSingleRowFast(FastConfig config, float[] data, PredictionType predictionType) throws LGBMException {
743 | if (!isClosed) {
744 | SWIGTYPE_p_float dataBuffer = new_floatArray(data.length);
745 | for (int i = 0; i < data.length; i++) {
746 | floatArray_setitem(dataBuffer, i, data[i]);
747 | }
748 | SWIGTYPE_p_long_long outLength = new_int64_tp();
749 | long outBufferSize = outBufferSize(1, data.length, predictionType);
750 | SWIGTYPE_p_double outBuffer = new_doubleArray(outBufferSize);
751 |
752 | int result = LGBM_BoosterPredictForMatSingleRowFast(
753 | voidpp_value(config.handle),
754 | float_to_voidp_ptr(dataBuffer),
755 | outLength,
756 | outBuffer
757 | );
758 | if (result < 0) {
759 | delete_floatArray(dataBuffer);
760 | delete_doubleArray(outBuffer);
761 | delete_int64_tp(outLength);
762 | throw new LGBMException(LGBM_GetLastError());
763 | } else {
764 | long length = int64_tp_value(outLength);
765 | double[] values = new double[(int) length];
766 | for (int i = 0; i < length; i++) {
767 | values[i] = doubleArray_getitem(outBuffer, i);
768 | }
769 | delete_floatArray(dataBuffer);
770 | delete_int64_tp(outLength);
771 | delete_doubleArray(outBuffer);
772 | return values[0];
773 | }
774 | } else {
775 | throw new LGBMException("Booster was already closed");
776 | }
777 | }
778 |
779 | public double predictForMatSingleRowFast(FastConfig config, double[] data, PredictionType predictionType) throws LGBMException {
780 | if (!isClosed) {
781 | SWIGTYPE_p_double dataBuffer = new_doubleArray(data.length);
782 | for (int i = 0; i < data.length; i++) {
783 | doubleArray_setitem(dataBuffer, i, data[i]);
784 | }
785 | SWIGTYPE_p_long_long outLength = new_int64_tp();
786 | long outBufferSize = outBufferSize(1, data.length, predictionType);
787 | SWIGTYPE_p_double outBuffer = new_doubleArray(outBufferSize);
788 |
789 | int result = LGBM_BoosterPredictForMatSingleRowFast(
790 | voidpp_value(config.handle),
791 | double_to_voidp_ptr(dataBuffer),
792 | outLength,
793 | outBuffer
794 | );
795 | if (result < 0) {
796 | delete_doubleArray(dataBuffer);
797 | delete_doubleArray(outBuffer);
798 | delete_int64_tp(outLength);
799 | throw new LGBMException(LGBM_GetLastError());
800 | } else {
801 | long length = int64_tp_value(outLength);
802 | double[] values = new double[(int) length];
803 | for (int i = 0; i < length; i++) {
804 | values[i] = doubleArray_getitem(outBuffer, i);
805 | }
806 | delete_doubleArray(dataBuffer);
807 | delete_int64_tp(outLength);
808 | delete_doubleArray(outBuffer);
809 | return values[0];
810 | }
811 | } else {
812 | throw new LGBMException("Booster was already closed");
813 | }
814 | }
815 |
816 |
817 | private int importanceType(FeatureImportanceType tpe) {
818 | int importanceType = C_API_FEATURE_IMPORTANCE_GAIN;
819 | switch (tpe) {
820 | case GAIN:
821 | importanceType = C_API_FEATURE_IMPORTANCE_GAIN;
822 | break;
823 | case SPLIT:
824 | importanceType = C_API_FEATURE_IMPORTANCE_SPLIT;
825 | break;
826 | }
827 | return importanceType;
828 | }
829 |
830 | /**
831 | * Get number of classes.
832 | * @return Number of classes
833 | * @throws LGBMException
834 | */
835 | public int getNumClasses() throws LGBMException {
836 | if (!isClosed) {
837 | SWIGTYPE_p_int numHandle = new_int32_tp();
838 | int result = LGBM_BoosterGetNumClasses(voidpp_value(handle), numHandle);
839 | if (result < 0) {
840 | delete_intp(numHandle);
841 | throw new LGBMException(LGBM_GetLastError());
842 | } else {
843 | int numClasses = intp_value(numHandle);
844 | delete_intp(numHandle);
845 | return numClasses;
846 | }
847 | } else {
848 | throw new LGBMException("Booster was already closed");
849 | }
850 | }
851 |
852 | /**
853 | * Get number of predictions for training data and validation data (this can be used to support customized evaluation functions).
854 | * @param dataIdx Index of data, 0: training data, 1: 1st validation data, 2: 2nd validation data and so on
855 | * @return Number of predictions
856 | * @throws LGBMException
857 | */
858 | public long getNumPredict(int dataIdx) throws LGBMException {
859 | if (!isClosed) {
860 | SWIGTYPE_p_long_long numHandle = new_int64_tp();
861 | int result = LGBM_BoosterGetNumPredict(voidpp_value(handle), dataIdx, numHandle);
862 | if (result < 0) {
863 | delete_int64_tp(numHandle);
864 | throw new LGBMException(LGBM_GetLastError());
865 | } else {
866 | long numClasses = int64_tp_value(numHandle);
867 | delete_int64_tp(numHandle);
868 | return numClasses;
869 | }
870 | } else {
871 | throw new LGBMException("Booster was already closed");
872 | }
873 | }
874 |
875 | /**
876 | * Get prediction for training data and validation data.
877 | * @param dataIdx Index of data, 0: training data, 1: 1st validation data, 2: 2nd validation data and so on
878 | * @return array with predictions, of size num_class * dataset.num_data
879 | * @throws LGBMException
880 | */
881 | public double[] getPredict(int dataIdx) throws LGBMException {
882 | if (!isClosed) {
883 | int allocatedSize = getNumClasses() * (int)getNumPredict(dataIdx);
884 | SWIGTYPE_p_double buffer = new_doubleArray(allocatedSize);
885 | SWIGTYPE_p_long_long size = new_int64_tp();
886 | int result = LGBM_BoosterGetPredict(voidpp_value(handle), dataIdx, size, buffer);
887 | if (result < 0) {
888 | delete_doubleArray(buffer);
889 | delete_int64_tp(size);
890 | throw new LGBMException(LGBM_GetLastError());
891 | } else {
892 | double[] out = new double[(int)int64_tp_value(size)];
893 | for (int i=0; ipredictForMat &
948 | * predictForMatSingleRow
949 | * for more info.
950 | *
951 | * @param rows the number of rows in the input data
952 | * @param cols the number of columns in the input data
953 | * @param predictionType the type of prediction we are trying to achieve
954 | * @return number of elements in the output result (size)
955 | */
956 | private long outBufferSize(int rows, int cols, PredictionType predictionType) {
957 | long defaultSize = 2L * rows;
958 | if (PredictionType.C_API_PREDICT_CONTRIB.equals(predictionType))
959 | return defaultSize * (cols + 1);
960 | else if (PredictionType.C_API_PREDICT_LEAF_INDEX.equals(predictionType))
961 | return defaultSize * iterations;
962 | else // for C_API_PREDICT_NORMAL & C_API_PREDICT_RAW_SCORE
963 | return defaultSize;
964 | }
965 |
966 | }
967 |
--------------------------------------------------------------------------------
/src/main/java/io/github/metarank/lightgbm4j/LGBMDataset.java:
--------------------------------------------------------------------------------
1 | package io.github.metarank.lightgbm4j;
2 |
3 | import com.microsoft.ml.lightgbm.*;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import java.io.IOException;
8 |
9 | import static com.microsoft.ml.lightgbm.lightgbmlib.*;
10 |
11 | public class LGBMDataset implements AutoCloseable {
12 | private volatile boolean isClosed = false;
13 |
14 | private static final Logger logger = LoggerFactory.getLogger(LGBMDataset.class);
15 |
16 | public SWIGTYPE_p_void handle;
17 | static {
18 | try {
19 | LGBMBooster.loadNative();
20 | } catch (IOException e) {
21 | logger.error("Cannot load native library for your platform", e);
22 | }
23 | }
24 |
25 | LGBMDataset(SWIGTYPE_p_void handle) {
26 | this.handle = handle;
27 | }
28 |
29 | /**
30 | * Load dataset from file (like LightGBM CLI version does).
31 | * @param fileName The name of the file
32 | * @param parameters Additional parameters
33 | * @return
34 | * @throws LGBMException
35 | */
36 | public static LGBMDataset createFromFile(String fileName, String parameters, LGBMDataset reference) throws LGBMException {
37 | SWIGTYPE_p_p_void handle = new_voidpp();
38 | int result = LGBM_DatasetCreateFromFile(fileName, parameters, reference == null ? null : reference.handle, handle);
39 | if (result < 0) {
40 | throw new LGBMException(LGBM_GetLastError());
41 | } else {
42 | return new LGBMDataset(voidpp_value(handle));
43 | }
44 | }
45 |
46 | /**
47 | * Create dataset from dense float[] matrix.
48 | * @param data input matrix
49 | * @param rows number of rows
50 | * @param cols number of cols
51 | * @param isRowMajor is a row-major encoding used?
52 | * @param parameters extra parameters
53 | * @return
54 | * @throws LGBMException
55 | */
56 | public static LGBMDataset createFromMat(float[] data, int rows, int cols, boolean isRowMajor, String parameters, LGBMDataset reference) throws LGBMException {
57 | SWIGTYPE_p_p_void handle = new_voidpp();
58 | SWIGTYPE_p_float dataBuffer = new_floatArray(data.length);
59 | for (int i = 0; i < data.length; i++) {
60 | floatArray_setitem(dataBuffer, i, data[i]);
61 | }
62 |
63 | int result = LGBM_DatasetCreateFromMat(
64 | float_to_voidp_ptr(dataBuffer),
65 | C_API_DTYPE_FLOAT32,
66 | rows,
67 | cols,
68 | isRowMajor ? 1 : 0,
69 | parameters,
70 | reference == null ? null : reference.handle,
71 | handle
72 | );
73 | delete_floatArray(dataBuffer);
74 | if (result < 0) {
75 | throw new LGBMException(LGBM_GetLastError());
76 | } else {
77 | return new LGBMDataset(voidpp_value(handle));
78 | }
79 | }
80 | /**
81 | * Create dataset from dense double[] matrix.
82 | * @param data input matrix
83 | * @param rows number of rows
84 | * @param cols number of cols
85 | * @param isRowMajor is a row-major encoding used?
86 | * @param parameters extra parameters
87 | * @param reference to align bin mappers with other dataset
88 | * @return
89 | * @throws LGBMException
90 | */
91 | public static LGBMDataset createFromMat(double[] data, int rows, int cols, boolean isRowMajor, String parameters, LGBMDataset reference) throws LGBMException {
92 | SWIGTYPE_p_p_void handle = new_voidpp();
93 | SWIGTYPE_p_double dataBuffer = new_doubleArray(data.length);
94 | for (int i = 0; i < data.length; i++) {
95 | doubleArray_setitem(dataBuffer, i, data[i]);
96 | }
97 | int result = LGBM_DatasetCreateFromMat(
98 | double_to_voidp_ptr(dataBuffer),
99 | C_API_DTYPE_FLOAT64,
100 | rows,
101 | cols,
102 | isRowMajor ? 1 : 0,
103 | parameters,
104 | reference == null ? null : reference.handle,
105 | handle
106 | );
107 | delete_doubleArray(dataBuffer);
108 | if (result < 0) {
109 | throw new LGBMException(LGBM_GetLastError());
110 | } else {
111 | return new LGBMDataset(voidpp_value(handle));
112 | }
113 | }
114 |
115 | /**
116 | * Get number of data points.
117 | * @return number of data points
118 | * @throws LGBMException
119 | */
120 | public int getNumData() throws LGBMException {
121 | if (!isClosed) {
122 | SWIGTYPE_p_int numDataP = new_intp();
123 | int result = LGBM_DatasetGetNumData(handle, numDataP);
124 | if (result < 0) {
125 | throw new LGBMException(LGBM_GetLastError());
126 | } else {
127 | int numData = intp_value(numDataP);
128 | delete_intp(numDataP);
129 | return numData;
130 | }
131 | } else {
132 | throw new LGBMException("Dataset was already closed.");
133 | }
134 | }
135 |
136 | /**
137 | * Get number of features.
138 | * @return number of features
139 | * @throws LGBMException
140 | */
141 | public int getNumFeatures() throws LGBMException {
142 | if (!isClosed) {
143 | SWIGTYPE_p_int numFeaturesP = new_intp();
144 | int result = LGBM_DatasetGetNumFeature(handle, numFeaturesP);
145 | if (result < 0) {
146 | throw new LGBMException(LGBM_GetLastError());
147 | } else {
148 | int numFeatures = intp_value(numFeaturesP);
149 | delete_intp(numFeaturesP);
150 | return numFeatures;
151 | }
152 | } else {
153 | throw new LGBMException("Dataset was already closed.");
154 | }
155 | }
156 |
157 | /**
158 | * Set feature names
159 | * @param featureNames a list of names.
160 | * @throws LGBMException
161 | */
162 | public void setFeatureNames(String[] featureNames) throws LGBMException {
163 | if (!isClosed) {
164 | int result = LGBM_DatasetSetFeatureNames(handle, featureNames, featureNames.length);
165 | if (result < 0) {
166 | throw new LGBMException(LGBM_GetLastError());
167 | }
168 | } else {
169 | throw new LGBMException("Dataset was already closed.");
170 | }
171 | }
172 |
173 | /**
174 | * Dumps dataset into a file for debugging.
175 | * @param fileName
176 | * @throws LGBMException
177 | */
178 | public void dumpText(String fileName) throws LGBMException {
179 | if (!isClosed) {
180 | int result = LGBM_DatasetDumpText(handle, fileName);
181 | if (result < 0) {
182 | throw new LGBMException(LGBM_GetLastError());
183 | }
184 | } else {
185 | throw new LGBMException("Dataset was already closed.");
186 | }
187 | }
188 |
189 | /**
190 | * Sets a double field. label and weight fields can only be float[]
191 | * @param fieldName
192 | * @param data
193 | * @throws LGBMException
194 | */
195 | public void setField(String fieldName, double[] data) throws LGBMException {
196 | if (!isClosed) {
197 | if (fieldName.equals("label")) throw new LGBMException("label can only be float[]");
198 | if (fieldName.equals("weight")) throw new LGBMException("weight can only be float[]");
199 | SWIGTYPE_p_double dataBuffer = new_doubleArray(data.length);
200 | for (int i = 0; i < data.length; i++) {
201 | doubleArray_setitem(dataBuffer, i, data[i]);
202 | }
203 |
204 | int result = LGBM_DatasetSetField(handle, fieldName, double_to_voidp_ptr(dataBuffer), data.length, C_API_DTYPE_FLOAT64);
205 | delete_doubleArray(dataBuffer);
206 | if (result < 0) {
207 | throw new LGBMException(LGBM_GetLastError());
208 | }
209 | } else {
210 | throw new LGBMException("Dataset was already closed.");
211 | }
212 | }
213 |
214 | /**
215 | * Sets an int field. It can only accept the group field.
216 | * @param fieldName
217 | * @param data
218 | * @throws LGBMException
219 | */
220 | public void setField(String fieldName, int[] data) throws LGBMException {
221 | if (!isClosed) {
222 | if (fieldName.equals("label")) throw new LGBMException("label can only be float[]");
223 | if (fieldName.equals("weight")) throw new LGBMException("weight can only be float[]");
224 | SWIGTYPE_p_int dataBuffer = new_intArray(data.length);
225 | for (int i = 0; i < data.length; i++) {
226 | intArray_setitem(dataBuffer, i, data[i]);
227 | }
228 |
229 | int result = LGBM_DatasetSetField(handle, fieldName, int_to_voidp_ptr(dataBuffer), data.length, C_API_DTYPE_INT32);
230 | delete_intArray(dataBuffer);
231 | if (result < 0) {
232 | throw new LGBMException(LGBM_GetLastError());
233 | }
234 | } else {
235 | throw new LGBMException("Dataset was already closed.");
236 | }
237 | }
238 |
239 | /**
240 | * Sets a double field. label and weight fields can only be float[]
241 | * @param fieldName
242 | * @param data
243 | * @throws LGBMException
244 | */
245 | public void setField(String fieldName, float[] data) throws LGBMException {
246 | if (!isClosed) {
247 | SWIGTYPE_p_float dataBuffer = new_floatArray(data.length);
248 | for (int i = 0; i < data.length; i++) {
249 | floatArray_setitem(dataBuffer, i, data[i]);
250 | }
251 |
252 | int result = LGBM_DatasetSetField(handle, fieldName,float_to_voidp_ptr(dataBuffer), data.length, C_API_DTYPE_FLOAT32);
253 | delete_floatArray(dataBuffer);
254 | if (result < 0) {
255 | throw new LGBMException(LGBM_GetLastError());
256 | }
257 | } else {
258 | throw new LGBMException("Dataset was already closed.");
259 | }
260 | }
261 |
262 | /**
263 | * Gets feature names from dataset, if dataset supports it.
264 | * @return list of feature names
265 | * @throws LGBMException
266 | */
267 | public String[] getFeatureNames() throws LGBMException {
268 | if (!isClosed) {
269 | SWIGTYPE_p_void arrayHandle = LGBM_DatasetGetFeatureNamesSWIG(handle);
270 | String[] names = StringArrayHandle_get_strings(arrayHandle);
271 | StringArrayHandle_free(arrayHandle);
272 | return names;
273 | } else {
274 | throw new LGBMException("Dataset was already closed.");
275 | }
276 | }
277 |
278 | /**
279 | * Get float[] field from the dataset.
280 | * @param field Field name
281 | * @return
282 | * @throws LGBMException
283 | */
284 | public float[] getFieldFloat(String field) throws LGBMException {
285 | if (!isClosed) {
286 | SWIGTYPE_p_int lenPtr = new_intp();
287 | SWIGTYPE_p_p_void bufferPtr = new_voidpp();
288 | SWIGTYPE_p_int typePtr = new_intp();
289 | int result = LGBM_DatasetGetField(handle, field, lenPtr, bufferPtr, typePtr);
290 | if (result < 0) {
291 | delete_intp(lenPtr);
292 | delete_voidpp(bufferPtr);
293 | delete_intp(typePtr);
294 | throw new LGBMException(LGBM_GetLastError());
295 | } else {
296 | int len = intp_value(lenPtr);
297 | int type = intp_value(typePtr);
298 | if (type == C_API_DTYPE_FLOAT32) {
299 | SWIGTYPE_p_void buf = voidpp_value(bufferPtr);
300 | float[] out = new float[len];
301 | for (int i=0; i 0);
29 | }
30 | String[] names = booster.getFeatureNames();
31 | double[] weights = booster.featureImportance(0, LGBMBooster.FeatureImportanceType.GAIN);
32 | assertTrue(names.length > 0);
33 | assertTrue(weights.length > 0);
34 | booster.close();
35 | dataset.close();
36 | }
37 |
38 | public static Stream datasets() throws LGBMException, IOException {
39 | return Stream.of(
40 | Arguments.of(datasetFromFile()),
41 | Arguments.of(datasetFromMat()),
42 | Arguments.of(datasetReadmeExample())
43 | );
44 | }
45 | public static LGBMDataset datasetFromFile() throws LGBMException, IOException {
46 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
47 | return dataset;
48 | }
49 |
50 | private static LGBMDataset datasetFromMat() throws LGBMException, IOException {
51 | BufferedReader reader = new BufferedReader(new InputStreamReader(CancerIntegrationTest.class.getResourceAsStream("/cancer.csv")));
52 | String[] names = Arrays.copyOfRange(reader.readLine().split(","), 0, 9);
53 | ArrayList valuesBuffer = new ArrayList<>();
54 | ArrayList labelsBuffer = new ArrayList<>();
55 | String line = reader.readLine();
56 | int rows = 0;
57 | while (line != null) {
58 | rows++;
59 | String[] parts = line.split(",");
60 | for (int i=0; i < parts.length; i++) {
61 | if (i < parts.length - 1) {
62 | valuesBuffer.add(Double.parseDouble(parts[i]));
63 | } else {
64 | labelsBuffer.add(Float.parseFloat(parts[i]));
65 | }
66 | }
67 | line = reader.readLine();
68 | }
69 | double[] values = new double[valuesBuffer.size()];
70 | for (int i=0; i < valuesBuffer.size(); i++) {
71 | values[i] = valuesBuffer.get(i);
72 | }
73 | float[] labels = new float[labelsBuffer.size()];
74 | for (int i=0; i < labelsBuffer.size(); i++) {
75 | labels[i] = labelsBuffer.get(i);
76 | }
77 | reader.close();
78 | LGBMDataset dataset = LGBMDataset.createFromMat(values, rows, names.length, true, "", null);
79 | dataset.setFeatureNames(names);
80 | dataset.setField("label", labels);
81 | return dataset;
82 | }
83 |
84 | public static LGBMDataset datasetReadmeExample() throws LGBMException {
85 | String[] columns = new String[] {
86 | "Age","BMI","Glucose","Insulin","HOMA","Leptin","Adiponectin","Resistin","MCP.1"
87 | };
88 | double[] values = new double[] {
89 | 71,30.3,102,8.34,2.098344,56.502,8.13,4.2989,200.976,
90 | 66,27.7,90,6.042,1.341324,24.846,7.652055,6.7052,225.88,
91 | 75,25.7,94,8.079,1.8732508,65.926,3.74122,4.49685,206.802,
92 | 78,25.3,60,3.508,0.519184,6.633,10.567295,4.6638,209.749,
93 | 69,29.4,89,10.704,2.3498848,45.272,8.2863,4.53,215.769,
94 | 85,26.6,96,4.462,1.0566016,7.85,7.9317,9.6135,232.006,
95 | 76,27.1,110,26.211,7.111918,21.778,4.935635,8.49395,45.843,
96 | 77,25.9,85,4.58,0.960273333,13.74,9.75326,11.774,488.829,
97 | 45,21.30394858,102,13.852,3.4851632,7.6476,21.056625,23.03408,552.444,
98 | 45,20.82999519,74,4.56,0.832352,7.7529,8.237405,28.0323,382.955,
99 | 49,20.9566075,94,12.305,2.853119333,11.2406,8.412175,23.1177,573.63,
100 | 34,24.24242424,92,21.699,4.9242264,16.7353,21.823745,12.06534,481.949,
101 | 42,21.35991456,93,2.999,0.6879706,19.0826,8.462915,17.37615,321.919,
102 | 68,21.08281329,102,6.2,1.55992,9.6994,8.574655,13.74244,448.799,
103 | 51,19.13265306,93,4.364,1.0011016,11.0816,5.80762,5.57055,90.6,
104 | 62,22.65625,92,3.482,0.790181867,9.8648,11.236235,10.69548,703.973
105 | };
106 |
107 | float[] labels = new float[] {
108 | 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
109 | };
110 | LGBMDataset dataset = LGBMDataset.createFromMat(values, 16, columns.length, true, "", null);
111 | dataset.setFeatureNames(columns);
112 | dataset.setField("label", labels);
113 | return dataset;
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/src/test/java/io/github/metarank/lightgbm4j/CategoricalIntegrationTest.java:
--------------------------------------------------------------------------------
1 | package io.github.metarank.lightgbm4j;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | public class CategoricalIntegrationTest {
6 | @Test
7 | void testCategorical() throws LGBMException {
8 | LGBMDataset ds = LGBMDataset.createFromFile("./src/test/resources/categorical.data", "", null);
9 | ds.setFeatureNames(new String[]{"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7"});
10 | String params = "objective=binary label=name:Classification categorical_feature=f0,f1,f2,f3,f4,f5,f6,f7";
11 | LGBMBooster booster = LGBMBooster.create(ds, params);
12 | for (int i=0; i<10; i++) {
13 | booster.updateOneIter();
14 | double[] eval1 = booster.getEval(0);
15 | System.out.println("train " + eval1[0]);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/test/java/io/github/metarank/lightgbm4j/CustomObjectiveTest.java:
--------------------------------------------------------------------------------
1 | package io.github.metarank.lightgbm4j;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.io.IOException;
6 |
7 | public class CustomObjectiveTest {
8 |
9 | @Test
10 | void testCancerCustomObjective() throws LGBMException, IOException {
11 | LGBMDataset dataset = CancerIntegrationTest.datasetFromFile();
12 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=none metric=rmse label=name:Classification");
13 | // actual ground truth label values
14 | float y[] = dataset.getFieldFloat("label");
15 | for (int it=0; it<10; it++) {
16 | // predictions for current iteration
17 | double[] yhat = booster.getPredict(0);
18 | float[] grad = new float[y.length];
19 | float[] hess = new float[y.length];
20 | for (int i=0; i {
29 | LGBMBooster.createFromModelfile("whatever");
30 | });
31 | }
32 |
33 | @Test
34 | public void testCreate() {
35 | Assertions.assertDoesNotThrow(() -> {
36 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
37 | LGBMBooster.create(ds, "");
38 | });
39 | }
40 |
41 | @Test
42 | public void testCreateFree() {
43 | Assertions.assertDoesNotThrow(() -> {
44 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
45 | LGBMBooster booster = LGBMBooster.create(ds, "");
46 | ds.close();
47 | booster.close();
48 | });
49 | }
50 |
51 | @Test
52 | public void testUpdateOneIter() throws LGBMException {
53 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
54 | LGBMBooster booster = LGBMBooster.create(ds, "");
55 | boolean finished = booster.updateOneIter();
56 | ds.close();
57 | booster.close();
58 | assertTrue(finished);
59 | }
60 |
61 | @Test
62 | public void testSaveModelToString() throws LGBMException {
63 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
64 | LGBMBooster booster = LGBMBooster.create(ds, "");
65 | String model = booster.saveModelToString(0, 0, LGBMBooster.FeatureImportanceType.GAIN);
66 | ds.close();
67 | booster.close();
68 | assertFalse(model.isEmpty(), "model string should not be empty");
69 | }
70 |
71 | @Test
72 | public void testLoadSaveString() throws LGBMException {
73 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
74 | LGBMBooster booster = LGBMBooster.create(ds, "");
75 | String model = booster.saveModelToString(0, 0, LGBMBooster.FeatureImportanceType.GAIN);
76 | ds.close();
77 | booster.close();
78 | Assertions.assertDoesNotThrow(() -> {
79 | LGBMBooster loaded = LGBMBooster.loadModelFromString(model);
80 | loaded.close();
81 | });
82 | }
83 |
84 | @Test
85 | public void testGetFeatureNames() throws LGBMException {
86 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
87 | LGBMBooster booster = LGBMBooster.create(ds, "");
88 | String[] names = booster.getFeatureNames();
89 | assertTrue(names.length > 0, "feature names should be present");
90 | ds.close();
91 | booster.close();
92 | }
93 |
94 | @Test
95 | public void testPredictForMatFloatNormalPrediction() throws LGBMException {
96 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
97 | LGBMBooster booster = LGBMBooster.create(ds, "");
98 | boolean finished = booster.updateOneIter();
99 | double[] pred;
100 | for (int i = 0; i < 10; i++) {
101 | // repeat multiple times to ensure it works
102 | // offline tests showed that one execution can work, while in multiple iterations it fails
103 | // valid for all types of predictions
104 | pred = booster.predictForMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, PredictionType.C_API_PREDICT_NORMAL);
105 | assertTrue(pred.length > 0, "predicted values should not be empty");
106 | }
107 | ds.close();
108 | booster.close();
109 | }
110 |
111 | @Test
112 | public void testPredictForMatFloatRawScorePrediction() throws LGBMException {
113 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
114 | LGBMBooster booster = LGBMBooster.create(ds, "");
115 | boolean finished = booster.updateOneIter();
116 | double[] pred;
117 | for (int i = 0; i < 10; i++) {
118 | pred = booster.predictForMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, PredictionType.C_API_PREDICT_RAW_SCORE);
119 | assertTrue(pred.length > 0, "predicted values should not be empty");
120 | }
121 | ds.close();
122 | booster.close();
123 | }
124 |
125 | @Test
126 | public void testPredictForMatFloatLeafIndexPrediction() throws LGBMException {
127 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
128 | LGBMBooster booster = LGBMBooster.create(ds, "");
129 | boolean finished = booster.updateOneIter();
130 | double[] pred;
131 | for (int i = 0; i < 10; i++) {
132 | pred = booster.predictForMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, PredictionType.C_API_PREDICT_LEAF_INDEX);
133 | assertTrue(pred.length > 0, "predicted values should not be empty");
134 | }
135 | ds.close();
136 | booster.close();
137 | }
138 |
139 | @Test
140 | public void testPredictForMatFloatContributionPrediction() throws LGBMException {
141 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
142 | LGBMBooster booster = LGBMBooster.create(ds, "");
143 | boolean finished = booster.updateOneIter();
144 | double[] pred;
145 | for (int i = 0; i < 10; i++) {
146 | pred = booster.predictForMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, PredictionType.C_API_PREDICT_CONTRIB);
147 | assertTrue(pred.length > 0, "predicted values should not be empty");
148 | }
149 | ds.close();
150 | booster.close();
151 | }
152 |
153 | @Test
154 | public void testPredictForMatDouble() throws LGBMException {
155 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
156 | LGBMBooster booster = LGBMBooster.create(ds, "");
157 | boolean finished = booster.updateOneIter();
158 | double[] pred;
159 | for (int i = 0; i < 10; i++) {
160 | pred = booster.predictForMat(new double[]{1.0, 1.0, 1.0, 1.0}, 2, 2, true, PredictionType.C_API_PREDICT_NORMAL);
161 | assertTrue(pred.length > 0, "predicted values should not be empty");
162 | }
163 | ds.close();
164 | booster.close();
165 | }
166 |
167 | @Test
168 | public void testAddValidData() throws LGBMException {
169 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
170 | LGBMBooster booster = LGBMBooster.create(ds, "");
171 | booster.addValidData(ds);
172 | ds.close();
173 | booster.close();
174 | }
175 |
176 | @Test
177 | public void testGetEval() throws LGBMException {
178 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
179 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
180 | boolean finished = booster.updateOneIter();
181 | double[] eval = booster.getEval(0);
182 | assertTrue(eval.length > 0);
183 | assertTrue(eval[0] > 0);
184 | dataset.close();
185 | booster.close();
186 | }
187 |
188 | @Test
189 | public void testGetEvalNames() throws LGBMException {
190 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
191 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
192 | booster.addValidData(dataset);
193 | String[] eval = booster.getEvalNames();
194 | assertTrue(eval.length > 0);
195 | dataset.close();
196 | booster.close();
197 | }
198 |
199 | @Test
200 | public void testFeatureImportance() throws LGBMException {
201 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
202 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
203 | booster.updateOneIter();
204 | booster.updateOneIter();
205 | booster.updateOneIter();
206 | String[] names = booster.getFeatureNames();
207 | double[] importance = booster.featureImportance(0, LGBMBooster.FeatureImportanceType.GAIN);
208 | assertTrue(names.length > 0);
209 | assertTrue(importance.length > 0);
210 | dataset.close();
211 | booster.close();
212 | }
213 |
214 | @Test
215 | public void testPredictForMatSingleRowNormalPrediction() throws LGBMException {
216 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
217 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
218 | booster.updateOneIter();
219 | booster.updateOneIter();
220 | booster.updateOneIter();
221 | for (int i = 0; i < 10; i++) {
222 | double pred1 = booster.predictForMatSingleRow(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_NORMAL);
223 | assertTrue(pred1 > 0);
224 | double pred2 = booster.predictForMatSingleRow(new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_NORMAL);
225 | assertTrue(pred2 > 0);
226 | }
227 | dataset.close();
228 | booster.close();
229 | }
230 |
231 | @Test
232 | public void testPredictForMatSingleRowRawScorePrediction() throws LGBMException {
233 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
234 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
235 | booster.updateOneIter();
236 | booster.updateOneIter();
237 | booster.updateOneIter();
238 | for (int i = 0; i < 10; i++) {
239 | // we assert that the result is finite, as it get both negative and positive values
240 | double pred1 = booster.predictForMatSingleRow(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_RAW_SCORE);
241 | assertTrue(Double.isFinite(pred1));
242 | double pred2 = booster.predictForMatSingleRow(new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_RAW_SCORE);
243 | assertTrue(Double.isFinite(pred2));
244 | }
245 | dataset.close();
246 | booster.close();
247 | }
248 |
249 | @Test
250 | public void testPredictForMatSingleRowLeafIndexPrediction() throws LGBMException {
251 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
252 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
253 | booster.updateOneIter();
254 | booster.updateOneIter();
255 | booster.updateOneIter();
256 | for (int i = 0; i < 10; i++) {
257 | // we assert that the result is finite, as it get both negative and positive values
258 | double pred1 = booster.predictForMatSingleRow(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_LEAF_INDEX);
259 | assertTrue(Double.isFinite(pred1));
260 | double pred2 = booster.predictForMatSingleRow(new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_LEAF_INDEX);
261 | assertTrue(Double.isFinite(pred2));
262 | }
263 | dataset.close();
264 | booster.close();
265 | }
266 |
267 | @Test
268 | public void testPredictForMatSingleRowContributionPrediction() throws LGBMException {
269 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
270 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
271 | booster.updateOneIter();
272 | booster.updateOneIter();
273 | booster.updateOneIter();
274 | for (int i = 0; i < 10; i++) {
275 | // we assert that the result is finite, as it get both negative and positive values
276 | double pred1 = booster.predictForMatSingleRow(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_CONTRIB);
277 | assertTrue(Double.isFinite(pred1));
278 | double pred2 = booster.predictForMatSingleRow(new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_CONTRIB);
279 | assertTrue(Double.isFinite(pred2));
280 | }
281 | dataset.close();
282 | booster.close();
283 | }
284 |
285 | @Test
286 | void testCreateByReference() throws LGBMException {
287 | LGBMDataset dataset1 = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
288 | LGBMDataset dataset2 = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", dataset1);
289 | LGBMBooster booster = LGBMBooster.create(dataset1, "objective=binary label=name:Classification");
290 | booster.updateOneIter();
291 | booster.updateOneIter();
292 | booster.updateOneIter();
293 |
294 | booster.addValidData(dataset2);
295 | double[] train = booster.getEval(0);
296 | double[] test = booster.getEval(1);
297 | assertEquals(train[0], test[0], 0.001);
298 | }
299 |
300 | @Test void testGetNumClasses() throws LGBMException {
301 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
302 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
303 | assertEquals(booster.getNumClasses(), 1);
304 | dataset.close();
305 | booster.close();
306 | }
307 |
308 | @Test void testGetNumPredict() throws LGBMException {
309 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
310 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
311 | assertEquals(booster.getNumPredict(0), 116);
312 | dataset.close();
313 | booster.close();
314 | }
315 |
316 | @Test void testGetPredict() throws LGBMException {
317 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
318 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
319 | booster.updateOneIter();
320 | booster.updateOneIter();
321 | booster.updateOneIter();
322 | double[] preds = booster.getPredict(0);
323 | assertEquals(preds.length, 116);
324 | dataset.close();
325 | booster.close();
326 | }
327 |
328 | @Test void testUpdateOneIterCustom() throws LGBMException {
329 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
330 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=none metric=rmse label=name:Classification");
331 | int size = dataset.getNumData();
332 | booster.updateOneIterCustom(randomArray(size), randomArray(size));
333 | booster.updateOneIterCustom(randomArray(size), randomArray(size));
334 | booster.updateOneIterCustom(randomArray(size), randomArray(size));
335 | double[] preds = booster.getPredict(0);
336 | assertEquals(preds.length, 116);
337 | dataset.close();
338 | booster.close();
339 | }
340 |
341 | @Test void testDoubleClose() throws LGBMException {
342 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
343 | LGBMBooster booster = LGBMBooster.create(ds, "");
344 | booster.close();
345 | assertThrows(LGBMException.class, booster::close);
346 |
347 | }
348 |
349 | @Test void testUseAfterClose() throws LGBMException {
350 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
351 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
352 | booster.updateOneIter();
353 | booster.updateOneIter();
354 | booster.updateOneIter();
355 | booster.close();
356 | assertThrows(LGBMException.class, () -> booster.getPredict(0));
357 | }
358 |
359 | @Test void testPredictFastFloat() throws LGBMException, Exception {
360 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
361 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
362 | booster.updateOneIter();
363 | booster.updateOneIter();
364 | booster.updateOneIter();
365 | LGBMBooster.FastConfig config = booster.predictForMatSingleRowFastInit(PredictionType.C_API_PREDICT_NORMAL, C_API_DTYPE_FLOAT32,9, "");
366 | double pred = booster.predictForMatSingleRowFast(config, new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_NORMAL);
367 | assertTrue(Double.isFinite(pred));
368 | config.close();
369 | dataset.close();
370 | booster.close();
371 | }
372 |
373 | @Test void testPredictFastDouble() throws LGBMException, Exception {
374 | LGBMDataset dataset = LGBMDataset.createFromFile("src/test/resources/cancer.csv", "header=true label=name:Classification", null);
375 | LGBMBooster booster = LGBMBooster.create(dataset, "objective=binary label=name:Classification");
376 | booster.updateOneIter();
377 | booster.updateOneIter();
378 | booster.updateOneIter();
379 | LGBMBooster.FastConfig config = booster.predictForMatSingleRowFastInit(PredictionType.C_API_PREDICT_NORMAL, C_API_DTYPE_FLOAT64,9, "");
380 | double pred = booster.predictForMatSingleRowFast(config, new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, PredictionType.C_API_PREDICT_NORMAL);
381 | assertTrue(Double.isFinite(pred));
382 | config.close();
383 | dataset.close();
384 | booster.close();
385 | }
386 |
387 | private float[] randomArray(int size) {
388 | float[] result = new float[size];
389 | Random rnd = new Random();
390 | for (int i=0; i {
15 | LGBMDataset.createFromFile("whatever", "", null);
16 | });
17 | }
18 |
19 | @Test void testCreateFromFile() {
20 | Assertions.assertDoesNotThrow( () -> {
21 | LGBMDataset.createFromFile("./src/test/resources/categorical.data", "", null);
22 | });
23 | }
24 |
25 | @Test void testCreateFromMatFloat() {
26 | Assertions.assertDoesNotThrow( () -> {
27 | LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
28 | });
29 | }
30 |
31 | @Test void testCreateFromMatDouble() {
32 | Assertions.assertDoesNotThrow( () -> {
33 | LGBMDataset.createFromMat(new double[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
34 | });
35 | }
36 |
37 | @Test void testGetNumData() throws LGBMException {
38 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
39 | assertEquals(2, ds.getNumData());
40 | ds.close();
41 | }
42 |
43 | @Test void testGetNumFeature() throws LGBMException {
44 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
45 | assertEquals(2, ds.getNumFeatures());
46 | ds.close();
47 | }
48 |
49 | @Test void testSetField() throws LGBMException {
50 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
51 | ds.setField("label", new float[] {1.0f, 0.0f});
52 | ds.close();
53 | }
54 |
55 | @Test void testGetFieldFloat() throws LGBMException {
56 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
57 | ds.setField("label", new float[] {1.0f, 0.0f});
58 | float[] labels = ds.getFieldFloat("label");
59 | assertEquals(labels[0], 1.0f);
60 | assertEquals(labels[1], 0.0f);
61 | ds.close();
62 | }
63 |
64 | @Test void testGetFieldInt() throws LGBMException {
65 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 3.0f, 4.0f, 5.0f, 6.0f}, 5, 2, true, "", null);
66 | ds.setField("label", new float[] {1.0f, 0.0f, 1.0f, 1.0f, 0.0f});
67 | ds.setField("group", new int[] { 3, 2 });
68 | int[] groups = ds.getFieldInt("group");
69 | assertEquals(groups[0], 0);
70 | assertEquals(groups[1], 3);
71 | assertEquals(groups[2], 5);
72 | ds.close();
73 | }
74 |
75 | @Test void testSetFeatureNames() throws LGBMException {
76 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
77 | Assertions.assertDoesNotThrow( () -> {
78 | ds.setFeatureNames(new String[] {"foo", "bar"});
79 | });
80 | ds.close();
81 | }
82 |
83 | @Test void testGetFeatureNames() throws LGBMException {
84 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
85 | Assertions.assertDoesNotThrow( () -> {
86 | ds.setFeatureNames(new String[] {"foo", "bar"});
87 | });
88 | String[] names = ds.getFeatureNames();
89 | assertArrayEquals(names, new String[] {"foo", "bar"});
90 | ds.close();
91 | }
92 |
93 | @Test void testDoubleClose() throws LGBMException {
94 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
95 | ds.close();
96 | ds.close();
97 | }
98 |
99 | @Test void testUseAfterClose() throws LGBMException {
100 | LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
101 | ds.close();
102 | assertThrows(LGBMException.class, ds::getFeatureNames);
103 |
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/src/test/java/io/github/metarank/lightgbm4j/MultithreadTest.java:
--------------------------------------------------------------------------------
1 | package io.github.metarank.lightgbm4j;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.concurrent.*;
8 |
9 | public class MultithreadTest {
10 | @Test
11 | public void testThreading() throws Exception {
12 | ExecutorService pool = Executors.newFixedThreadPool(32);
13 | List queue = new ArrayList<>();
14 | for (int i = 0; i < 1000; i++) {
15 | queue.add(new CreateTask());
16 | }
17 | pool.invokeAll(queue);
18 | }
19 |
20 |
21 | public class CreateTask implements Callable {
22 |
23 | @Override
24 | public Integer call() throws Exception {
25 | LGBMDataset ds = LGBMDataset.createFromMat(new float[] {1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
26 | LGBMBooster.create(ds, "");
27 |
28 | return 0;
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/io/github/metarank/lightgbm4j/RankingIntegrationTest.java:
--------------------------------------------------------------------------------
1 | package io.github.metarank.lightgbm4j;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.io.BufferedReader;
6 | import java.io.IOException;
7 | import java.io.InputStreamReader;
8 | import java.util.*;
9 | import java.util.stream.Collectors;
10 | import java.util.zip.GZIPInputStream;
11 |
12 | import static org.junit.jupiter.api.Assertions.assertTrue;
13 |
14 | public class RankingIntegrationTest {
15 | @Test
16 | public void testLetor() throws LGBMException, IOException {
17 | LGBMDataset train = datasetFromResource("/mq2008/train.txt.gz", null, false);
18 | LGBMDataset test = datasetFromResource("/mq2008/test.txt.gz", train, false);
19 | trainBooster(train, test);
20 | train.close();
21 | test.close();
22 | }
23 |
24 | @Test
25 | public void testLetorPosition() throws LGBMException, IOException {
26 | LGBMDataset train = datasetFromResource("/mq2008/train.txt.gz", null, true);
27 | LGBMDataset test = datasetFromResource("/mq2008/test.txt.gz", train, true);
28 | trainBooster(train, test);
29 | train.close();
30 | test.close();
31 | }
32 |
33 | public void trainBooster(LGBMDataset train, LGBMDataset test) throws LGBMException, IOException {
34 | LGBMBooster booster = LGBMBooster.create(train, "objective=lambdarank metric=ndcg lambdarank_truncation_level=10 max_depth=5 learning_rate=0.1 num_leaves=8");
35 | booster.addValidData(test);
36 | for (int i=0; i<100; i++) {
37 | booster.updateOneIter();
38 | double[] eval1 = booster.getEval(0);
39 | double[] eval2 = booster.getEval(1);
40 | System.out.println("train " + eval1[0] + " test " + eval2[0]);
41 | assertTrue(eval1[0] > 0.5);
42 | }
43 | String[] names = booster.getFeatureNames();
44 | double[] weights = booster.featureImportance(0, LGBMBooster.FeatureImportanceType.GAIN);
45 | assertTrue(names.length > 0);
46 | assertTrue(weights.length > 0);
47 | booster.close();
48 | }
49 |
50 |
51 | private static LGBMDataset datasetFromResource(String file, LGBMDataset parent, boolean withPosition) throws LGBMException, IOException {
52 | BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(RankingIntegrationTest.class.getResourceAsStream(file))));
53 | ArrayList lines = reader.lines().map(line -> {
54 | int commentIndex = line.indexOf('#');
55 | if (commentIndex >= 0) {
56 | return line.substring(0, commentIndex);
57 | } else {
58 | return line;
59 | }
60 | }).collect(Collectors.toCollection(ArrayList::new));
61 | int maxFeatureId = 0; // features are 1-indexed!
62 | Set queriesSet = new HashSet<>();
63 | for (String line: lines) {
64 | String[] tokens = line.split(" ");
65 | int group = Integer.parseInt(tokens[1].split(":")[1]);
66 | queriesSet.add(group);
67 | for (int i = 2; i < tokens.length; i++) {
68 | String[] parts = tokens[i].split(":");
69 | int featureId = Integer.parseInt(parts[0]);
70 | if (featureId > maxFeatureId) {
71 | maxFeatureId = featureId;
72 | }
73 | }
74 | }
75 |
76 | int rows = lines.size();
77 | int queries = queriesSet.size();
78 | double[] features = new double[maxFeatureId * rows];
79 | float[] labels = new float[rows];
80 | int[] groups = new int[queries];
81 | int[] positions = new int[rows];
82 | String[] featureNames = new String[maxFeatureId];
83 | for (int i=1; i <= maxFeatureId; i++) {
84 | featureNames[i-1] = "f"+i;
85 | }
86 | int lastGroup = Integer.MIN_VALUE;
87 | int lastCount = 0;
88 | int groupIndex = 0;
89 | int position = 0;
90 | for (int row = 0; row < rows; row++) {
91 | String line = lines.get(row);
92 | String[] tokens = line.split(" ");
93 | float label = Float.parseFloat(tokens[0]);
94 | labels[row] = label;
95 | positions[row] = position;
96 | int group = Integer.parseInt(tokens[1].split(":")[1]);
97 | if (group != lastGroup) {
98 | // next query
99 | if (lastCount > 0) {
100 | // so it's not the first one
101 | groups[groupIndex] = lastCount;
102 | groupIndex++;
103 | }
104 | lastGroup = group;
105 | lastCount = 1;
106 | position = 0;
107 | } else {
108 | lastCount++;
109 | }
110 |
111 | for (int i=2; i < tokens.length; i++) {
112 | String[] feature = tokens[i].split(":");
113 | int id = Integer.parseInt(feature[0]) - 1;
114 | double value = Double.parseDouble(feature[1]);
115 | features[row * maxFeatureId + id] = value;
116 | }
117 | position++;
118 | }
119 | groups[groupIndex] = lastCount;
120 |
121 |
122 | reader.close();
123 | LGBMDataset dataset = LGBMDataset.createFromMat(features, rows, maxFeatureId, true, "", parent);
124 | dataset.setFeatureNames(featureNames);
125 | dataset.setField("label", labels);
126 | dataset.setField("group", groups);
127 | if (withPosition) dataset.setField("position", positions);
128 | return dataset;
129 | }
130 |
131 | }
132 |
--------------------------------------------------------------------------------
/src/test/resources/cancer.csv:
--------------------------------------------------------------------------------
1 | Age,BMI,Glucose,Insulin,HOMA,Leptin,Adiponectin,Resistin,MCP.1,Classification
2 | 48,23.5,70,2.707,0.467408667,8.8071,9.7024,7.99585,417.114,0
3 | 83,20.69049454,92,3.115,0.706897333,8.8438,5.429285,4.06405,468.786,0
4 | 82,23.12467037,91,4.498,1.009651067,17.9393,22.43204,9.27715,554.697,0
5 | 68,21.36752137,77,3.226,0.612724933,9.8827,7.16956,12.766,928.22,0
6 | 86,21.11111111,92,3.549,0.8053864,6.6994,4.81924,10.57635,773.92,0
7 | 49,22.85445769,92,3.226,0.732086933,6.8317,13.67975,10.3176,530.41,0
8 | 89,22.7,77,4.69,0.890787333,6.964,5.589865,12.9361,1256.083,0
9 | 76,23.8,118,6.47,1.883201333,4.311,13.25132,5.1042,280.694,0
10 | 73,22,97,3.35,0.801543333,4.47,10.358725,6.28445,136.855,0
11 | 75,23,83,4.952,1.013839467,17.127,11.57899,7.0913,318.302,0
12 | 34,21.47,78,3.469,0.6674356,14.57,13.11,6.92,354.6,0
13 | 29,23.01,82,5.663,1.145436133,35.59,26.72,4.58,174.8,0
14 | 25,22.86,82,4.09,0.827270667,20.45,23.67,5.14,313.73,0
15 | 24,18.67,88,6.107,1.33,8.88,36.06,6.85,632.22,0
16 | 38,23.34,75,5.782,1.06967,15.26,17.95,9.35,165.02,0
17 | 44,20.76,86,7.553,1.6,14.09,20.32,7.64,63.61,0
18 | 47,22.03,84,2.869,0.59,26.65,38.04,3.32,191.72,0
19 | 61,32.03895937,85,18.077,3.790144333,30.7729,7.780255,13.68392,444.395,0
20 | 64,34.5297228,95,4.427,1.037393667,21.2117,5.46262,6.70188,252.449,0
21 | 32,36.51263743,87,14.026,3.0099796,49.3727,5.1,17.10223,588.46,0
22 | 36,28.57667585,86,4.345,0.921719333,15.1248,8.6,9.1539,534.224,0
23 | 34,31.97501487,87,4.53,0.972138,28.7502,7.64276,5.62592,572.783,0
24 | 29,32.27078777,84,5.81,1.203832,45.6196,6.209635,24.6033,904.981,0
25 | 35,30.27681661,84,4.376,0.9067072,39.2134,9.048185,16.43706,733.797,0
26 | 54,30.48315806,90,5.537,1.229214,12.331,9.73138,10.19299,1227.91,0
27 | 45,37.03560819,83,6.76,1.383997333,39.9802,4.617125,8.70448,586.173,0
28 | 50,38.57875854,106,6.703,1.752611067,46.6401,4.667645,11.78388,887.16,0
29 | 66,31.44654088,90,9.245,2.05239,45.9624,10.35526,23.3819,1102.11,0
30 | 35,35.2507611,90,6.817,1.513374,50.6094,6.966895,22.03703,667.928,0
31 | 36,34.17489,80,6.59,1.300426667,10.2809,5.065915,15.72187,581.313,0
32 | 66,36.21227888,101,15.533,3.869788067,74.7069,7.53955,22.32024,864.968,0
33 | 53,36.7901662,101,10.175,2.534931667,27.1841,20.03,10.26309,695.754,0
34 | 28,35.85581466,87,8.576,1.8404096,68.5102,4.7942,21.44366,358.624,0
35 | 43,34.42217362,89,23.194,5.091856133,31.2128,8.300955,6.71026,960.246,0
36 | 51,27.68877813,77,3.855,0.732193,20.092,3.19209,10.37518,473.859,0
37 | 67,29.60676726,79,5.819,1.133929133,21.9033,2.19428,4.2075,585.307,0
38 | 66,31.2385898,82,4.181,0.845676933,16.2247,4.267105,3.29175,634.602,0
39 | 69,35.09270153,101,5.646,1.4066068,83.4821,6.796985,82.1,263.499,0
40 | 60,26.34929208,103,5.138,1.305394533,24.2998,2.19428,20.2535,378.996,0
41 | 77,35.58792924,76,3.881,0.727558133,21.7863,8.12555,17.2615,618.272,0
42 | 76,29.2184076,83,5.376,1.1006464,28.562,7.36996,8.04375,698.789,0
43 | 76,27.2,94,14.07,3.262364,35.891,9.34663,8.4156,377.227,0
44 | 75,27.3,85,5.197,1.089637667,10.39,9.000805,7.5767,335.393,0
45 | 69,32.5,93,5.43,1.245642,15.145,11.78796,11.78796,270.142,0
46 | 71,30.3,102,8.34,2.098344,56.502,8.13,4.2989,200.976,0
47 | 66,27.7,90,6.042,1.341324,24.846,7.652055,6.7052,225.88,0
48 | 75,25.7,94,8.079,1.8732508,65.926,3.74122,4.49685,206.802,0
49 | 78,25.3,60,3.508,0.519184,6.633,10.567295,4.6638,209.749,0
50 | 69,29.4,89,10.704,2.3498848,45.272,8.2863,4.53,215.769,0
51 | 85,26.6,96,4.462,1.0566016,7.85,7.9317,9.6135,232.006,0
52 | 76,27.1,110,26.211,7.111918,21.778,4.935635,8.49395,45.843,0
53 | 77,25.9,85,4.58,0.960273333,13.74,9.75326,11.774,488.829,0
54 | 45,21.30394858,102,13.852,3.4851632,7.6476,21.056625,23.03408,552.444,1
55 | 45,20.82999519,74,4.56,0.832352,7.7529,8.237405,28.0323,382.955,1
56 | 49,20.9566075,94,12.305,2.853119333,11.2406,8.412175,23.1177,573.63,1
57 | 34,24.24242424,92,21.699,4.9242264,16.7353,21.823745,12.06534,481.949,1
58 | 42,21.35991456,93,2.999,0.6879706,19.0826,8.462915,17.37615,321.919,1
59 | 68,21.08281329,102,6.2,1.55992,9.6994,8.574655,13.74244,448.799,1
60 | 51,19.13265306,93,4.364,1.0011016,11.0816,5.80762,5.57055,90.6,1
61 | 62,22.65625,92,3.482,0.790181867,9.8648,11.236235,10.69548,703.973,1
62 | 38,22.4996371,95,5.261,1.232827667,8.438,4.77192,15.73606,199.055,1
63 | 69,21.51385851,112,6.683,1.846290133,32.58,4.138025,15.69876,713.239,1
64 | 49,21.36752137,78,2.64,0.507936,6.3339,3.886145,22.94254,737.672,1
65 | 51,22.89281998,103,2.74,0.696142667,8.0163,9.349775,11.55492,359.232,1
66 | 59,22.83287935,98,6.862,1.658774133,14.9037,4.230105,8.2049,355.31,1
67 | 45,23.14049587,116,4.902,1.4026256,17.9973,4.294705,5.2633,518.586,1
68 | 54,24.21875,86,3.73,0.791257333,8.6874,3.70523,10.34455,635.049,1
69 | 64,22.22222222,98,5.7,1.37788,12.1905,4.783985,13.91245,395.976,1
70 | 46,20.83,88,3.42,0.742368,12.87,18.55,13.56,301.21,1
71 | 44,19.56,114,15.89,4.468268,13.08,20.37,4.62,220.66,1
72 | 45,20.26,92,3.44,0.780650667,7.65,16.67,7.84,193.87,1
73 | 44,24.74,106,58.46,15.28534133,18.16,16.1,5.31,244.75,1
74 | 51,18.37,105,6.03,1.56177,9.62,12.76,3.21,513.66,1
75 | 72,23.62,105,4.42,1.14478,21.78,17.86,4.82,195.94,1
76 | 46,22.21,86,36.94,7.836205333,10.16,9.76,5.68,312,1
77 | 43,26.5625,101,10.555,2.629602333,9.8,6.420295,16.1,806.724,1
78 | 55,31.97501487,92,16.635,3.775036,37.2234,11.018455,7.16514,483.377,1
79 | 43,31.25,103,4.328,1.099600533,25.7816,12.71896,38.6531,775.322,1
80 | 86,26.66666667,201,41.611,20.6307338,47.647,5.357135,24.3701,1698.44,1
81 | 41,26.6727633,97,22.033,5.271762467,44.7059,13.494865,27.8325,783.796,1
82 | 59,28.67262608,77,3.188,0.605507467,17.022,16.44048,31.6904,910.489,1
83 | 81,31.64036818,100,9.669,2.38502,38.8066,10.636525,29.5583,426.175,1
84 | 48,32.46191136,99,28.677,7.0029234,46.076,21.57,10.15726,738.034,1
85 | 71,25.51020408,112,10.395,2.871792,19.0653,5.4861,42.7447,799.898,1
86 | 42,29.296875,98,4.172,1.008511467,12.2617,6.695585,53.6717,1041.843,1
87 | 65,29.666548,85,14.649,3.071407,26.5166,7.28287,19.46324,1698.44,1
88 | 48,28.125,90,2.54,0.56388,15.5325,10.22231,16.11032,1698.44,1
89 | 85,27.68877813,196,51.814,25.05034187,70.8824,7.901685,55.2153,1078.359,1
90 | 48,31.25,199,12.162,5.9699204,18.1314,4.104105,53.6308,1698.44,1
91 | 58,29.15451895,139,16.582,5.685415067,22.8884,10.26266,13.97399,923.886,1
92 | 40,30.83653053,128,41.894,13.22733227,31.0385,6.160995,17.55503,638.261,1
93 | 82,31.21748179,100,18.077,4.458993333,31.6453,9.92365,19.94687,994.316,1
94 | 52,30.8012487,87,30.212,6.4834952,29.2739,6.26854,24.24591,764.667,1
95 | 49,32.46191136,134,24.887,8.225983067,42.3914,10.79394,5.768,656.393,1
96 | 60,31.23140988,131,30.13,9.736007333,37.843,8.40443,11.50005,396.021,1
97 | 49,29.77777778,70,8.396,1.449709333,51.3387,10.73174,20.76801,602.486,1
98 | 44,27.88761707,99,9.208,2.2485936,12.6757,5.47817,23.03306,407.206,1
99 | 40,27.63605442,103,2.432,0.617890133,14.3224,6.78387,26.0136,293.123,1
100 | 71,27.91551882,104,18.2,4.668906667,53.4997,1.65602,49.24184,256.001,1
101 | 69,28.44444444,108,8.808,2.3464512,14.7485,5.288025,16.48508,353.568,1
102 | 74,28.65013774,88,3.012,0.6538048,31.1233,7.65222,18.35574,572.401,1
103 | 66,26.5625,89,6.524,1.432235467,14.9084,8.42996,14.91922,269.487,1
104 | 65,30.91557669,97,10.491,2.5101466,44.0217,3.71009,20.4685,396.648,1
105 | 72,29.13631634,83,10.949,2.241625267,26.8081,2.78491,14.76966,232.018,1
106 | 57,34.83814777,95,12.548,2.940414667,33.1612,2.36495,9.9542,655.834,1
107 | 73,37.109375,134,5.636,1.862885867,41.4064,3.335665,6.89235,788.902,1
108 | 45,29.38475666,90,4.713,1.046286,23.8479,6.644245,15.55625,621.273,1
109 | 46,33.18,92,5.75,1.304866667,18.69,9.16,8.89,209.19,1
110 | 68,35.56,131,8.15,2.633536667,17.87,11.9,4.19,198.4,1
111 | 75,30.48,152,7.01,2.628282667,50.53,10.06,11.73,99.45,1
112 | 54,36.05,119,11.91,3.495982,89.27,8.01,5.06,218.28,1
113 | 45,26.85,92,3.33,0.755688,54.68,12.1,10.96,268.23,1
114 | 62,26.84,100,4.53,1.1174,12.45,21.42,7.32,330.16,1
115 | 65,32.05,97,5.73,1.370998,61.48,22.54,10.33,314.05,1
116 | 72,25.59,82,2.82,0.570392,24.96,33.75,3.27,392.46,1
117 | 86,27.18,138,19.91,6.777364,90.28,14.11,4.35,90.09,1
118 |
--------------------------------------------------------------------------------
/src/test/resources/categorical.data:
--------------------------------------------------------------------------------
1 | version https://git-lfs.github.com/spec/v1
2 | oid sha256:8913061988c7ffe8acaa7744012b5536fe1f0e515f305080fd96d8c0ee40efd7
3 | size 298658
4 |
--------------------------------------------------------------------------------
/src/test/resources/mq2008/test.txt.gz:
--------------------------------------------------------------------------------
1 | version https://git-lfs.github.com/spec/v1
2 | oid sha256:d12b6a9ba0577460540a6c27630866b880d30f0b33431e27cf21d5b6efc80f1e
3 | size 345457
4 |
--------------------------------------------------------------------------------
/src/test/resources/mq2008/train.txt.gz:
--------------------------------------------------------------------------------
1 | version https://git-lfs.github.com/spec/v1
2 | oid sha256:6f5707c2cb822750c16be00edf807c0eb41776020a0c0eb8b4f36fdead63dcd7
3 | size 1126873
4 |
--------------------------------------------------------------------------------
/swig-build/Dockerfile.amd64:
--------------------------------------------------------------------------------
1 | FROM ubuntu:focal-20240427
2 | RUN apt-get update
3 | ARG DEBIAN_FRONTEND=noninteractive
4 | RUN apt-get install -y --no-install-recommends openjdk-11-jdk gcc g++ build-essential git software-properties-common lsb-release dirmngr swig4.0 gpg-agent ca-certificates
5 | RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1A127079A92F09ED
6 | RUN apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
7 | RUN apt update && apt install -y kitware-archive-keyring
8 | RUN apt update && apt install -y cmake
9 | RUN git clone --recursive --depth 1 --branch v4.4.0 https://github.com/microsoft/LightGBM
10 | WORKDIR /LightGBM
11 | ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
12 | RUN cmake -B build -S . -DUSE_SWIG=ON
13 | RUN cmake --build build -j6
--------------------------------------------------------------------------------
/swig-build/Dockerfile.arm64:
--------------------------------------------------------------------------------
1 | FROM ubuntu:focal-20240427
2 | RUN apt-get update
3 | ARG DEBIAN_FRONTEND=noninteractive
4 | RUN apt-get install -y --no-install-recommends openjdk-11-jdk gcc g++ build-essential git software-properties-common lsb-release dirmngr swig4.0 gpg-agent ca-certificates
5 | RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1A127079A92F09ED
6 | RUN apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
7 | RUN apt update && apt install -y kitware-archive-keyring
8 | RUN apt update && apt install -y cmake
9 | RUN git clone --recursive --depth 1 --branch v4.4.0 https://github.com/microsoft/LightGBM
10 | WORKDIR /LightGBM
11 | ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64/
12 | RUN cmake -B build -S . -DUSE_SWIG=ON
13 | RUN cmake --build build -j6
--------------------------------------------------------------------------------
/swig-build/build_linux_amd64.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | docker buildx build --platform amd64 -t lgbm:latest -f "Dockerfile.amd64" .
--------------------------------------------------------------------------------
/swig-build/build_linux_arm64.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
3 | docker buildx build --platform arm64 -t lgbm:latest -f "Dockerfile.arm64" .
--------------------------------------------------------------------------------
/swig-build/build_macos.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | # brew install
4 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
5 | (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/m1/.zprofile
6 | eval "$(/opt/homebrew/bin/brew shellenv)"
7 |
8 | # install deps
9 | brew install libomp git openjdk@11 cmake swig htop mc
10 |
11 | sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
12 | echo 'export PATH="/opt/homebrew/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc
13 |
14 | export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"
15 | export CPPFLAGS="-I/opt/homebrew/opt/openjdk@11/include -I/opt/homebrew/opt/libomp/include"
16 | export JAVA_HOME=/opt/homebrew/Cellar/openjdk@11/11.0.23/libexec/openjdk.jdk/Contents/Home
17 | export CC=gcc CXX=g++
18 |
19 | # checkout
20 |
21 | git clone --recursive --depth 1 --branch v4.4.0 https://github.com/microsoft/LightGBM
22 | cd LightGBM
23 | cmake -B build -S . -DUSE_SWIG=ON
24 | cmake --build build -j6
25 |
--------------------------------------------------------------------------------