Treelite runtime API

Runtime API of treelite Python package.

Runtime API provides the minimum necessary tools to deploy tree prediction modules in the wild.

class treelite.runtime.Predictor(libpath, verbose=False)

Predictor class: loader for compiled shared libraries

Parameters:
  • libpath (str) – location of dynamic shared library (.dll/.so/.dylib)
  • verbose (bool, optional) – Whether to print extra messages during construction
predict(batch, nthread=None, verbose=False, pred_margin=False)

Make prediction using a batch of data rows

Parameters:
  • batch (object of type Batch) – batch of rows for which predictions will be made
  • nthread (int, optional) – Number of threads (default to number of cores)
  • verbose (bool, optional) – Whether to print extra messages during prediction
  • 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