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

TreeNode

A node in a DecisionTree: either a leaf carrying a boolean prediction, or an internal node that branches on a boolean feature (false -> left, true -> right).

Operations

Fit #

Fits a real recursive binary tree to boolean training data. Each row is a set of boolean features with the class label in the LAST column. Nodes branch on the feature with the lowest weighted Gini impurity and stop at max_depth, min_samples, or a pure node (leaf = majority class).

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

Parameters

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

Example

data := [
  [false, false, false],
  [false, true,  false],
  [true,  false, false],
  [true,  true,  true]];   # label = f0 AND f1
tree := DecisionTree->New(4, 1);
tree->Fit(data);
tree->Predict([true, true])->PrintLine();   # true

Gini # native

Calculates the Gini index

function : native : Gini(acheived:Float, goal:Float) ~ Float

Parameters

NameTypeDescription
acheivedFloatnumber acheived
goalFloatachievement target

Return

TypeDescription
FloatGini index

IsFitted #

Whether the tree has been fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if fitted

Load # function

Loads a fitted tree from a file.

function : Load(filename:String) ~ DecisionTree

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
DecisionTreefitted tree, or Nil on failure

LoadCsv # function

Loads a boolean input matrix from a CSV file of 1s and 0s

function : LoadCsv(filename:String) ~ Bool[,]

Parameters

NameTypeDescription
filenameStringCSV file to load

Return

TypeDescription
Boolboolean matrix

Matches # function

Count matches in a column

function : Matches(index:Int, matrix:Bool[,]) ~ Int

Parameters

NameTypeDescription
indexIntcolumn index
matrixBoolmatrix matrix to inspect

Return

TypeDescription
Intnumber of matches

Mismatches # function

Count mismatches in a column

function : Mismatches(index:Int, matrix:Bool[,]) ~ Int

Parameters

NameTypeDescription
indexIntcolumn index
matrixBoolmatrix matrix to inspect

Return

TypeDescription
Intnumber of mismatches

New # constructor

Creates an (unfitted) decision tree classifier.

New(max_depth:Int, min_samples:Int)

Parameters

NameTypeDescription
max_depthIntmaximum tree depth
min_samplesIntminimum samples required to split a node

Example

tree := DecisionTree->New(4, 1);

NewInternal # function

Creates an internal split node.

function : NewInternal(feature:Int) ~ TreeNode

Parameters

NameTypeDescription
featureIntfeature index to branch on

Return

TypeDescription
TreeNodeinternal node

NewLeaf # function

Creates a leaf node.

function : NewLeaf(prediction:Bool) ~ TreeNode

Parameters

NameTypeDescription
predictionBoolclass the leaf predicts

Return

TypeDescription
TreeNodeleaf node

Predict #

Predicts the class for a single feature row by walking the tree.

method : public : Predict(row:Bool[]) ~ Bool

Parameters

NameTypeDescription
rowBoolfeature values (trailing label column, if present, is ignored)

Return

TypeDescription
Boolpredicted class

PredictAll #

Predicts classes for every row of an input matrix.

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

Parameters

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

Return

TypeDescription
Boolper-row predictions

Query # native

Splits a matrix based on a list of decisions

function : native : Query(decisions:Vector<Split>, input:Bool[,]) ~ Bool[,]

Parameters

NameTypeDescription
decisionsVector<Split>list of decisions
inputBoolmatrix to be split

Return

TypeDescription
Boolsplit matrix

ReadFrom # function

Reads a tree previously written with WriteTo from a deserializer.

function : ReadFrom(deserializer:System.IO.Deserializer) ~ DecisionTree

Parameters

NameTypeDescription
deserializerDeserializersource deserializer

Return

TypeDescription
DecisionTreefitted tree, or Nil on failure

Score #

Computes classification accuracy on labeled data, comparing 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

Split # native

Splits a matrix along the given column

function : native : Split(index:Int, input:Bool[,]) ~ Bool[,]

Parameters

NameTypeDescription
indexIntindex used to split
inputBoolmatrix to split

Return

TypeDescription
Boolsplit matrix

Store #

Saves the fitted tree to a file.

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

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise

Train # native

Calculates a list of decision splits

function : native : Train(input:Bool[,]) ~ Vector<Split>

Parameters

NameTypeDescription
inputBooltraining matrix

Return

TypeDescription
Vector<Split>list of decision tree splits

WriteTo #

Serializes the tree (hyperparameters + nodes in pre-order) onto an existing serializer; used by Store and by ensembles that embed trees (RandomForest).

method : public : WriteTo(serializer:System.IO.Serializer) ~ Nil

Parameters

NameTypeDescription
serializerSerializerdestination serializer