Treelite
Functions
Model_loader

Functions

int TreeliteLoadLightGBMModel (const char *filename, ModelHandle *out)
 load a model file generated by LightGBM (Microsoft/LightGBM). The model file must contain a decision tree ensemble. More...
 
int TreeliteLoadXGBoostModel (const char *filename, ModelHandle *out)
 load a model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tree ensemble. More...
 
int TreeliteLoadXGBoostJSON (const char *filename, ModelHandle *out)
 load a json model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tree ensemble. More...
 
int TreeliteLoadXGBoostJSONString (const char *json_str, size_t length, ModelHandle *out)
 load a model stored as JSON stringby XGBoost (dmlc/xgboost). The model json must contain a decision tree ensemble. More...
 
int TreeliteLoadXGBoostModelFromMemoryBuffer (const void *buf, size_t len, ModelHandle *out)
 load an XGBoost model from a memory buffer. More...
 
int TreeliteLoadLightGBMModelFromString (const char *model_str, ModelHandle *out)
 Load a LightGBM model from a string. The string should be created with the model_to_string() method in LightGBM. More...
 
int TreeliteLoadSKLearnRandomForestRegressor (int n_estimators, int n_features, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **weighted_n_node_samples, const double **impurity, ModelHandle *out)
 Load a scikit-learn random forest regressor model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. Note that this function can also be used to load an ensemble of extremely randomized trees (sklearn.ensemble.ExtraTreesRegressor). More...
 
int TreeliteLoadSKLearnIsolationForest (int n_estimators, int n_features, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **weighted_n_node_samples, const double **impurity, const double ratio_c, ModelHandle *out)
 Load a scikit-learn isolation forest model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. More...
 
int TreeliteLoadSKLearnRandomForestClassifier (int n_estimators, int n_features, int n_classes, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **weighted_n_node_samples, const double **impurity, ModelHandle *out)
 Load a scikit-learn random forest classifier model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. Note that this function can also be used to load an ensemble of extremely randomized trees (sklearn.ensemble.ExtraTreesClassifier). More...
 
int TreeliteLoadSKLearnGradientBoostingRegressor (int n_estimators, int n_features, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **weighted_n_node_samples, const double **impurity, ModelHandle *out)
 Load a scikit-learn gradient boosting regressor model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. More...
 
int TreeliteLoadSKLearnGradientBoostingClassifier (int n_estimators, int n_features, int n_classes, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **weighted_n_node_samples, const double **impurity, ModelHandle *out)
 Load a scikit-learn gradient boosting classifier model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. More...
 
int TreeliteQueryNumTree (ModelHandle handle, size_t *out)
 Query the number of trees in the model. More...
 
int TreeliteQueryNumFeature (ModelHandle handle, size_t *out)
 Query the number of features used in the model. More...
 
int TreeliteQueryNumClass (ModelHandle handle, size_t *out)
 Query the number of classes of the model. (1 if the model is binary classifier or regressor) More...
 
int TreeliteSetTreeLimit (ModelHandle handle, size_t limit)
 keep first N trees of model, limit must smaller than number of trees. More...
 
int TreeliteSerializeModel (const char *filename, ModelHandle handle)
 Serialize (persist) a model object to disk. More...
 
int TreeliteDeserializeModel (const char *filename, ModelHandle *out)
 Deserialize (load) a model object from disk. More...
 
int TreeliteConcatenateModelObjects (const ModelHandle *objs, size_t len, ModelHandle *out)
 Concatenate multiple model objects into a single model object by copying all member trees into the destination model object. More...
 
int TreeliteDumpAsJSON (ModelHandle handle, int pretty_print, const char **out_json_str)
 Dump a model object as a JSON string. More...
 
int TreeliteFreeModel (ModelHandle handle)
 delete model from memory More...
 

Detailed Description

Model loader interface: read trees from the disk

Function Documentation

◆ TreeliteConcatenateModelObjects()

int TreeliteConcatenateModelObjects ( const ModelHandle objs,
size_t  len,
ModelHandle out 
)

Concatenate multiple model objects into a single model object by copying all member trees into the destination model object.

Parameters
objsPointer to the beginning of the list of model objects
lenNumber of model objects
outUsed to save the concatenated model

Definition at line 243 of file c_api.cc.

◆ TreeliteDeserializeModel()

