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

RandomForest

Random forest algorithm

Example

forest := RandomForest->New(8);
forest->Train(0.3, data);
result := forest->Query(data);

possible := Bool->Rows(result)->As(Float);
matched := DecisionTree->Matches(result->Columns() - 1, result)->As(Float);
matched_perc := (matched / possible * 100.0)->As(Int);
"matched {$matched_perc}%"->PrintLine();

Operations

Fit #

Fits the ensemble: builds num_trees decision trees, each fitted to its own bootstrap sample (rows drawn with replacement) of the training data. Each row is boolean features with the class label in the LAST column.

method : public : Fit(input:Bool[,]) ~ Nil

Parameters

NameTypeDescription
inputBooltraining matrix (features..., label) of Bool

Example

forest := RandomForest->New(16);
forest->Fit(data);
preds := forest->Predict(data);

IsFitted #

Whether the ensemble has been trained.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if trained

Load # function

Loads a fitted ensemble from a file.

function : Load(filename:String) ~ RandomForest

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
RandomForestfitted ensemble, or Nil on failure

New # constructor

Constructor with default tree hyperparameters (max depth 8, min samples 1, fixed seed for reproducible bootstrap sampling).

New(num_trees:Int)

Parameters

NameTypeDescription
num_treesIntnumber of trees in the ensemble

Example

forest := RandomForest->New(16);

New # constructor

Constructor with explicit tree hyperparameters.

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

Parameters

NameTypeDescription
num_treesIntnumber of trees in the ensemble
max_depthIntmaximum depth of each tree
min_samplesIntminimum samples required to split a node
seedIntPRNG seed for reproducible bootstrap sampling

Predict #

Predicts class labels by majority vote across all trees (ties resolve to true).

method : public : Predict(input:Bool[,]) ~ Bool[]

Parameters

NameTypeDescription
inputBoolmatrix of feature rows (a trailing label column is ignored)

Return

TypeDescription
Boolper-row predicted classes

Score #

Computes classification accuracy on labeled data, comparing the ensemble's majority-vote predictions against the class label in the LAST column.

method : public : Score(input:Bool[,]) ~ Float

Parameters

NameTypeDescription
inputBoollabeled matrix (features..., label) of Bool

Return

TypeDescription
Floatfraction of correctly classified rows, or 0.0 if not fitted

Store #

Saves the fitted ensemble to a file.

method : public : Store(filename:String) ~ Bool

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise