Treelite runtime API

Runtime API of treelite Python package.

Treelite prediction runtime package

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

Predictor class: loader for compiled shared libraries

Parameters
  • 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

Type libpath

str

Type nthread

int, optional

Type verbose

bool, optional

property num_feature

Query number of features used in the model

property 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 – 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

Type batch

object of type Batch

Type verbose

bool, optional

Type pred_margin

bool, optional

predict_instance(inst, missing=None, pred_margin=False)

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

Parameters
  • 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

Type inst

numpy.ndarray / scipy.sparse.csr_matrix / dict

Type missing

float, optional

Type pred_margin

bool, optional

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

Type csr

object of class treelite.DMatrix or scipy.sparse.csr_matrix

Type rbegin

int, optional

Type rend

int, optional

Returns

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

Rtype

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

Type mat

object of type numpy.ndarray, with dimension 2

Type rbegin

int, optional

Type rend

int, optional

Type missing

float, optional

Returns

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

Rtype

Batch

shape()

Get dimensions of the batch

Returns

dims – (number of rows, number of columns)

Rtype

tuple of length 2