Runtime API of treelite Python package.
Treelite prediction runtime package
treelite.runtime.
Predictor
(libpath, nthread=None, verbose=False)¶Predictor class: loader for compiled shared libraries
libpath – location of dynamic shared library (.dll/.so/.dylib)
nthread – number of worker threads to use; if unspecified, use maximum number of hardware threads
verbose – Whether to print extra messages during construction
int
, optional
bool
, optional
num_feature
¶Query number of features used in the model
num_output_group
¶Query number of output groups of the model
predict
(batch, verbose=False, pred_margin=False)¶Perform batch prediction with a 2D sparse data matrix. Worker threads will
internally divide up work for batch prediction. Note that this function
may be called by only one thread at a time. In order to use multiple
threads to process multiple prediction requests simultaneously, use
predict_instance()
instead.
batch – batch of rows for which predictions will be made
verbose – Whether to print extra messages during prediction
pred_margin – whether to produce raw margins rather than transformed probabilities
object of type Batch
bool
, optional
bool
, optional
predict_instance
(inst, missing=None, pred_margin=False)¶Perform single-instance prediction. Prediction is run by the calling thread.
inst – Data instance for which a prediction will be made. If inst
is of
type scipy.sparse.csr_matrix
, its first dimension must be 1
(shape[0]==1
). If inst
is of type numpy.ndarray
,
it must be one-dimensional. If inst
is of type
dict
, it must be a dictionary where the keys
indicate feature indices (0-based) and the values corresponding
feature values.
missing – Value in the data instance that represents a missing value. If set to
None
, numpy.nan
will be used. Only applicable if inst
is
of type numpy.ndarray
.
pred_margin – Whether to produce raw margins rather than transformed probabilities
float
, optional
bool
, optional
treelite.runtime.
Batch
¶Batch of rows to be used for prediction
from_csr
(csr, rbegin=None, rend=None)¶Get a sparse batch from a subset of rows in a CSR (Compressed Sparse Row)
matrix. The subset is given by the range [rbegin, rend)
.
csr – data matrix
rbegin – the index of the first row in the subset
rend – one past the index of the last row in the subset. If missing, set to the end of the matrix.
object of class treelite.DMatrix
or scipy.sparse.csr_matrix
int
, optional
int
, optional
sparse_batch – a sparse batch consisting of rows [rbegin, rend)
from_npy2d
(mat, rbegin=0, rend=None, missing=None)¶Get a dense batch from a 2D numpy matrix.
If mat
does not have order='C'
(also known as row-major) or is not
contiguous, a temporary copy will be made.
If mat
does not have dtype=numpy.float32
, a temporary copy will be
made also.
Thus, as many as two temporary copies of data can be made. One should set
input layout and type judiciously to conserve memory.
mat – data matrix
rbegin – the index of the first row in the subset
rend – one past the index of the last row in the subset. If missing, set to the end of the matrix.
missing – value indicating missing value. If missing, set to numpy.nan
.
object of type numpy.ndarray
, with dimension 2
int
, optional
int
, optional
float
, optional
dense_batch – a dense batch consisting of rows [rbegin, rend)