mojolearn.DecisionTreeClassifier#

class mojolearn.DecisionTreeClassifier(*, criterion='gini', 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, class_weight=None, ccp_alpha=0.0, monotonic_cst=None, n_bins=128, device='gpu', inference_engine='auto', numeric_mode=None)[source]#

Bases: RandomForestClassifier

A single classification tree (CART).

Follows scikit-learn’s DecisionTreeClassifier: one tree grown on every row (no bootstrap) and, by default, every feature. Split thresholds are searched over n_bins per-feature quantile bins (cuML’s rule) rather than every midpoint, so thresholds can differ from scikit-learn’s. X is read as float32; NaN or infinity in X raises ValueError.

Parameters:
  • criterion ({“gini”, “entropy”, “log_loss”}, default=”gini”) – Split quality measure. "log_loss" is the same as "entropy".

  • 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 ExtraTreesClassifier; it accepts no sample_weight or class_weight.

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

  • class_weight (dict, “balanced” or None, default=None) – Per-class weights, multiplied into sample_weight. "balanced_subsample" is not supported.

  • 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, default=128) – Number of quantile bins per feature used for the split search.

  • 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”, “deterministic”, “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:
  • classes_ (array of shape (n_classes,)) – Class labels seen in fit.

  • n_classes_ (int) – Number of classes.

  • 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[:, 0] > 0.5).astype(np.int32)
>>> model = mojolearn.DecisionTreeClassifier(max_depth=4).fit(X, y)
>>> proba = model.predict_proba(X)
__init__(*, criterion='gini', 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, class_weight=None, ccp_alpha=0.0, monotonic_cst=None, n_bins=128, device='gpu', inference_engine='auto', numeric_mode=None)[source]#

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 class label of each row of X.

predict_proba(X)

Class probabilities: the class distribution of each row's leaf.

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)[source]#

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,)) – Class labels; at least two classes are required.

  • sample_weight (array-like of shape (n_samples,), default=None) – Per-row weights, multiplied into the class_weight row weights. Not accepted with splitter="random".

Returns:

self

get_depth()[source]#

Return the depth of the fitted tree.

get_n_leaves()[source]#

Return the number of leaves of the fitted tree.

predict_proba(X)[source]#

Class probabilities: the class distribution of each row’s leaf.

Returns an array of shape (n_samples, n_classes), columns in the order of classes_.

predict(X)[source]#

Predict the class label of each row of X.

save(path)[source]#

Save the fitted tree to path.

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