RandomForest
Random forest algorithm
Example
forest := RandomForest->New(8);
forest->Train(0.3, data);
result := forest->Query(data);
possible := Bool->Rows(result)->As(Float);
matched := DecisionTree->Matches(result->Columns() - 1, result)->As(Float);
matched_perc := (matched / possible * 100.0)->As(Int);
"matched {$matched_perc}%"->PrintLine();Operations
Fit #
Fits the ensemble: builds num_trees decision trees, each fitted to its own bootstrap sample (rows drawn with replacement) of the training data. Each row is boolean features with the class label in the LAST column.
method : public : Fit(input:Bool[,]) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| input | Bool | training matrix (features..., label) of Bool |
Example
forest := RandomForest->New(16);
forest->Fit(data);
preds := forest->Predict(data);IsFitted #
Whether the ensemble has been trained.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if trained |
Load # function
Loads a fitted ensemble from a file.
function : Load(filename:String) ~ RandomForestParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| RandomForest | fitted ensemble, or Nil on failure |
New # constructor
Constructor with default tree hyperparameters (max depth 8, min samples 1, fixed seed for reproducible bootstrap sampling).
New(num_trees:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_trees | Int | number of trees in the ensemble |
Example
forest := RandomForest->New(16);New # constructor
Constructor with explicit tree hyperparameters.
New(num_trees:Int, max_depth:Int, min_samples:Int, seed:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_trees | Int | number of trees in the ensemble |
| max_depth | Int | maximum depth of each tree |
| min_samples | Int | minimum samples required to split a node |
| seed | Int | PRNG seed for reproducible bootstrap sampling |
Predict #
Predicts class labels by majority vote across all trees (ties resolve to true).
method : public : Predict(input:Bool[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| input | Bool | matrix of feature rows (a trailing label column is ignored) |
Return
| Type | Description |
|---|---|
| Bool | per-row predicted classes |
Score #
Computes classification accuracy on labeled data, comparing the ensemble's majority-vote predictions against the class label in the LAST column.
method : public : Score(input:Bool[,]) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| input | Bool | labeled matrix (features..., label) of Bool |
Return
| Type | Description |
|---|---|
| Float | fraction of correctly classified rows, or 0.0 if not fitted |