mojolearn.DecisionTreeRegressor#

class mojolearn.DecisionTreeRegressor(*, criterion='squared_error', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, ccp_alpha=0.0, monotonic_cst=None, n_bins=None, device='gpu', inference_engine='auto', numeric_mode=None)#

A single regression tree (CART).

Follows scikit-learn’s DecisionTreeRegressor: one tree grown on every row (no bootstrap) and, by default, every feature. Split thresholds are searched over per-feature quantile bins (cuML’s rule) rather than every midpoint, so thresholds can differ from scikit-learn’s. fit accepts no sample_weight. X and y are read as float32.

Parameters:
  • criterion ({“squared_error”, “mse”, “poisson”, “gamma”, “inverse_gaussian”}, default=”squared_error”) – Split quality measure. "mse" is the same as "squared_error". "absolute_error" and "friedman_mse" raise NotImplementedError.

  • splitter ({“best”, “random”}, default=”best”) – "best" searches the quantile bins of every candidate feature. "random" grows one extremely randomized tree (one random threshold per candidate feature), the builder of ExtraTreesRegressor.

  • max_depth (int or None, default=None) – Maximum depth of the tree. None grows until the other stopping rules apply.

  • min_samples_split (int, default=2) – Minimum number of rows a node needs before it can be split.

  • min_samples_leaf (int, default=1) – Minimum number of rows in each leaf.

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

  • max_features (int, float, {“sqrt”, “log2”} or None, default=None) – Number of features considered per split: an int count, a fraction in (0, 1], "sqrt", "log2", or None for every feature.

  • random_state (int or None, default=None) – Seed for the random draws. None means seed 0, so repeated fits give the same tree (unlike scikit-learn, where None draws a fresh seed).

  • max_leaf_nodes (int or None, default=None) – Grow best-first to at most this many leaves. Supported only with splitter="random"; with splitter="best" it raises NotImplementedError.

  • min_impurity_decrease (float, default=0.0) – A node is split only if the split decreases impurity by at least this much.

  • ccp_alpha (float, default=0.0) – Only 0.0 is accepted; cost-complexity pruning is not implemented and any other value raises NotImplementedError.

  • monotonic_cst (None, default=None) – Only None is accepted; monotonic constraints raise NotImplementedError.

  • n_bins (int or None, default=None) – Number of quantile bins per feature used for the split search. None uses the numeric mode’s default: 128, or 256 in fast mode.

  • device ({“gpu”}, default=”gpu”) – Only "gpu" is accepted. On a CPU-only install the same fit runs on the host binding.

  • inference_engine ({“auto”, “sequential”, “parallel_groves”}, default=”auto”) – Prediction kernel. "auto" lets the library choose; in identical mode it predicts as "sequential".

  • numeric_mode ({“identical”, “fast”} or None, default=None) – Numeric mode for this estimator. None uses the process default (see mojolearn.set_numeric_mode). "fast" applies on Apple GPUs only.

Variables:

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

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = X @ rng.random(8, dtype=np.float32)
>>> model = mojolearn.DecisionTreeRegressor(max_depth=6).fit(X, y)
>>> pred = model.predict(X)

Methods

apply(X)

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

fit(X, y[, sample_weight])

Fit the tree.

get_depth()

Return the depth of the fitted tree.

get_n_leaves()

Return the number of leaves of the fitted tree.

get_params([deep])

load(path)

Load a forest saved by save.

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict the target of each row of X.

predict_with_leaves(X)

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

save(path)

Save the fitted tree to path.

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, sample_weight=None)#

Fit the tree.

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

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

  • sample_weight (None) – Must be None; row weights raise NotImplementedError.

Returns:

self

get_depth()#

Return the depth of the fitted tree.

get_n_leaves()#

Return the number of leaves of the fitted tree.

predict(X)#

Predict the target of each row of X.

save(path)#

Save the fitted tree to path.

Raises NotImplementedError for a tree fitted with splitter="random".

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.

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.