Class LGBMBooster
java.lang.Object
io.github.metarank.lightgbm4j.LGBMBooster
- All Implemented Interfaces:
AutoCloseable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enum -
Method Summary
Modifier and TypeMethodDescriptionvoidaddValidData(LGBMDataset dataset) Add new validation data to booster.voidclose()Deallocate all native memory for the LightGBM model.static LGBMBoostercreate(LGBMDataset dataset, String parameters) Create a new boosting learner.static LGBMBoostercreateFromModelfile(String file) Load an existing booster from model file.double[]featureImportance(int numIteration, LGBMBooster.FeatureImportanceType importanceType) Get model feature importance.double[]getEval(int dataIndex) Get evaluation for training data and validation data.String[]Get names of evaluation datasets.String[]Get names of features.intGet number of classes.intGet number of features.longgetNumPredict(int dataIdx) Get number of predictions for training data and validation data (this can be used to support customized evaluation functions).double[]getPredict(int dataIdx) Get prediction for training data and validation data.static booleanCalled from tests.static LGBMBoosterloadModelFromString(String model) Load an existing booster from string.static voidLoads all corresponsing native libraries for current platform.double[]predictForMat(double[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType) double[]predictForMat(double[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType, String parameter) Make prediction for a new double[] dataset.double[]predictForMat(float[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType) double[]predictForMat(float[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType, String parameter) Make prediction for a new float[] dataset.doublepredictForMatSingleRow(double[] data, PredictionType predictionType) Make prediction for a new double[] row dataset.doublepredictForMatSingleRow(float[] data, PredictionType predictionType) Make prediction for a new float[] row dataset.doublepredictForMatSingleRowFast(LGBMBooster.FastConfig config, double[] data, PredictionType predictionType) doublepredictForMatSingleRowFast(LGBMBooster.FastConfig config, float[] data, PredictionType predictionType) predictForMatSingleRowFastInit(PredictionType predictionType, int dtype, int ncols, String parameter) saveModelToString(int startIteration, int numIteration, LGBMBooster.FeatureImportanceType featureImportance) Save model to string.booleanUpdate the model for one iteration.booleanupdateOneIterCustom(float[] grad, float[] hess) Update the model by specifying gradient and Hessian directly (this can be used to support customized loss functions).
-
Method Details
-
isNativeLoaded
public static boolean isNativeLoaded()Called from tests.- Returns:
- true if JNI libraries were loaded successfully.
-
loadNative
Loads all corresponsing native libraries for current platform. Called from the class initializer, so usually there is no need to call it directly.- Throws:
IOException
-
createFromModelfile
Load an existing booster from model file.- Parameters:
file- Filename of model- Returns:
- Booster instance.
- Throws:
LGBMException
-
loadModelFromString
Load an existing booster from string.- Parameters:
model- Model string- Returns:
- Booster instance.
- Throws:
LGBMException
-
close
Deallocate all native memory for the LightGBM model.- Specified by:
closein interfaceAutoCloseable- Throws:
LGBMException
-
predictForMat
public double[] predictForMat(float[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType, String parameter) throws LGBMException Make prediction for a new float[] dataset.- Parameters:
input- input matrix, as a 1D array. Size should be rows * cols.rows- number of rowscols- number of colsisRowMajor- is the 1d encoding a row-major?predictionType- the prediction typeparameter- prediction options- Returns:
- array of predictions
- Throws:
LGBMException
-
predictForMat
public double[] predictForMat(float[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType) throws LGBMException - Throws:
LGBMException
-
predictForMat
public double[] predictForMat(double[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType, String parameter) throws LGBMException Make prediction for a new double[] dataset.- Parameters:
input- input matrix, as a 1D array. Size should be rows * cols.rows- number of rowscols- number of colsisRowMajor- is the 1 d encoding a row-major?predictionType- the prediction typeparameter- prediction options- Returns:
- array of predictions
- Throws:
LGBMException
-
predictForMat
public double[] predictForMat(double[] input, int rows, int cols, boolean isRowMajor, PredictionType predictionType) throws LGBMException - Throws:
LGBMException
-
create
Create a new boosting learner.- Parameters:
dataset- a LGBMDataset with the training data.parameters- Parameters in format ‘key1=value1 key2=value2’- Returns:
- Throws:
LGBMException
-
updateOneIter
Update the model for one iteration.- Returns:
- true if there are no more splits possible, so training is finished.
- Throws:
LGBMException
-
saveModelToString
public String saveModelToString(int startIteration, int numIteration, LGBMBooster.FeatureImportanceType featureImportance) throws LGBMException Save model to string.- Parameters:
startIteration- Start index of the iteration that should be savednumIteration- Index of the iteration that should be saved, 0 and negative means save allfeatureImportance- Type of feature importance, can be FeatureImportanceType.SPLIT or FeatureImportanceType.GAIN- Returns:
- Throws:
LGBMException
-
getFeatureNames
Get names of features.- Returns:
- a list of feature names.
- Throws:
LGBMException
-
addValidData
Add new validation data to booster.- Parameters:
dataset- dataset to validate- Throws:
LGBMException
-
getEval
Get evaluation for training data and validation data.- Parameters:
dataIndex- Index of data, 0: training data, 1: 1st validation data, 2: 2nd validation data and so on- Returns:
- Throws:
LGBMException
-
getEvalNames
Get names of evaluation datasets.- Returns:
- array of eval dataset names.
- Throws:
LGBMException
-
featureImportance
public double[] featureImportance(int numIteration, LGBMBooster.FeatureImportanceType importanceType) throws LGBMException Get model feature importance.- Parameters:
numIteration- Number of iterations for which feature importance is calculated, 0 or less means use allimportanceType- GAIN or SPLIT- Returns:
- Result array with feature importance
- Throws:
LGBMException
-
getNumFeature
Get number of features.- Returns:
- number of features
- Throws:
LGBMException
-
predictForMatSingleRow
public double predictForMatSingleRow(double[] data, PredictionType predictionType) throws LGBMException Make prediction for a new double[] row dataset. This method re-uses the internal predictor structure from previous calls and is optimized for single row invocation.- Parameters:
data- input vectorpredictionType- the prediction type- Returns:
- score
- Throws:
LGBMException
-
predictForMatSingleRow
public double predictForMatSingleRow(float[] data, PredictionType predictionType) throws LGBMException Make prediction for a new float[] row dataset. This method re-uses the internal predictor structure from previous calls and is optimized for single row invocation.- Parameters:
data- input vectorpredictionType- the prediction type- Returns:
- score
- Throws:
LGBMException
-
predictForMatSingleRowFastInit
public LGBMBooster.FastConfig predictForMatSingleRowFastInit(PredictionType predictionType, int dtype, int ncols, String parameter) throws LGBMException - Throws:
LGBMException
-
predictForMatSingleRowFast
public double predictForMatSingleRowFast(LGBMBooster.FastConfig config, float[] data, PredictionType predictionType) throws LGBMException - Throws:
LGBMException
-
predictForMatSingleRowFast
public double predictForMatSingleRowFast(LGBMBooster.FastConfig config, double[] data, PredictionType predictionType) throws LGBMException - Throws:
LGBMException
-
getNumClasses
Get number of classes.- Returns:
- Number of classes
- Throws:
LGBMException
-
getNumPredict
Get number of predictions for training data and validation data (this can be used to support customized evaluation functions).- Parameters:
dataIdx- Index of data, 0: training data, 1: 1st validation data, 2: 2nd validation data and so on- Returns:
- Number of predictions
- Throws:
LGBMException
-
getPredict
Get prediction for training data and validation data.- Parameters:
dataIdx- Index of data, 0: training data, 1: 1st validation data, 2: 2nd validation data and so on- Returns:
- array with predictions, of size num_class * dataset.num_data
- Throws:
LGBMException
-
updateOneIterCustom
Update the model by specifying gradient and Hessian directly (this can be used to support customized loss functions). The length of the arrays referenced by grad and hess must be equal to num_class * num_train_data, this is not verified by the library, the caller must ensure this.- Parameters:
grad- The first order derivative (gradient) statisticshess- The second order derivative (Hessian) statistics- Returns:
- true means the update was successfully finished (cannot split anymore), false indicates failure
- Throws:
LGBMException
-