int TreeliteDeserializeModel ( const char *  filename,
ModelHandle out 
)

Deserialize (load) a model object from disk.

Parameters
filenamename of the file from which to deserialize the model. The file should be created by a call to TreeliteSerializeModel().
handlehandle to the model object
Returns
0 for success, -1 for failure

Definition at line 233 of file c_api.cc.

◆ TreeliteDumpAsJSON()

int TreeliteDumpAsJSON ( ModelHandle  handle,
int  pretty_print,
const char **  out_json_str 
)

Dump a model object as a JSON string.

Parameters
handleThe handle to the model object
pretty_printWhether to pretty-print JSON string (0 for false, != 0 for true)
out_json_strThe JSON string
Returns
0 for success, -1 for failure

Definition at line 254 of file c_api.cc.

◆ TreeliteFreeModel()

int TreeliteFreeModel ( ModelHandle  handle)

delete model from memory

Parameters
handlemodel to remove
Returns
0 for success, -1 for failure

Definition at line 263 of file c_api.cc.

◆ TreeliteLoadLightGBMModel()

int TreeliteLoadLightGBMModel ( const char *  filename,
ModelHandle out 
)

load a model file generated by LightGBM (Microsoft/LightGBM). The model file must contain a decision tree ensemble.

Parameters
filenamename of model file
outloaded model
Returns
0 for success, -1 for failure

Definition at line 116 of file c_api.cc.

◆ TreeliteLoadLightGBMModelFromString()

int TreeliteLoadLightGBMModelFromString ( const char *  model_str,
ModelHandle out 
)

Load a LightGBM model from a string. The string should be created with the model_to_string() method in LightGBM.

Parameters
model_strthe model string
outloaded model
Returns
0 for success, -1 for failure

Definition at line 151 of file c_api.cc.

◆ TreeliteLoadSKLearnGradientBoostingClassifier()

int TreeliteLoadSKLearnGradientBoostingClassifier ( int  n_estimators,
int  n_features,
int  n_classes,
const int64_t *  node_count,
const int64_t **  children_left,
const int64_t **  children_right,
const int64_t **  feature,
const double **  threshold,
const double **  value,
const int64_t **  n_node_samples,
const double **  weighted_n_node_samples,
const double **  impurity,
ModelHandle out 
)

Load a scikit-learn gradient boosting classifier model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail.

Parameters
n_estimatorsnumber of trees in the random forest
n_featuresnumber of features in the training data
n_classesnumber of classes in the target variable
node_countnode_count[i] stores the number of nodes in the i-th tree
children_leftchildren_left[i][k] stores the ID of the left child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
children_rightchildren_right[i][k] stores the ID of the right child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
featurefeature[i][k] stores the ID of the feature used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
thresholdthreshold[i][k] stores the threshold used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
valuevalue[i][k] stores the leaf output of node k of the i-th tree. This is only defined if node k is a leaf node.
n_node_samplesn_node_samples[i][k] stores the number of data samples associated with node k of the i-th tree.
weighted_n_node_samplesweighted_n_node_samples[i][k] stores the sum of weighted data samples associated with node k of the i-th tree.
impurityimpurity[i][k] stores the impurity measure (gini, entropy etc) associated with node k of the i-th tree.
outpointer to store the loaded model
Returns
0 for success, -1 for failure

Definition at line 210 of file c_api.cc.

◆ TreeliteLoadSKLearnGradientBoostingRegressor()

int TreeliteLoadSKLearnGradientBoostingRegressor ( int  n_estimators,
int  n_features,
const int64_t *  node_count,
const int64_t **  children_left,
const int64_t **  children_right,
const int64_t **  feature,
const double **  threshold,
const double **  value,
const int64_t **  n_node_samples,
const double **  weighted_n_node_samples,
const double **  impurity,
ModelHandle out 
)

Load a scikit-learn gradient boosting regressor model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail.

