Docs · Machine learning

rf: random forest classifier.

A random forest of histogram decision trees, the equivalent of Spark MLlib's RandomForestClassifier. Each tree sees a Poisson(1) bootstrap of the rows hashed from the global row index and tree number, and a hashed candidate feature subset per node. Prediction is a majority vote with ties going to the smallest class.

Module
ml_rf
Header
clio_ml/decision_tree.h
Rows
features, then class label

What it does

Trees are fitted one after another with the decision tree engine. Because the bootstrap and the feature subsets are hashed rather than sampled from a stream, the forest is the same for any paging, block count or node count.

Rows

n_features feature columns followed by the class label. clio_ml.COLUMNS["rf"] is 1.

Python

python
import numpy as np, clio_ml as cm
rng = np.random.default_rng(1)
X = rng.normal(size=(4096, 7)).astype(np.float32)
y = (X[:, 0] + X[:, 1] > 0).astype(np.float32)
data = np.column_stack([X, y]).astype(np.float32)

r = cm.fit_rf(data, num_trees=4, tree={"max_depth": 3, "num_classes": 2})
print(len(r.model.trees))
print(cm.predict("rf", r, data[0, :7]))
  • fit_rf(X, **options). The nested tree options are passed as a dict under tree.
  • Options (RandomForestOptions): num_trees (8), seed (1), features_per_node (0 = ceil(sqrt(nf))), tree (a DecisionTreeOptions: num_classes 2, max_depth 4, num_bins 32, threads 256, row_base 0).
  • Result RandomForestResult: model (RandomForestModel with trees, a list of DecisionTreeModel) and stats.
  • predict("rf", r, x) returns the voted class.

C++

cpp
struct RandomForestOptions {
  u32 num_trees = 8;
  u32 seed = 1;
  u32 features_per_node = 0;  // 0 = ceil(sqrt(nf))
  DecisionTreeOptions tree;   // depth, bins, classes, threads, row_base, cluster
};
struct RandomForestModel {
  std::vector<DecisionTreeModel> trees;
  std::vector<float> Flatten() const;
};
struct RandomForestResult { RandomForestModel model; VecStats stats; };
RandomForestResult FitRandomForest(Dataset &ds, const RandomForestOptions &opt);
RandomForestResult FitRandomForestPlain(const float *data, u64 n_rows, u32 nf,
                                        u64 elems_per_page, u32 nblocks,
                                        const RandomForestOptions &opt);
RandomForestResult FitRandomForestReference(/* same arguments */);
u32 RandomForestPredict(const RandomForestModel &m, const float *x);

The ChiMod is module ml_rf.

Related: decision tree, gradient-boosted classifier.