Since the scope of treelite is limited to prediction only, one must use other machine learning packages to train decision tree ensemble models. In this document, we will show how to import an ensemble model that had been trained elsewhere.
Contents
XGBoost (dmlc/xgboost) is a fast, scalable package for gradient boosting. Both treelite and XGBoost are hosted by the DMLC (Distributed Machine Learning Community) group.
Treelite plays well with XGBoost — if you used XGBoost to train your ensemble model, you need only one line of code to import it. Depending on where your model is located, you should do one of the following:
xgboost.Booster
object# bst = an object of type xgboost.Booster
model = Model.from_xgboost(bst)
# model had been saved to a file named my_model.model
# notice the second argument format='xgboost'
model = Model.load('my_model.model', format='xgboost')
LightGBM (Microsoft/LightGBM) is
another well known machine learning package for gradient boosting. To import
models generated by LightGBM, use the load()
method
with argument format='lightgbm'
:
# model had been saved to a file named my_model.txt
# notice the second argument format='lightgbm'
model = Model.load('my_model.txt', format='lightgbm')
Scikit-learn (scikit-learn/scikit-learn) is a Python machine learning package known for its versatility and ease of use. It supports a wide variety of models and algorithms. The following kinds of models can be imported into treelite.
sklearn.ensemble.RandomForestRegressor
sklearn.ensemble.RandomForestClassifier
sklearn.ensemble.GradientBoostingRegressor
sklearn.ensemble.GradientBoostingClassifier
To import scikit-learn models, use
treelite.gallery.sklearn.import_model()
:
# clf is the model object generated by scikit-learn
import treelite.gallery.sklearn
model = treelite.gallery.sklearn.import_model(clf)
If you used other packages to train your ensemble model, you’d need to specify the model programmatically. There are two ways to do this: