mojolearn.RandomForestClassifier#

class mojolearn.RandomForestClassifier(n_estimators=100, *, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='sqrt', max_leaf_nodes=None, min_impurity_decrease=0.0, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight=None, ccp_alpha=0.0, max_samples=None, monotonic_cst=None, max_leaves=-1, n_bins=128, n_streams=4, max_batch_size=4096, device='gpu', inference_engine='auto', numeric_mode=None)#

Random forest classifier with cuML’s defaults.

An ensemble of bootstrapped decision trees whose split thresholds are searched over at most n_bins per-feature quantiles, as in cuML, rather than scikit-learn’s exact thresholds. Defaults follow cuML. Every scikit-learn-shaped parameter is either honoured or refused by name with NotImplementedError; none is accepted and ignored.

X is converted to float32; NaN or infinity in X raises ValueError. y may hold numbers or strings; NaN labels are refused. The fit is reproducible from random_state, and random_state=None means seed 0 (deterministic), unlike scikit-learn. Supports the fast (Apple GPUs only) and identical numeric modes through numeric_mode.

Parameters:
  • n_estimators (int, default 100) – Number of trees.

  • criterion ({‘gini’, ‘entropy’, ‘log_loss’}, default ‘gini’) – Split criterion; ‘log_loss’ is a second spelling of ‘entropy’.

  • max_depth (int or None, default None) – Maximum tree depth. None means unlimited, as in cuML and scikit-learn.

  • min_samples_split (int, default 2) – Minimum rows needed to split a node.

  • min_samples_leaf (int, default 1) – Minimum rows in a leaf.

  • min_weight_fraction_leaf (float, default 0.0) – Only 0.0 is accepted; any other value raises NotImplementedError.

  • max_features ({‘sqrt’, ‘log2’}, int, float or None, default ‘sqrt’) – Features considered per split: a name, a count in [1, n_features], a fraction in (0, 1], or None for all features.

  • max_leaf_nodes (None, default None) – Refused by name: scikit-learn’s best-first growth to exactly k leaves is not available here. Use max_leaves for cuML’s level-order leaf cap, or mojolearn.ExtraTreesClassifier, whose max_leaf_nodes is scikit-learn’s.

  • min_impurity_decrease (float, default 0.0) – Minimum impurity decrease required to split.

  • bootstrap (bool, default True) – Sample rows with replacement for each tree.

  • oob_score (bool, default False) – Compute the out-of-bag score during fit; requires bootstrap=True. Callable scorers are not supported.

  • n_jobs (None, default None) – Only None is accepted.

  • random_state (int or None, default None) – Seed; None means 0.

  • verbose (int, default 0) – Only 0 is accepted.

  • warm_start (bool, default False) – Only False is accepted; there is no incremental fit.

  • class_weight (dict, ‘balanced’ or None, default None) – Per-class weights. ‘balanced_subsample’ is not supported.

  • ccp_alpha (float, default 0.0) – Only 0.0 is accepted; cost-complexity pruning is not implemented.

  • max_samples (float or None, default None) – Fraction of rows drawn per tree, in (0, 1]. None means all rows.

  • monotonic_cst (None, default None) – Only None is accepted.

  • max_leaves (int, default -1) – cuML’s leaf cap on a level-order grower; -1 (or None) means unlimited. This is not scikit-learn’s max_leaf_nodes.

  • n_bins (int, default 128) – Number of per-feature quantile bins searched for splits.

  • n_streams (int, default 4) – cuML’s stream count parameter.

  • max_batch_size (int, default 4096) – cuML’s node batch size for the tree builder.

  • device ({‘gpu’}, default ‘gpu’) – Only ‘gpu’ is accepted. On a CPU-only install the same setting runs the host build of the forest.

  • inference_engine ({‘auto’, ‘sequential’, ‘parallel_groves’}, default ‘auto’) – How predict reduces over trees. ‘auto’ uses ‘parallel_groves’ in fast mode and ‘sequential’ otherwise.

  • numeric_mode ({‘fast’, ‘identical’} or None, default None) – Numeric mode for this estimator; None uses the package default. 'fast' is available on Apple GPUs only.

