ml_random_forest

ml_random_forest(
    x,
    y,
    newdata,
    seed=None,
    regression=True,
    features=None,
    minimum_split_size=2,
    max_depth=100,
    number_of_trees=1000,
    alpha=0.1,
)

Random forest regression or classification.

Mirrors the C# RandomForest class: fits number_of_trees decision trees on bootstrap resamples of the training data and reports the spread of their predictions as an interval.

Training cost is linear in number_of_trees, and the library’s default of 1000 is the knob to turn if a call is slow – a few dozen trees is usually enough to see the shape of the answer. A seeded run is bit-identical between Python and R, because the whole computation lives in the shared compiled core.

For a classifier every column is floored to an integer class label, including mean.

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; None uses the computer clock. None
regression bool True fits regression trees; False classifiers. True
features int The number of random features per split. None uses max(1, x.shape[1] - 1). None
minimum_split_size int The smallest node a tree will split. 2
max_depth int The recursion cap. 100
number_of_trees int How many trees to grow. 1000
alpha float The interval level: 0.1 gives a 90% interval. 0.1

Returns

Name Type Description
numpy.ndarray One row per row of newdata, with four columns in the order lower, median, upper, mean. (The R twin returns the same table with those column names attached; numpy arrays carry no dimnames, so the order is fixed and documented instead – the same convention :meth:LinearRegressionResult.predict already uses for its interval table.)

Examples

>>> from corehydropy import ml_random_forest
>>> x = [1, 2, 3, 4, 5, 6, 100, 101, 102, 103, 104, 105]
>>> y = [10, 11, 10, 11, 10, 11, 100, 101, 100, 101, 100, 101]
>>> ml_random_forest(x, y, newdata=[3, 104], seed=42, number_of_trees=25).shape
(2, 4)