public class Booster extends Object implements Serializable, com.esotericsoftware.kryo.KryoSerializable
| Modifier and Type | Class and Description |
|---|---|
static class |
Booster.FeatureImportanceType
Supported feature importance types
WEIGHT = Number of nodes that a feature was used to determine a split
GAIN = Average information gain per split for a feature
COVER = Average cover per split for a feature
TOTAL_GAIN = Total information gain over all splits of a feature
TOTAL_COVER = Total cover over all splits of a feature
|
static class |
Booster.PredictionType
This enumeration defines the type of prediction to be made and is used for inplace predictions.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_FORMAT |
| Modifier and Type | Method and Description |
|---|---|
void |
boost(DMatrix dtrain,
float[] grad,
float[] hess)
Deprecated.
|
void |
boost(DMatrix dtrain,
int iter,
float[] grad,
float[] hess)
Update with give grad and hess
|
void |
dispose() |
String |
evalSet(DMatrix[] evalMatrixs,
String[] evalNames,
IEvaluation eval)
Evaluate the Booster model given customized Evaluation class
|
String |
evalSet(DMatrix[] evalMatrixs,
String[] evalNames,
IEvaluation eval,
float[] metricsOut) |
String |
evalSet(DMatrix[] evalMatrixs,
String[] evalNames,
int iter)
Evaluate the Booster model with given dmatrixs.
|
String |
evalSet(DMatrix[] evalMatrixs,
String[] evalNames,
int iter,
float[] metricsOut)
Evaluate the Booster model with given dmatrixs.
|
protected void |
finalize() |
String |
getAttr(String key)
Get attribute value from the Booster based on the key provided.
|
Map<String,String> |
getAttrs()
Get attributes stored in the Booster as a Map.
|
String[] |
getFeatureNames()
Get all the feature names from the Booster.
|
Map<String,Integer> |
getFeatureScore(String featureMap)
Get importance of each feature
|
Map<String,Integer> |
getFeatureScore(String[] featureNames)
Get importance of each feature with specified feature names.
|
String[] |
getFeatureTypes()
Get feature types from the Booster.
|
String[] |
getModelDump(String[] featureNames,
boolean withStats)
Get the dump of the model as a string array with specified feature names.
|
String[] |
getModelDump(String[] featureNames,
boolean withStats,
String format)
Get the dump of the model as a string array with specified feature map, stats,
and the specified format.
|
String[] |
getModelDump(String featureMap,
boolean withStats)
Get the dump of the model as a string array
|
String[] |
getModelDump(String featureMap,
boolean withStats,
String format)
Get the dump of the model as a string array with specified feature map, stats,
and the specified format.
|
int |
getNumBoostedRound() |
long |
getNumFeature()
Get number of model features.
|
Map<String,Double> |
getScore(String[] featureNames,
String importanceType)
Get the feature importances for gain or cover (average or total)
|
Map<String,Double> |
getScore(String featureMap,
String importanceType)
Get the feature importances for gain or cover (average or total), with feature names
|
float[][] |
inplace_predict(float[] data,
int nrow,
int ncol,
float missing)
Perform thread-safe prediction.
|
float[][] |
inplace_predict(float[] data,
int nrow,
int ncol,
float missing,
int[] iteration_range)
Perform thread-safe prediction.
|
float[][] |
inplace_predict(float[] data,
int nrow,
int ncol,
float missing,
int[] iteration_range,
Booster.PredictionType predict_type,
float[] base_margin)
Perform thread-safe prediction.
|
float[][] |
predict(DMatrix data)
Make a prediction with test data in a DMatrix format.
|
float[][] |
predict(DMatrix data,
boolean outputMargin)
Make a prediction with test data in a DMatrix format and output margin.
|
float[][] |
predict(DMatrix data,
boolean outputMargin,
int treeLimit)
Advanced predict function with all the options.
|
float[][] |
predictContrib(DMatrix data,
int treeLimit)
Output feature contributions toward predictions of given data
|
float[][] |
predictLeaf(DMatrix data,
int treeLimit)
Predict leaf indices given the data
|
void |
read(com.esotericsoftware.kryo.Kryo kryo,
com.esotericsoftware.kryo.io.Input input) |
void |
saveModel(OutputStream out)
Save the model to file opened as output stream.
|
void |
saveModel(OutputStream out,
String format)
Save the model to file opened as output stream.
|
void |
saveModel(String modelPath)
Save model to modelPath
|
void |
setAttr(String key,
String value)
Set an attribute key-value pair to the Booster.
|
void |
setAttrs(Map<String,String> attrs)
Set multiple attribute key-value pairs to the Booster.
|
void |
setFeatureNames(String[] featureNames)
Set feature names to the Booster.
|
void |
setFeatureTypes(String[] featureTypes)
Set feature types to the Booster.
|
void |
setParam(String key,
Object value)
Set parameter to the Booster.
|
void |
setParams(Map<String,Object> params)
Set parameters to the Booster.
|
byte[] |
toByteArray()
Save model into raw byte array in the UBJSON ("ubj") format.
|
byte[] |
toByteArray(String format)
Save model into raw byte array.
|
void |
update(DMatrix dtrain,
int iter)
Update the booster for one iteration.
|
void |
update(DMatrix dtrain,
int iter,
IObjective obj)
Update with customize object functon
|
void |
update(DMatrix dtrain,
IObjective obj)
Deprecated.
|
void |
write(com.esotericsoftware.kryo.Kryo kryo,
com.esotericsoftware.kryo.io.Output output) |
public static final String DEFAULT_FORMAT
public final void setParam(String key, Object value) throws XGBoostError
key - param namevalue - param valueXGBoostError - native errorpublic void setParams(Map<String,Object> params) throws XGBoostError
params - parameters key-value mapXGBoostError - native errorpublic final Map<String,String> getAttrs() throws XGBoostError
XGBoostError - native errorpublic final String getAttr(String key) throws XGBoostError
key - attribute keyXGBoostError - native errorpublic final void setAttr(String key, String value) throws XGBoostError
key - attribute keyvalue - attribute valueXGBoostError - native errorpublic void setAttrs(Map<String,String> attrs) throws XGBoostError
attrs - attributes key-value mapXGBoostError - native errorpublic final String[] getFeatureNames() throws XGBoostError
XGBoostErrorpublic void setFeatureNames(String[] featureNames) throws XGBoostError
featureNames - An array of all the feature names.XGBoostErrorpublic final String[] getFeatureTypes() throws XGBoostError
XGBoostErrorpublic void setFeatureTypes(String[] featureTypes) throws XGBoostError
featureTypes - An array of all the feature types.XGBoostErrorpublic void update(DMatrix dtrain, int iter) throws XGBoostError
dtrain - training dataiter - current iteration numberXGBoostError - native error@Deprecated public void update(DMatrix dtrain, IObjective obj) throws XGBoostError
XGBoostErrorpublic void update(DMatrix dtrain, int iter, IObjective obj) throws XGBoostError
dtrain - training dataiter - The current training iteration.obj - customized objective classXGBoostError - native error@Deprecated public void boost(DMatrix dtrain, float[] grad, float[] hess) throws XGBoostError
XGBoostErrorpublic void boost(DMatrix dtrain, int iter, float[] grad, float[] hess) throws XGBoostError
dtrain - training dataiter - The current training iteration.grad - first order of gradienthess - seconde order of gradientXGBoostError - native errorpublic String evalSet(DMatrix[] evalMatrixs, String[] evalNames, int iter) throws XGBoostError
evalMatrixs - dmatrixs for evaluationevalNames - name for eval dmatrixs, used for check resultsiter - current eval iterationXGBoostError - native errorpublic String evalSet(DMatrix[] evalMatrixs, String[] evalNames, int iter, float[] metricsOut) throws XGBoostError
evalMatrixs - dmatrixs for evaluationevalNames - name for eval dmatrixs, used for check resultsiter - current eval iterationmetricsOut - output array containing the evaluation metrics for each evalMatrixXGBoostError - native errorpublic String evalSet(DMatrix[] evalMatrixs, String[] evalNames, IEvaluation eval) throws XGBoostError
evalMatrixs - evaluation matrixevalNames - evaluation nameseval - custom evaluatorXGBoostError - native errorpublic String evalSet(DMatrix[] evalMatrixs, String[] evalNames, IEvaluation eval, float[] metricsOut) throws XGBoostError
XGBoostErrorpublic float[][] inplace_predict(float[] data,
int nrow,
int ncol,
float missing)
throws XGBoostError
data - Flattened input matrix of features for predictionnrow - The number of preditions to make (count of input matrix rows)ncol - The number of features in the model (count of input matrix columns)missing - Value indicating missing element in the data input matrixXGBoostErrorpublic float[][] inplace_predict(float[] data,
int nrow,
int ncol,
float missing,
int[] iteration_range)
throws XGBoostError
data - Flattened input matrix of features for predictionnrow - The number of preditions to make (count of input matrix rows)ncol - The number of features in the model (count of input matrix columns)missing - Value indicating missing element in the data input matrixiteration_range - Specifies which layer of trees are used in prediction. For
example, if a random forest is trained with 100 rounds.
Specifying `iteration_range=[10, 20)`, then only the forests
built during [10, 20) (half open set) rounds are used in this
prediction.XGBoostErrorpublic float[][] inplace_predict(float[] data,
int nrow,
int ncol,
float missing,
int[] iteration_range,
Booster.PredictionType predict_type,
float[] base_margin)
throws XGBoostError
data - Flattened input matrix of features for predictionnrow - The number of preditions to make (count of input matrix rows)ncol - The number of features in the model (count of input matrix columns)missing - Value indicating missing element in the data input matrixiteration_range - Specifies which layer of trees are used in prediction. For
example, if a random forest is trained with 100 rounds.
Specifying `iteration_range=[10, 20)`, then only the forests
built during [10, 20) (half open set) rounds are used in this
prediction.predict_type - What kind of prediction to run.XGBoostErrorpublic float[][] predictLeaf(DMatrix data, int treeLimit) throws XGBoostError
data - The input data.treeLimit - Number of trees to include, 0 means all trees.XGBoostErrorpublic float[][] predictContrib(DMatrix data, int treeLimit) throws XGBoostError
data - The input data.treeLimit - Number of trees to include, 0 means all trees.XGBoostErrorpublic float[][] predict(DMatrix data) throws XGBoostError
data - dmatrix storing the test input on which predictions are to be madeXGBoostError - native errorpublic float[][] predict(DMatrix data, boolean outputMargin) throws XGBoostError
data - dmatrix storing the test input on which predictions are to be madeoutputMargin - output marginXGBoostErrorpublic float[][] predict(DMatrix data, boolean outputMargin, int treeLimit) throws XGBoostError
data - matrix storing the test input on which predictions are to be madeoutputMargin - output margintreeLimit - limit number of trees, 0 means all trees.XGBoostErrorpublic void saveModel(String modelPath) throws XGBoostError
modelPath - model pathXGBoostErrorpublic void saveModel(OutputStream out) throws XGBoostError, IOException
out - The output streamXGBoostErrorIOExceptionpublic void saveModel(OutputStream out, String format) throws XGBoostError, IOException
out - The output streamformat - The model format (ubj, json, deprecated)XGBoostErrorIOExceptionpublic String[] getModelDump(String featureMap, boolean withStats) throws XGBoostError
featureMap - A string containing the path to a feature map.withStats - Controls whether the split statistics are output.XGBoostError - native errorpublic String[] getModelDump(String featureMap, boolean withStats, String format) throws XGBoostError
featureMap - A string containing the path to a feature map.withStats - Controls whether the split statistics are output.format - The format in which the model is dumped (text, json, ubj).XGBoostErrorpublic String[] getModelDump(String[] featureNames, boolean withStats) throws XGBoostError
featureNames - Names of the features.XGBoostErrorpublic String[] getModelDump(String[] featureNames, boolean withStats, String format) throws XGBoostError
featureNames - An array of strings containing the feature names.withStats - Controls whether the split statistics are output.format - The format in which the model is dumped (text, json, ubj).XGBoostErrorpublic Map<String,Integer> getFeatureScore(String[] featureNames) throws XGBoostError
XGBoostError - native errorpublic Map<String,Integer> getFeatureScore(String featureMap) throws XGBoostError
XGBoostError - native errorpublic Map<String,Double> getScore(String[] featureNames, String importanceType) throws XGBoostError
XGBoostError - native errorpublic Map<String,Double> getScore(String featureMap, String importanceType) throws XGBoostError
XGBoostError - native errorpublic byte[] toByteArray()
throws XGBoostError
XGBoostError - native errorpublic byte[] toByteArray(String format) throws XGBoostError
format - The output format. Available options are "json", "ubj" and "deprecated".XGBoostError - native errorpublic long getNumFeature()
throws XGBoostError
XGBoostErrorpublic int getNumBoostedRound()
throws XGBoostError
XGBoostErrorprotected void finalize()
throws Throwable
public void dispose()
public void write(com.esotericsoftware.kryo.Kryo kryo,
com.esotericsoftware.kryo.io.Output output)
write in interface com.esotericsoftware.kryo.KryoSerializablepublic void read(com.esotericsoftware.kryo.Kryo kryo,
com.esotericsoftware.kryo.io.Input input)
read in interface com.esotericsoftware.kryo.KryoSerializableCopyright © 2026. All rights reserved.