Parameters
n_estimatorsnumber of trees in the random forest
n_featuresnumber of features in the training data
node_countnode_count[i] stores the number of nodes in the i-th tree
children_leftchildren_left[i][k] stores the ID of the left child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
children_rightchildren_right[i][k] stores the ID of the right child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
featurefeature[i][k] stores the ID of the feature used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
thresholdthreshold[i][k] stores the threshold used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
valuevalue[i][k] stores the leaf output of node k of the i-th tree. This is only defined if node k is a leaf node.
n_node_samplesn_node_samples[i][k] stores the number of data samples associated with node k of the i-th tree.
weighted_n_node_samplesweighted_n_node_samples[i][k] stores the sum of weighted data samples associated with node k of the i-th tree.
impurityimpurity[i][k] stores the impurity measure (gini, entropy etc) associated with node k of the i-th tree.
outpointer to store the loaded model
Returns
0 for success, -1 for failure

Definition at line 197 of file c_api.cc.

◆ TreeliteLoadSKLearnIsolationForest()

int TreeliteLoadSKLearnIsolationForest ( int  n_estimators,
int  n_features,
const int64_t *  node_count,
const int64_t **  children_left,
const int64_t **  children_right,
const int64_t **  feature,
const double **  threshold,
const double **  value,
const int64_t **  n_node_samples,
const double **  weighted_n_node_samples,
const double **  impurity,
const double  ratio_c,
ModelHandle out 
)

Load a scikit-learn isolation forest model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail.

Parameters
n_estimatorsnumber of trees in the random forest
n_featuresnumber of features in the training data
node_countnode_count[i] stores the number of nodes in the i-th tree
children_leftchildren_left[i][k] stores the ID of the left child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
children_rightchildren_right[i][k] stores the ID of the right child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
featurefeature[i][k] stores the ID of the feature used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
thresholdthreshold[i][k] stores the threshold used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
valuevalue[i][k] stores the expected isolation depth of node k of the i-th tree. This is only defined if node k is a leaf node.
n_node_samplesn_node_samples[i][k] stores the number of data samples associated with node k of the i-th tree.
weighted_n_node_samplesweighted_n_node_samples[i][k] stores the sum of weighted data samples associated with node k of the i-th tree.
impuritynot used, but must be passed as array of arrays for each tree and node.
ratio_cstandardizing constant to use for calculation of the anomaly score.
outpointer to store the loaded model
Returns
0 for success, -1 for failure

Definition at line 171 of file c_api.cc.

◆ TreeliteLoadSKLearnRandomForestClassifier()

int TreeliteLoadSKLearnRandomForestClassifier ( int  n_estimators,
int  n_features,
int  n_classes,
const int64_t *  node_count,
const int64_t **  children_left,
const int64_t **  children_right,
const int64_t **  feature,
const double **  threshold,
const double **  value,
const int64_t **  n_node_samples,
const double **  weighted_n_node_samples,
const double **  impurity,
ModelHandle out 
)

Load a scikit-learn random forest classifier model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. Note that this function can also be used to load an ensemble of extremely randomized trees (sklearn.ensemble.ExtraTreesClassifier).

Parameters
n_estimatorsnumber of trees in the random forest
n_featuresnumber of features in the training data
n_classesnumber of classes in the target variable
node_countnode_count[i] stores the number of nodes in the i-th tree
children_leftchildren_left[i][k] stores the ID of the left child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
children_rightchildren_right[i][k] stores the ID of the right child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
featurefeature[i][k] stores the ID of the feature used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
thresholdthreshold[i][k] stores the threshold used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
valuevalue[i][k] stores the leaf output of node k of the i-th tree. This is only defined if node k is a leaf node.
n_node_samplesn_node_samples[i][k] stores the number of data samples associated with node k of the i-th tree.
weighted_n_node_samplesweighted_n_node_samples[i][k] stores the sum of weighted data samples associated with node k of the i-th tree.
impurityimpurity[i][k] stores the impurity measure (gini, entropy etc) associated with node k of the i-th tree.
outpointer to store the loaded model
Returns
0 for success, -1 for failure

Definition at line 184 of file c_api.cc.

◆ TreeliteLoadSKLearnRandomForestRegressor()

int TreeliteLoadSKLearnRandomForestRegressor ( int  n_estimators,
int  n_features,
const int64_t *  node_count,
const int64_t **  children_left,
const int64_t **  children_right,
const int64_t **  feature,
const double **  threshold,
const double **  value,
const int64_t **  n_node_samples,
const double **  weighted_n_node_samples,
const double **  impurity,
ModelHandle out 
)

Load a scikit-learn random forest regressor model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail. Note that this function can also be used to load an ensemble of extremely randomized trees (sklearn.ensemble.ExtraTreesRegressor).

