v2026.10.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.

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

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