mojolearn.HostForest#

class mojolearn.HostForest(*, estimator, offsets, colid, quesval, left_child, leaves, n_features, n_trees, num_outputs, classes=None, device=None, inference_engine='sequential')[source]#

Bases: object

A saved random forest or extra-trees model that predicts on the CPU.

Build one with HostForest.from_file(path) from a file written by RandomForestClassifier.save, RandomForestRegressor.save, ExtraTreesClassifier.save or ExtraTreesRegressor.save. Prediction runs the inference engine the file names (‘sequential’ or ‘parallel_groves’) and returns what the GPU estimator returns for the same file: random forest probabilities and regression values as float32, extra-trees probabilities and regression values as float64. The model arrays are used exactly as saved, never cast; a file whose tree arrays are inconsistent raises ValueError.

Variables:
  • estimator (str) – Name of the estimator class that saved the model.

  • classes_ (list) – Class labels (classifiers only).

  • n_classes_ (int) – Number of classes (classifiers only).

  • n_features_in_ (int) – Number of features the model expects.

  • device (str or None) – The device recorded in the file.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> y = X[:, 0] * 2.0
>>> mojolearn.RandomForestRegressor(n_estimators=20).fit(X, y).save("rf.npz")
>>> pred = mojolearn.HostForest.from_file("rf.npz").predict(X)
__init__(*, estimator, offsets, colid, quesval, left_child, leaves, n_features, n_trees, num_outputs, classes=None, device=None, inference_engine='sequential')[source]#

Methods

from_file(path)

Load a forest from a file written by a random forest or extra-trees save.

model_sha256()

SHA-256 hex digest of the model arrays' bytes, in save order.

predict(X)

Predict with the saved model.

predict_proba(X)

Class probabilities, for classifier models only.

Attributes

inference_engine

'sequential' or 'parallel_groves'.

n_trees

Number of trees in the model.

classmethod from_file(path)[source]#

Load a forest from a file written by a random forest or extra-trees save.

The model predicts through the inference engine the file names. A model saved with inference_engine='parallel_groves' must have been saved under numeric_mode='identical'; otherwise this raises ValueError, because the CPU engine computes identical results only.

Parameters:

path (str or path-like) – The saved model file.

Returns:

HostForest

property n_trees#

Number of trees in the model.

property inference_engine#

‘sequential’ or ‘parallel_groves’. Read-only.

Type:

The inference engine the file named

model_sha256()[source]#

SHA-256 hex digest of the model arrays’ bytes, in save order.

Identifies the exact model used for a prediction.

predict_proba(X)[source]#

Class probabilities, for classifier models only.

Returns the same values as the estimator’s own predict_proba: float32 for a random forest, float64 for extra trees, shape (n_samples, n_classes). A regressor model raises AttributeError.

predict(X)[source]#

Predict with the saved model.

For a classifier, the class with the highest probability (first maximum winning) mapped through classes_. For a regressor, the mean of the trees’ predictions: float32 for a random forest, float64 for extra trees.