Parameters
n_estimatorsnumber of trees in the random forest
n_featuresnumber of features in the training data
node_countnode_count[i] stores the number of nodes in the i-th tree
children_leftchildren_left[i][k] stores the ID of the left child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
children_rightchildren_right[i][k] stores the ID of the right child node of node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
featurefeature[i][k] stores the ID of the feature used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
thresholdthreshold[i][k] stores the threshold used in the binary tree split at node k of the i-th tree. This is only defined if node k is an internal (non-leaf) node.
valuevalue[i][k] stores the leaf output of node k of the i-th tree. This is only defined if node k is a leaf node.
n_node_samplesn_node_samples[i][k] stores the number of data samples associated with node k of the i-th tree.
weighted_n_node_samplesweighted_n_node_samples[i][k] stores the sum of weighted data samples associated with node k of the i-th tree.
impurityimpurity[i][k] stores the impurity measure (gini, entropy etc) associated with node k of the i-th tree.
outpointer to store the loaded model
Returns
0 for success, -1 for failure

Definition at line 158 of file c_api.cc.

◆ TreeliteLoadXGBoostJSON()

int TreeliteLoadXGBoostJSON ( const char *  filename,
ModelHandle out 
)

load a json model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tree ensemble.

Parameters
filenamename of model file
outloaded model
Returns
0 for success, -1 for failure

Definition at line 130 of file c_api.cc.

◆ TreeliteLoadXGBoostJSONString()

int TreeliteLoadXGBoostJSONString ( const char *  json_str,
size_t  length,
ModelHandle out 
)

load a model stored as JSON stringby XGBoost (dmlc/xgboost). The model json must contain a decision tree ensemble.

Parameters
json_strthe string containing the JSON model specification
lengththe length of the JSON string
outloaded model
Returns
0 for success, -1 for failure

Definition at line 137 of file c_api.cc.

◆ TreeliteLoadXGBoostModel()

int TreeliteLoadXGBoostModel ( const char *  filename,
ModelHandle out 
)

load a model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tree ensemble.

Parameters
filenamename of model file
outloaded model
Returns
0 for success, -1 for failure

Definition at line 123 of file c_api.cc.

◆ TreeliteLoadXGBoostModelFromMemoryBuffer()

int TreeliteLoadXGBoostModelFromMemoryBuffer ( const void *  buf,
size_t  len,
ModelHandle out 
)

load an XGBoost model from a memory buffer.

Parameters
bufmemory buffer
lensize of memory buffer
outloaded model
Returns
0 for success, -1 for failure

Definition at line 144 of file c_api.cc.

◆ TreeliteQueryNumClass()

int TreeliteQueryNumClass ( ModelHandle  handle,
size_t *  out 
)

Query the number of classes of the model. (1 if the model is binary classifier or regressor)

Parameters
handlemodel to query
outnumber of output groups
Returns
0 for success, -1 for failure

Definition at line 299 of file c_api.cc.

◆ TreeliteQueryNumFeature()

int TreeliteQueryNumFeature ( ModelHandle  handle,
size_t *  out 
)

Query the number of features used in the model.

Parameters
handlemodel to query
outnumber of features
Returns
0 for success, -1 for failure

Definition at line 292 of file c_api.cc.

◆ TreeliteQueryNumTree()

int TreeliteQueryNumTree ( ModelHandle  handle,
size_t *  out 
)

Query the number of trees in the model.

Parameters
handlemodel to query
outnumber of trees
Returns
0 for success, -1 for failure

Definition at line 285 of file c_api.cc.

◆ TreeliteSerializeModel()

int TreeliteSerializeModel ( const char *  filename,
ModelHandle  handle 
)

Serialize (persist) a model object to disk.

Parameters
filenamename of the file to which to serialize the model. The file will be using a binary format that's optimized to store the Treelite model object efficiently.
handlehandle to the model object
Returns
0 for success, -1 for failure

Definition at line 223 of file c_api.cc.

◆ TreeliteSetTreeLimit()

int TreeliteSetTreeLimit ( ModelHandle  handle,
size_t  limit 
)

keep first N trees of model, limit must smaller than number of trees.

Parameters
handlemodel
limitnumber of trees to keep
Returns
0 for success, -1 for failure

Definition at line 306 of file c_api.cc.