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.

GradientBoostedTrees

Gradient boosted regression trees with squared-error loss: the model starts at the target mean and each stage fits a RegressionTree to the current residuals, added with a shrinkage (learning rate) factor. Deterministic.

Example

gbt := GradientBoostedTrees->New(50, 0.1, 3, 2);
gbt->Fit(X, y);
"R²={$gbt->Score(X, y)}"->PrintLine();

Operations

Fit #

Fits the boosted ensemble.

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

GetTreeCount #

Gets the number of boosting stages fitted.

method : public : GetTreeCount() ~ Int

Return

TypeDescription
Inttree count

IsFitted #

Whether the ensemble has been fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if fitted

Load # function

Loads a fitted ensemble from a file.

function : Load(filename:String) ~ GradientBoostedTrees

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
GradientBoostedTreesfitted ensemble, or Nil on failure

New # constructor

Constructor

New(num_trees:Int, learning_rate:Float, max_depth:Int, min_samples:Int)

Parameters

NameTypeDescription
num_treesIntboosting stages
learning_rateFloatshrinkage per stage (e.g. 0.1)
max_depthIntmaximum depth of each tree
min_samplesIntminimum samples required to split a node

Predict #

Predicts targets for every row of an input matrix.

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

Parameters

NameTypeDescription
XFloatfeature matrix

Return

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

Score #

Scores the ensemble 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 ensemble to a file.

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

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise