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.
RandomForestClassifier
A forest of continuous-feature classification trees: each is fitted to a bootstrap sample of the rows and considers a sample of the features at every node, and the scores are averaged.
RandomForest above is built on the binary-feature DecisionTree, so continuous data must be binned before it can be used. This is built on DecisionTreeClassifier and splits on thresholds directly.
Both kinds of sampling matter, for different reasons. Bootstrapping the rows and sampling the features decorrelates the trees, which is what makes the average better than any single tree. Sampling the features is also the cheapest thing available on build time: the split search dominates and is linear in the features tried, so the default of sqrt(features) is roughly that speedup over trying all of them.
Deterministic: the same seed rebuilds the same forest, so a result can be reproduced rather than merely repeated.
scikit-learn's RandomForestClassifier maps as: n_estimators (num_trees), max_depth (-1 for None), min_samples_leaf (SetMinSamplesLeaf), max_features (0 for 'sqrt'), random_state (seed), feature_importances_ (GetFeatureImportances); GetFitSeconds times the fit.
Example
forest := RandomForestClassifier->New(100, 8, 5, 42);
forest->Fit(x_train, y_train);
scores := forest->PredictScore(x_test);
recall := Metrics->RecallAtFpr(scores, y_test, 0.001);Operations
- New
- Fit
- GetFeatureImportances
- GetFitSeconds
- GetMaxDepth
- GetMaxFeatures
- GetMaxTreeDepth
- GetMinSamples
- GetMinSamplesLeaf
- GetTreeCount
- IsFitted
- Predict
- PredictScore
- SetMinSamplesLeaf
Fit #
Grows the forest.
method : public : Fit(X:Float[,], y:Bool[]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features, continuous |
| y | Bool | one label per row, true for the positive class |
Return
| Type | Description |
|---|---|
| Bool | true when every tree was built |
GetFeatureImportances #
Mean decrease in Gini impurity per feature: each tree's importances (normalised to sum to 1) averaged over the trees and normalised again, as scikit-learn's feature_importances_.
method : public : GetFeatureImportances() ~ Float[]Return
| Type | Description |
|---|---|
| Float | one importance per feature, or Nil when unfitted |
GetFitSeconds #
Seconds the last Fit took, bootstrapping included.
method : public : GetFitSeconds() ~ FloatReturn
| Type | Description |
|---|---|
| Float | elapsed seconds |
GetMaxDepth #
The depth limit as set; negative means unlimited.
method : public : GetMaxDepth() ~ IntReturn
| Type | Description |
|---|---|
| Int | max_depth |
GetMaxFeatures #
The features sampled per node as set; 0 means sqrt(features).
method : public : GetMaxFeatures() ~ IntReturn
| Type | Description |
|---|---|
| Int | max_features |
GetMaxTreeDepth #
The deepest leaf over all trees.
method : public : GetMaxTreeDepth() ~ IntReturn
| Type | Description |
|---|---|
| Int | depth, or 0 when unfitted |
GetMinSamples #
The node size at or below which a node becomes a leaf.
method : public : GetMinSamples() ~ IntReturn
| Type | Description |
|---|---|
| Int | min_samples |
GetMinSamplesLeaf #
The rows every leaf must keep.
method : public : GetMinSamplesLeaf() ~ IntReturn
| Type | Description |
|---|---|
| Int | min_samples_leaf |
GetTreeCount #
How many trees were grown.
method : public : GetTreeCount() ~ IntReturn
| Type | Description |
|---|---|
| Int | tree count, or 0 when unfitted |
IsFitted #
Whether Fit has been called successfully.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true when this forest has been grown |
New # constructor
Constructor using the usual sqrt(features) sample at each node.
New(num_trees:Int, max_depth:Int, min_samples:Int, seed:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_trees | Int | trees in the forest |
| max_depth | Int | deepest any tree may grow; -1 (DecisionTreeClassifier->UnlimitedDepth) for no limit |
| min_samples | Int | a node with this many rows or fewer becomes a leaf |
| seed | Int | seeds bootstrapping and feature sampling |
New # constructor
Constructor with an explicit feature sample size.
New(num_trees:Int, max_depth:Int, min_samples:Int, max_features:Int, seed:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_trees | Int | trees in the forest |
| max_depth | Int | deepest any tree may grow; -1 (DecisionTreeClassifier->UnlimitedDepth) for no limit |
| min_samples | Int | a node with this many rows or fewer becomes a leaf |
| max_features | Int | features to sample per node; 0 means sqrt(features) |
| seed | Int | seeds bootstrapping and feature sampling |
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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | one label per row, or Nil when unfitted |
PredictScore #
The mean of the trees' leaf proportions: the forest's score for each row.
method : public : PredictScore(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | one score per row, or Nil when unfitted |
SetMinSamplesLeaf #
Sets scikit-learn's min_samples_leaf for every tree: a split is only considered when both children keep at least this many rows. Rows drawn more than once into a tree's bootstrap sample count once per draw.
method : public : SetMinSamplesLeaf(min_samples_leaf:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| min_samples_leaf | Int | rows each leaf must keep; values below 1 mean 1 |