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

DecisionTreeClassifier

A classification tree that splits continuous features on a threshold, chosen by Gini impurity.

DecisionTree above takes Bool[,]: every feature must already be binary, so continuous data has to be quantile-binned before it can be used, and the binning throws away exactly the thresholds the tree exists to find. This takes Float[,] and finds the threshold itself, the same way RegressionTree does for a continuous target -- sorted values, midpoints between consecutive distinct values as candidates -- with Gini in place of squared error and a class proportion at the leaf.

PredictScore returns that proportion rather than a label, because a fixed false-positive budget needs a score to threshold. A Bool prediction fixes one operating point and cannot answer what the recall would be at another.

scikit-learn's knobs map as: max_depth (-1, UnlimitedDepth, for None), min_samples_leaf (SetMinSamplesLeaf), feature_importances_ (GetFeatureImportances) and apply (Apply). min_samples is this class's own "stop splitting at this size"; 1 matches sklearn's min_samples_split = 2.

Implements: ScoreModel

Example

tree := DecisionTreeClassifier->New(8, 5);
tree->Fit(x_train, y_train);
scores := tree->PredictScore(x_test);
recall := Metrics->RecallAtFpr(scores, y_test, 0.001);

Operations

Apply #

The leaf each row lands in, scikit-learn's apply: leaves are numbered 0 to GetLeafCount() - 1. Rows that share a leaf share a score, so this is how to see what a min_samples_leaf or max_depth setting did to the partition.

method : public : Apply(X:Float[,]) ~ Int[]

Parameters

NameTypeDescription
XFloatrows x features, with the same feature count used at Fit

Return

TypeDescription
Intone leaf index per row, or Nil when unfitted

Fit #

Builds the tree.

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 the tree was built, false when the inputs do not line up

FitRows #

Builds the tree over a chosen list of rows, which may repeat. A forest fits each tree to a bootstrap sample, and a sample drawn with replacement is a list of row indexes rather than a matrix, so passing the list avoids copying the data once per tree.

method : public : FitRows(X:Float[,], y:Bool[], rows_idx:Int[]) ~ Bool

Parameters

NameTypeDescription
XFloatrows x features, continuous
yBoolone label per row of X
rows_idxIntrow indexes to build over; repeats are allowed and meaningful

Return

TypeDescription
Booltrue when the tree was built

GetDepth #

The deepest leaf of the fitted tree (the root alone is depth 0).

method : public : GetDepth() ~ Int

Return

TypeDescription
Intdepth, or 0 when unfitted

GetFeatureImportances #

Mean decrease in Gini impurity per feature, normalised to sum to 1: scikit-learn's feature_importances_. Each split credits its feature with the rows it saw times the impurity it removed.

method : public : GetFeatureImportances() ~ Float[]

Return

TypeDescription
Floatone importance per feature, all zero for a tree with no split, or Nil when unfitted

GetFitSeconds #

Seconds the last Fit or FitRows took.

method : public : GetFitSeconds() ~ Float

Return

TypeDescription
Floatelapsed seconds

GetLeafCount #

Leaves in the fitted tree.

method : public : GetLeafCount() ~ Int

Return

TypeDescription
Intleaf count, or 0 when unfitted

GetMaxDepth #

The depth limit as set; negative means unlimited.

method : public : GetMaxDepth() ~ Int

Return

TypeDescription
Intmax_depth

GetMinSamples #

The node size at or below which a node becomes a leaf.

method : public : GetMinSamples() ~ Int

Return

TypeDescription
Intmin_samples

GetMinSamplesLeaf #

The rows every leaf must keep.

method : public : GetMinSamplesLeaf() ~ Int

Return

TypeDescription
Intmin_samples_leaf

IsFitted #

Whether Fit has been called successfully.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when this tree has been built

New # constructor

Constructor. Every feature is considered at every node.

New(max_depth:Int, min_samples:Int)

Parameters

NameTypeDescription
max_depthIntdeepest the tree may grow; -1 (UnlimitedDepth) for no limit, 0 for a single leaf
min_samplesInta node with this many rows or fewer becomes a leaf

New # constructor

Constructor for a tree that considers only a sample of the features at each node, which is how a forest decorrelates its trees. It is also the cheapest lever on build time: the split search dominates, and it is linear in the number of features tried, so trying sqrt(f) of f is roughly that speedup.

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

Parameters

NameTypeDescription
max_depthIntdeepest the tree may grow
min_samplesInta node with this many rows or fewer becomes a leaf
max_featuresIntfeatures to sample per node; 0 or more than the width means all
seedIntseeds the feature sampling, so the same seed rebuilds the same tree

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 fraction of training rows at each row's leaf that were positive. This is the score to threshold, and what the fixed-false-positive-rate metrics consume.

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

Parameters

NameTypeDescription
XFloatrows x features, with the same feature count used at Fit

Return

TypeDescription
Floatone score per row, or Nil when unfitted or the shape does not match

SetMinSamplesLeaf #

Sets scikit-learn's min_samples_leaf: a split is only considered when both children keep at least this many rows, so every leaf holds at least that many. This is separate from min_samples, which stops splitting a node that is already small. When a forest fits a tree to a bootstrap sample, a row drawn twice counts twice (scikit-learn counts it once and weights it).

method : public : SetMinSamplesLeaf(min_samples_leaf:Int) ~ Nil

Parameters

NameTypeDescription
min_samples_leafIntrows each leaf must keep; values below 1 mean 1

UnlimitedDepth # function

The max_depth that means no limit, scikit-learn's max_depth=None: -1. Any negative depth works the same way; 0 keeps its meaning of a single leaf.

function : UnlimitedDepth() ~ Int

Return

TypeDescription
Int-1