mojolearn.RandomTreesEmbedding#

class mojolearn.RandomTreesEmbedding(n_estimators=100, *, max_depth=5, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_leaf_nodes=None, min_impurity_decrease=0.0, sparse_output=False, n_jobs=None, random_state=None, verbose=0, warm_start=False)[source]#

Bases: _TreesEnsembleBase

An unsupervised embedding by totally random trees.

Follows scikit-learn’s RandomTreesEmbedding: a forest of extremely randomized trees (one random feature per split) is fitted to uniform random targets, and transform one-hot encodes the leaf each row reaches in every tree. Unlike scikit-learn, the output is a dense float64 array; sparse_output=True raises NotImplementedError.

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

  • max_depth (int, default=5) – Maximum depth of each tree.

  • 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) – Stored for scikit-learn compatibility; not used by the fit.

  • max_leaf_nodes (int or None, default=None) – Grow each tree best-first to at most this many leaves.

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

  • sparse_output (bool, default=False) – Only False is accepted.

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

  • random_state (int or None, default=None) – Seed for the random targets and the trees. None means seed 0. A RandomState object raises ValueError.

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

  • warm_start (bool, default=False) – Only False is accepted.

Variables:
  • forest_ (ExtraTreesRegressor) – The fitted forest.

  • n_trees_ (int) – Number of trees.

  • n_output_features_ (int) – Width of the embedding: the total number of leaves.

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

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> emb = mojolearn.RandomTreesEmbedding(n_estimators=10, random_state=0).fit_transform(X)
__init__(n_estimators=100, *, max_depth=5, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_leaf_nodes=None, min_impurity_decrease=0.0, sparse_output=False, n_jobs=None, random_state=None, verbose=0, warm_start=False)[source]#

Methods

apply(X)

Leaf reached by each row in each tree.

fit(X[, y, sample_weight])

Fit the random trees on X (n_samples, n_features).

fit_transform(X[, y, sample_weight])

Fit on X, then return transform(X).

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

set_params(**params)

Set constructor parameters.

transform(X)

One-hot encode the leaves of X.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None, sample_weight=None)[source]#

Fit the random trees on X (n_samples, n_features).

y is ignored. sample_weight must be None; row weights raise NotImplementedError.

Returns:

self

apply(X)[source]#

Leaf reached by each row in each tree.

Returns an int32 array of shape (n_samples, n_trees) of node ids, numbered within each tree.

transform(X)[source]#

One-hot encode the leaves of X.

Returns a dense float64 array of shape (n_samples, n_output_features_).

fit_transform(X, y=None, sample_weight=None)[source]#

Fit on X, then return transform(X).