Treelite runtime API

Runtime API of treelite Python package.

Treelite prediction runtime package

class treelite.runtime.Predictor(libpath, nthread=None, verbose=False, include_master_thread=True)

Predictor class: loader for compiled shared libraries

Parameters:
  • libpath (str) – location of dynamic shared library (.dll/.so/.dylib)
  • nthread (int, optional) – number of worker threads to use; if unspecified, use maximum number of hardware threads
  • verbose (bool, optional) – Whether to print extra messages during construction
  • include_master_thread (bool, optional) – Whether to assign work to the master thread
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.

Parameters:
  • batch (object of type Batch) – batch of rows for which predictions will be made
  • verbose (bool, optional) – Whether to print extra messages during prediction
  • pred_margin (bool, optional) – whether to produce raw margins rather than transformed probabilities
predict_instance(inst, missing=None, pred_margin=False)

Perform single-instance prediction. Prediction is run by the calling thread.

Parameters:
  • 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
class treelite.runtime.Batch

Batch of rows to be used for prediction

classmethod 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).

Parameters:
  • 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.
Returns:

sparse_batch – a sparse batch consisting of rows [rbegin, rend)

Return type:

Batch

classmethod 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.

Parameters:
  • 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.
Returns:

dense_batch – a dense batch consisting of rows [rbegin, rend)

Return type:

Batch

shape()

Get dimensions of the batch

Returns:dims – (number of rows, number of columns)
Return type:tuple of length 2