Runtime API of Treelite Python package.
treelite_runtime.
Predictor
(libpath, nthread=None, verbose=False)¶Predictor class: loader for compiled shared libraries
global_bias
¶Query global bias of the model
num_feature
¶Query number of features used in the model
num_output_group
¶Query number of output groups of the model
pred_transform
¶Query pred transform 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.
predict_instance
(inst, missing=None, pred_margin=False)¶Perform single-instance prediction. Prediction is run by the calling thread.
inst (numpy.ndarray
/ scipy.sparse.csr_matrix
/ dict
) – 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 (float
, optional) – 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 (bool
, optional) – Whether to produce raw margins rather than transformed probabilities
sigmoid_alpha
¶Query sigmoid alpha of the model
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 (object of class treelite.DMatrix
or scipy.sparse.csr_matrix
) – data matrix
rbegin (int
, optional) – the index of the first row in the subset
rend (int
, optional) – one past the index of the last row in the subset. If missing, set to
the end of the matrix.
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 (object of type numpy.ndarray
, with dimension 2) – data matrix
rbegin (int
, optional) – the index of the first row in the subset
rend (int
, optional) – one past the index of the last row in the subset. If missing, set to
the end of the matrix.
missing (float
, optional) – value indicating missing value. If missing, set to numpy.nan
.
dense_batch – a dense batch consisting of rows [rbegin, rend)
treelite_runtime.
TreeliteRuntimeError
¶Error thrown by Treelite runtime