v2026.11.0
All Bundles
Bundle

Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

RandomForestClassifier

A forest of continuous-feature classification trees: each is fitted to a bootstrap sample of the rows and considers a sample of the features at every node, and the scores are averaged.

RandomForest above is built on the binary-feature DecisionTree, so continuous data must be binned before it can be used. This is built on DecisionTreeClassifier and splits on thresholds directly.

Both kinds of sampling matter, for different reasons. Bootstrapping the rows and sampling the features decorrelates the trees, which is what makes the average better than any single tree. Sampling the features is also the cheapest thing available on build time: the split search dominates and is linear in the features tried, so the default of sqrt(features) is roughly that speedup over trying all of them.

Deterministic: the same seed rebuilds the same forest, so a result can be reproduced rather than merely repeated.

scikit-learn's RandomForestClassifier maps as: n_estimators (num_trees), max_depth (-1 for None), min_samples_leaf (SetMinSamplesLeaf), max_features (0 for 'sqrt'), random_state (seed), feature_importances_ (GetFeatureImportances); GetFitSeconds times the fit.

Implements: ScoreModel

Example

forest := RandomForestClassifier->New(100, 8, 5, 42);
forest->Fit(x_train, y_train);
scores := forest->PredictScore(x_test);
recall := Metrics->RecallAtFpr(scores, y_test, 0.001);

Operations

Fit #

Grows the forest.

method : public : Fit(X:Float[,], y:Bool[]) ~ Bool

Parameters

NameTypeDescription
XFloatrows x features, continuous
yBoolone label per row, true for the positive class

Return

TypeDescription
Booltrue when every tree was built

GetFeatureImportances #

Mean decrease in Gini impurity per feature: each tree's importances (normalised to sum to 1) averaged over the trees and normalised again, as scikit-learn's feature_importances_.

method : public : GetFeatureImportances() ~ Float[]

Return

TypeDescription
Floatone importance per feature, or Nil when unfitted

GetFitSeconds #

Seconds the last Fit took, bootstrapping included.

method : public : GetFitSeconds() ~ Float

Return

TypeDescription
Floatelapsed seconds

GetMaxDepth #

The depth limit as set; negative means unlimited.

method : public : GetMaxDepth() ~ Int

Return

TypeDescription
Intmax_depth

GetMaxFeatures #

The features sampled per node as set; 0 means sqrt(features).

method : public : GetMaxFeatures() ~ Int

Return

TypeDescription
Intmax_features

GetMaxTreeDepth #

The deepest leaf over all trees.

method : public : GetMaxTreeDepth() ~ Int

Return

TypeDescription
Intdepth, or 0 when unfitted

GetMinSamples #

The node size at or below which a node becomes a leaf.

method : public : GetMinSamples() ~ Int

Return

TypeDescription
Intmin_samples

GetMinSamplesLeaf #

The rows every leaf must keep.

method : public : GetMinSamplesLeaf() ~ Int

Return

TypeDescription
Intmin_samples_leaf

GetTreeCount #

How many trees were grown.

method : public : GetTreeCount() ~ Int

Return

TypeDescription
Inttree count, or 0 when unfitted

IsFitted #

Whether Fit has been called successfully.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when this forest has been grown

New # constructor

Constructor using the usual sqrt(features) sample at each node.

New(num_trees:Int, max_depth:Int, min_samples:Int, seed:Int)

Parameters

NameTypeDescription
num_treesInttrees in the forest
max_depthIntdeepest any tree may grow; -1 (DecisionTreeClassifier->UnlimitedDepth) for no limit
min_samplesInta node with this many rows or fewer becomes a leaf
seedIntseeds bootstrapping and feature sampling

New # constructor

Constructor with an explicit feature sample size.

New(num_trees:Int, max_depth:Int, min_samples:Int, max_features:Int, seed:Int)

Parameters

NameTypeDescription
num_treesInttrees in the forest
max_depthIntdeepest any tree may grow; -1 (DecisionTreeClassifier->UnlimitedDepth) for no limit
min_samplesInta node with this many rows or fewer becomes a leaf
max_featuresIntfeatures to sample per node; 0 means sqrt(features)
seedIntseeds bootstrapping and feature sampling

Predict #

Labels at the usual half-way threshold. Prefer PredictScore with an explicit threshold when the false-alarm rate matters.

method : public : Predict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boolone label per row, or Nil when unfitted

PredictScore #

The mean of the trees' leaf proportions: the forest's score for each row.

method : public : PredictScore(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatone score per row, or Nil when unfitted

SetMinSamplesLeaf #

Sets scikit-learn's min_samples_leaf for every tree: a split is only considered when both children keep at least this many rows. Rows drawn more than once into a tree's bootstrap sample count once per draw.

method : public : SetMinSamplesLeaf(min_samples_leaf:Int) ~ Nil

Parameters

NameTypeDescription
min_samples_leafIntrows each leaf must keep; values below 1 mean 1