Qu

tree_model

Builtin index · Statistics & ML

result = tree_model(X, y, [max_depth=20], [min_samples_split=2], [kind=])

Input: X, y, max_depth, min_samples_split, kind · Output: result

What it is

One CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters). Splits by variance reduction for regression, Gini impurity for classification.

Overloads

X can be number, matrix, or string; y can be number, matrix, or string; max_depth can be number, matrix, or string; min_samples_split can be number, matrix, or string; kind can be number, matrix, or string.

2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = tree_model(X, y, [max_depth=20], [min_samples_split=2], [kind=])
X numberOne CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
y numberOne CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
max_depth number, optional (default 20)One CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
min_samples_split number, optional (default 2)One CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
kind number, optionalkind = "regression" splits each node by variance reduction and predicts the mean of a leaf's training targets -- a continuous number.
result Record
—

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

Xtr = randn(80, 2, seed = 53)
ytr = Xtr[:, 0] * 3 + Xtr[:, 1] * -2 + 0.1 * randn(80, seed = 55)
Xte = randn(20, 2, seed = 54)
yte = Xte[:, 0] * 3 + Xte[:, 1] * -2
trm = tree_model(Xtr, ytr, max_depth = 4, kind = "regression")
print("tree_model regression R2 on new data: {round(trm.score(Xte, yte), 3)}")
output
tree_model regression R2 on new data: 0.799
CodeInputOutputDescription
result = tree_model(X, y, [max_depth=20], [min_samples_split=2], [kind=])
X matrix or stringOne CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
y matrix or stringOne CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
max_depth matrix or string, optional (default 20)One CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
min_samples_split matrix or string, optional (default 2)One CART decision tree on an N-row, D-column matrix X and a length-N target y, with max_depth (integer, default 20) limiting tree depth, min_samples_split (integer, default 2) the minimum node size to keep splitting, and kind a string, "regression" or "classification" (never inferred — must be given explicitly when it matters).
kind matrix or string, optionalkind = "classification" splits each node by Gini impurity instead and predicts the majority class among a leaf's training labels.
result Record
—

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

Xtrc = randn(80, 2, seed = 56)
ytrc = zeros(80)
for i in 0 to 79
    if Xtrc[i, 0] + Xtrc[i, 1] > 0
        ytrc[i] = 1
    end if
end for
Xtec = randn(20, 2, seed = 57)
ytec = zeros(20)
for i in 0 to 19
    if Xtec[i, 0] + Xtec[i, 1] > 0
        ytec[i] = 1
    end if
end for
tcm = tree_model(Xtrc, ytrc, max_depth = 4, kind = "classification")
print("tree_model classification accuracy on new data: {round(tcm.score(Xtec, ytec), 3)}")
output
tree_model classification accuracy on new data: 0.9

random_forest_model takes the same kind= and branches the same way, tree by tree.

How it works

tree_model(X, y, [max_depth=], [min_samples_split=], [kind=]) — a single CART-style tree (see TreeArrays's own doc comment for the flat array representation, and build_tree's for the splitting rule). kind= is explicit, not auto-detected from y ("looks categorical" is genuinely ambiguous — a 0/1 target and a small integer *count* regression target look identical at the type level): kind="regression" (default, variance/SSE reduction) or kind="classification" (Gini impurity).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.