Variables:
  • classes_ (list) – Class labels in sorted order (numbers by value, strings by code point).

  • n_classes_ (int) – Number of classes.

  • n_features_in_ (int) – Number of features seen in fit.

  • oob_score_ (float) – Out-of-bag score; present when oob_score=True.

  • oob_decision_function_ (Array) – Out-of-bag class probabilities, (n_samples, n_classes) float64; present when oob_score=True.

  • feature_importances_ (Array) – Impurity importances, float32; present when fit was called with feature_importances=True.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] > 0.5).astype(np.int64)
>>> model = mojolearn.RandomForestClassifier(n_estimators=50).fit(X, y)
>>> proba = model.predict_proba(X)

Methods

apply(X)

Return tree-relative leaf IDs, shape (rows, trees), from the native walk.

fit(X, y, *[, feature_importances])

Fit the forest.

get_params([deep])

load(path)

Load a forest saved by save.

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predicted class labels: the argmax of predict_proba, first maximum winning, mapped through classes_.

predict_proba(X)

Class probabilities: the per-class vote fractions over the trees.

predict_with_leaves(X)

Return ordered raw mean scores (rows, outputs) and leaf IDs (rows, trees).

save(path)

Write the fitted forest to path as an npz file.

score(X, y[, sample_weight])

Mode-aware GPU accuracy or Float32 R², weighted when sample_weight is given (scikit-learn's weighted accuracy and R² on the pinned-sum path of metrics.accuracy_score and metrics.r2_score).

set_params(**params)

Set constructor parameters.

staged_predict(X)

Return eager mean prefixes, shape (rows, trees, outputs), in tree order.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, *, feature_importances=False)#

Fit the forest.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (array-like of shape (n_samples,)) – Targets.

  • feature_importances (bool, default False) – Also compute feature_importances_ during this fit.

Returns:

self

predict_proba(X)#

Class probabilities: the per-class vote fractions over the trees.

Returns:

Array of shape (n_samples, n_classes), float32 – Columns in classes_ order.

predict(X)#

Predicted class labels: the argmax of predict_proba, first maximum winning, mapped through classes_.

Returns:

Array or list of shape (n_samples,) – An int64 or float64 Array for numeric labels, a Python list for string labels.

apply(X)#

Return tree-relative leaf IDs, shape (rows, trees), from the native walk.

classmethod load(path)#

Load a forest saved by save.

The loaded model predicts; it does not refit. Loading a file saved by a different estimator class raises ValueError.

numeric_mode_used()#

The numeric mode this estimator runs in.

Returns:

str – 'identical' or 'fast', read from the compiled binary the estimator uses, not from the argument it was given.

predict_with_leaves(X)#

Return ordered raw mean scores (rows, outputs) and leaf IDs (rows, trees).

Classifier scores are undecoded class votes. This auxiliary ordered contract is independent of an explicitly selected groves engine.

save(path)#

Write the fitted forest to path as an npz file.

The file holds the prediction arrays exactly as fitted, with exact dtypes; floats never pass through decimal text. A model saved with the ‘parallel_groves’ inference engine also records its engine and numeric mode. The file bytes depend only on the model, so equal models give equal file hashes on any machine.

score(X, y, sample_weight=None)#

Mode-aware GPU accuracy or Float32 R², weighted when sample_weight is given (scikit-learn’s weighted accuracy and R² on the pinned-sum path of metrics.accuracy_score and metrics.r2_score).

Class labels are compared without narrowing their values by the x_metrics binding’s grouped match count (accuracy_fraction). Regression scoring uses the same Float32 target domain as tree fitting.

set_params(**params)#

Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.

staged_predict(X)#

Return eager mean prefixes, shape (rows, trees, outputs), in tree order.

Each prefix uses the fixed increasing-tree fold through that tree. Only this requested API allocates the complete prefix buffer.

vendor_used()#

The GPU backend this estimator runs on.

Returns:

str – 'metal', 'cuda' or 'hip', read from the compiled binary the estimator uses.