Package ml.dmlc.xgboost4j.java
Class Booster
java.lang.Object
ml.dmlc.xgboost4j.java.Booster
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable,Serializable
public class Booster
extends Object
implements Serializable, com.esotericsoftware.kryo.KryoSerializable
Booster for xgboost, this is a model API that support interactive build of an XGBoost Model
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSupported 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 featurestatic enumThis enumeration defines the type of prediction to be made and is used for inplace predictions. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidDeprecated.voidUpdate with give grad and hessvoiddispose()Evaluate the Booster model with given dmatrixs.Evaluate the Booster model with given dmatrixs.evalSet(DMatrix[] evalMatrixs, String[] evalNames, IEvaluation eval) Evaluate the Booster model given customized Evaluation classevalSet(DMatrix[] evalMatrixs, String[] evalNames, IEvaluation eval, float[] metricsOut) protected voidfinalize()final StringGet attribute value from the Booster based on the key provided.getAttrs()Get attributes stored in the Booster as a Map.final String[]Get all the feature names from the Booster.getFeatureScore(String featureMap) Get importance of each featuregetFeatureScore(String[] featureNames) Get importance of each feature with specified feature names.final String[]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 arrayString[]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.intlongGet number of model features.Get the feature importances for gain or cover (average or total)Get the feature importances for gain or cover (average or total), with feature namesfloat[][]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[][]Make a prediction with test data in a DMatrix format.float[][]Make a prediction with test data in a DMatrix format and output margin.float[][]Advanced predict function with all the options.float[][]predictContrib(DMatrix data, int treeLimit) Output feature contributions toward predictions of given datafloat[][]predictLeaf(DMatrix data, int treeLimit) Predict leaf indices given the datavoidread(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) voidsaveModel(OutputStream out) Save the model to file opened as output stream.voidsaveModel(OutputStream out, String format) Save the model to file opened as output stream.voidSave model to modelPathfinal voidSet an attribute key-value pair to the Booster.voidSet multiple attribute key-value pairs to the Booster.voidsetFeatureNames(String[] featureNames) Set feature names to the Booster.voidsetFeatureTypes(String[] featureTypes) Set feature types to the Booster.final voidSet parameter to the Booster.voidSet parameters to the Booster.byte[]Save model into raw byte array in the UBJSON ("ubj") format.byte[]toByteArray(String format) Save model into raw byte array.voidUpdate the booster for one iteration.voidupdate(DMatrix dtrain, int iter, IObjective obj) Update with customize object functonvoidupdate(DMatrix dtrain, IObjective obj) Deprecated.voidwrite(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output)
-
Field Details
-
DEFAULT_FORMAT
- See Also:
-
-
Method Details
-
setParam
Set parameter to the Booster.- Parameters:
key- param namevalue- param value- Throws:
XGBoostError- native error
-
setParams
Set parameters to the Booster.- Parameters:
params- parameters key-value map- Throws:
XGBoostError- native error
-
getAttrs
Get attributes stored in the Booster as a Map.- Returns:
- A map contain attribute pairs.
- Throws:
XGBoostError- native error
-
getAttr
Get attribute value from the Booster based on the key provided.- Parameters:
key- attribute key- Returns:
- attribute value
- Throws:
XGBoostError- native error
-
setAttr
Set an attribute key-value pair to the Booster.- Parameters:
key- attribute keyvalue- attribute value- Throws:
XGBoostError- native error
-
setAttrs
Set multiple attribute key-value pairs to the Booster.- Parameters:
attrs- attributes key-value map- Throws:
XGBoostError- native error
-
getFeatureNames
Get all the feature names from the Booster.- Returns:
- An array of all the feature names.
- Throws:
XGBoostError
-
setFeatureNames
Set feature names to the Booster.- Parameters:
featureNames- An array of all the feature names.- Throws:
XGBoostError
-
getFeatureTypes
Get feature types from the Booster.- Returns:
- An array of all the feature types.
- Throws:
XGBoostError
-
setFeatureTypes
Set feature types to the Booster.- Parameters:
featureTypes- An array of all the feature types.- Throws:
XGBoostError
-
update
Update the booster for one iteration.- Parameters:
dtrain- training dataiter- current iteration number- Throws:
XGBoostError- native error
-
update
Deprecated.- Throws:
XGBoostError
-
update
Update with customize object functon- Parameters:
dtrain- training dataiter- The current training iteration.obj- customized objective class- Throws:
XGBoostError- native error
-
boost
Deprecated.- Throws:
XGBoostError
-
boost
Update with give grad and hess- Parameters:
dtrain- training dataiter- The current training iteration.grad- first order of gradienthess- seconde order of gradient- Throws:
XGBoostError- native error
-
evalSet
Evaluate the Booster model with given dmatrixs.- Parameters:
evalMatrixs- dmatrixs for evaluationevalNames- name for eval dmatrixs, used for check resultsiter- current eval iteration- Returns:
- eval Information containing the evaluation results
- Throws:
XGBoostError- native error
-
evalSet
public String evalSet(DMatrix[] evalMatrixs, String[] evalNames, int iter, float[] metricsOut) throws XGBoostError Evaluate the Booster model with given dmatrixs.- Parameters:
evalMatrixs- dmatrixs for evaluationevalNames- name for eval dmatrixs, used for check resultsiter- current eval iterationmetricsOut- output array containing the evaluation metrics for each evalMatrix- Returns:
- eval Information containing the evaluation results
- Throws:
XGBoostError- native error
-
evalSet
public String evalSet(DMatrix[] evalMatrixs, String[] evalNames, IEvaluation eval) throws XGBoostError Evaluate the Booster model given customized Evaluation class- Parameters:
evalMatrixs- evaluation matrixevalNames- evaluation nameseval- custom evaluator- Returns:
- eval Information containing the evaluation results
- Throws:
XGBoostError- native error
-
evalSet
public String evalSet(DMatrix[] evalMatrixs, String[] evalNames, IEvaluation eval, float[] metricsOut) throws XGBoostError - Throws:
XGBoostError
-
inplace_predict
public float[][] inplace_predict(float[] data, int nrow, int ncol, float missing) throws XGBoostError Perform thread-safe prediction.- Parameters:
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 thedatainput matrix- Returns:
- predict Result matrix
- Throws:
XGBoostError
-
inplace_predict
public float[][] inplace_predict(float[] data, int nrow, int ncol, float missing, int[] iteration_range) throws XGBoostError Perform thread-safe prediction.- Parameters:
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 thedatainput 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.- Returns:
- predict Result matrix
- Throws:
XGBoostError
-
inplace_predict
public float[][] inplace_predict(float[] data, int nrow, int ncol, float missing, int[] iteration_range, Booster.PredictionType predict_type, float[] base_margin) throws XGBoostError Perform thread-safe prediction.- Parameters:
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 thedatainput 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.- Returns:
- predict Result matrix
- Throws:
XGBoostError
-
predictLeaf
Predict leaf indices given the data- Parameters:
data- The input data.treeLimit- Number of trees to include, 0 means all trees.- Returns:
- The leaf indices of the instance.
- Throws:
XGBoostError
-
predictContrib
Output feature contributions toward predictions of given data- Parameters:
data- The input data.treeLimit- Number of trees to include, 0 means all trees.- Returns:
- The feature contributions and bias.
- Throws:
XGBoostError
-
predict
Make a prediction with test data in a DMatrix format.- Parameters:
data- dmatrix storing the test input on which predictions are to be made- Returns:
- predict The results of the prediction, where each row corresponds to a prediction.
- Throws:
XGBoostError- native error
-
predict
Make a prediction with test data in a DMatrix format and output margin.- Parameters:
data- dmatrix storing the test input on which predictions are to be madeoutputMargin- output margin- Returns:
- predict The results of the prediction, where each row corresponds to a prediction.
- Throws:
XGBoostError
-
predict
Advanced predict function with all the options.- Parameters:
data- matrix storing the test input on which predictions are to be madeoutputMargin- output margintreeLimit- limit number of trees, 0 means all trees.- Returns:
- predict The results of the prediction, where each row corresponds to a prediction.
- Throws:
XGBoostError
-
saveModel
Save model to modelPath- Parameters:
modelPath- model path- Throws:
XGBoostError
-
saveModel
Save the model to file opened as output stream. The model format is compatible with other xgboost bindings. The output stream can only save one xgboost model. This function will close the OutputStream after the save.- Parameters:
out- The output stream- Throws:
XGBoostErrorIOException
-
saveModel
Save the model to file opened as output stream. The model format is compatible with other xgboost bindings. The output stream can only save one xgboost model. This function will close the OutputStream after the save.- Parameters:
out- The output streamformat- The model format (ubj, json, deprecated)- Throws:
XGBoostErrorIOException
-
getModelDump
Get the dump of the model as a string array- Parameters:
featureMap- A string containing the path to a feature map.withStats- Controls whether the split statistics are output.- Returns:
- The dumped model information
- Throws:
XGBoostError- native error
-
getModelDump
public String[] getModelDump(String featureMap, boolean withStats, String format) throws XGBoostError Get the dump of the model as a string array with specified feature map, stats, and the specified format.- Parameters:
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).- Returns:
- The dumped model information
- Throws:
XGBoostError
-
getModelDump
Get the dump of the model as a string array with specified feature names.- Parameters:
featureNames- Names of the features.- Returns:
- dumped model information
- Throws:
XGBoostError
-
getModelDump
public String[] getModelDump(String[] featureNames, boolean withStats, String format) throws XGBoostError Get the dump of the model as a string array with specified feature map, stats, and the specified format.- Parameters:
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).- Returns:
- The dumped model information
- Throws:
XGBoostError
-
getFeatureScore
Get importance of each feature with specified feature names.- Returns:
- featureScoreMap key: feature name, value: feature importance score, can be nill.
- Throws:
XGBoostError- native error
-
getFeatureScore
Get importance of each feature- Returns:
- featureScoreMap key: feature index, value: feature importance score, can be nill
- Throws:
XGBoostError- native error
-
getScore
public Map<String,Double> getScore(String[] featureNames, String importanceType) throws XGBoostError Get the feature importances for gain or cover (average or total)- Returns:
- featureImportanceMap key: feature index, values: feature importance score based on gain or cover
- Throws:
XGBoostError- native error
-
getScore
Get the feature importances for gain or cover (average or total), with feature names- Returns:
- featureImportanceMap key: feature name, values: feature importance score based on gain or cover
- Throws:
XGBoostError- native error
-
toByteArray
Save model into raw byte array in the UBJSON ("ubj") format.- Returns:
- the saved byte array
- Throws:
XGBoostError- native error
-
toByteArray
Save model into raw byte array.- Parameters:
format- The output format. Available options are "json", "ubj" and "deprecated".- Returns:
- the saved byte array
- Throws:
XGBoostError- native error
-
getNumFeature
Get number of model features.- Returns:
- the number of features.
- Throws:
XGBoostError
-
getNumBoostedRound
- Throws:
XGBoostError
-
finalize
-
dispose
public void dispose() -
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) - Specified by:
writein interfacecom.esotericsoftware.kryo.KryoSerializable
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) - Specified by:
readin interfacecom.esotericsoftware.kryo.KryoSerializable
-