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.

RegressionTree

Regression tree over numeric features: recursive binary splits chosen to minimize the children's summed squared error (variance reduction), with depth and minimum-sample stops; leaves predict the mean target. Deterministic. Also the base learner for GradientBoostedTrees.

Example

tree := RegressionTree->New(4, 2);
tree->Fit(X, y);                       # y is rows x 1
pred := tree->Predict([2.5]);

Operations

Fit #

Fits the tree to numeric training data.

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

Parameters

NameTypeDescription
XFloatfeature matrix (rows=samples, cols=features)
yFloattarget matrix (rows=samples, cols=1)

Return

TypeDescription
Booltrue if fitting succeeded

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) ~ RegressionTree

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
RegressionTreefitted tree, or Nil on failure

New # constructor

Creates an (unfitted) regression tree.

New(max_depth:Int, min_samples:Int)

Parameters

NameTypeDescription
max_depthIntmaximum tree depth
min_samplesIntminimum samples required to split a node

Predict #

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

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

Parameters

NameTypeDescription
rowFloatfeature values

Return

TypeDescription
Floatpredicted target

PredictAll #

Predicts targets for every row of an input matrix.

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

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Floatprediction matrix (rows x 1), or Nil if not fitted

ReadFrom # function

Reads a tree previously written with WriteTo from a deserializer.

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

Parameters

NameTypeDescription
deserializerDeserializersource deserializer

Return

TypeDescription
RegressionTreefitted tree, or Nil on failure

Score #

Scores the tree on the given data (R-squared of predictions).

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

Parameters

NameTypeDescription
XFloatfeature matrix
yFloattrue target matrix (rows x 1)

Return

TypeDescription
FloatR-squared, or 0.0 if not fitted or inputs are invalid

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

WriteTo #

Serializes the tree (hyperparameters + nodes in pre-order) onto an existing serializer; used by Store and by GradientBoostedTrees.

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

Parameters

NameTypeDescription
serializerSerializerdestination serializer