ml_decision_tree

ml_decision_tree(
    x,
    y,
    newdata,
    seed=None,
    regression=True,
    features=None,
    minimum_split_size=2,
    max_depth=100,
)

Decision tree regression or classification.

Mirrors the C# DecisionTree class: recursively splits the training data on the feature and threshold that most reduce variance (regression) or most increase information gain (classification), then predicts by walking a new observation down the tree.

At the library’s defaults a REGRESSION tree recurses until every leaf holds a single training observation, so it memorizes the training data and generalizes poorly. That is upstream’s behaviour, not a port artifact, and it is why :func:ml_random_forest exists. Set minimum_split_size or max_depth to regularize it.

Parameters

Name Type Description Default
x array_like Training predictors, one row per observation. required
y array_like The training response, one value per row of x. required
newdata array_like Predictors to predict for, with the same number of columns as x. required
seed int PRNG seed for the random feature subsets; None uses the computer clock. None
regression bool True fits a regression tree; False a classifier. True
features int The number of random features to consider at each split. None (the default) uses the library’s own max(1, x.shape[1] - 1). None
minimum_split_size int The smallest node the tree will split. 2
max_depth int The recursion cap. 100

Returns

Name Type Description
numpy.ndarray One prediction per row of newdata.

Examples

>>> from corehydropy import ml_decision_tree
>>> x = [1, 2, 3, 4, 5, 6, 100, 101, 102, 103, 104, 105]
>>> y = [10, 10, 10, 10, 10, 10, 100, 100, 100, 100, 100, 100]
>>> ml_decision_tree(x, y, newdata=[3, 104], seed=7).tolist()
[10.0, 100.0]