RandomForestClassifier and RandomForestRegressor using C++ tree builders.
Classes
Random Forest classifier - ensemble of random trees.
__init__( self, n_estimators: int = 100, max_features: Any = 'sqrt', max_depth: Optional[int] = None, min_samples_split: int = 2, min_samples_leaf: int = 1, bootstrap: bool = True, oob_score: bool = False, random_state: Optional[int] = None, n_jobs: int = ..., criterion: str = 'gini', )
Overview
The Random Forest algorithm works as follows:
- For each of the T trees, draw a bootstrap sample
- Build a fully-grown randomized tree on each bootstrap sample,
- For prediction, aggregate results via majority voting (classification)
- Optionally compute the out-of-bag (OOB) score using samples not
Theory
Each tree h_t is trained on a bootstrap sample S_t. The ensemble prediction is determined by majority vote:
The generalization error of a Random Forest is bounded by:
where \bar{\rho} is the mean correlation between trees and s is the strength (margin) of individual trees.
The out-of-bag error is computed using each sample's predictions only from trees that did not include it in their bootstrap:
Parameters
n_estimators
max_features
max_depth
min_samples_split
min_samples_leaf
bootstrap
oob_score
random_state
n_jobs
criterion
'gini' or 'entropy').
Attributes
estimators_
classes_
n_features_
oob_score_
oob_score=True).
feature_importances_
Notes
Complexity:
- Training: O(T \cdot n \cdot k \cdot \log(n)) where T =
- Prediction: O(T \cdot \log(n)) per sample
- When you need a robust, general-purpose classifier
- High-dimensional datasets where feature selection is implicit
- When out-of-bag error estimation is desired (no separate validation set)
- When training can be parallelized across multiple cores
- When individual tree interpretability is less important than accuracy
References
See Also
Basic usage for classification with OOB score:
>>> from tuiml.algorithms.trees import RandomForestClassifier
>>> import numpy as np
>>>
>>> # Create sample data
>>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [2, 3], [4, 5]])
>>> y = np.array([0, 0, 1, 1, 0, 1])
>>>
>>> # Fit a random forest
>>> clf = RandomForestClassifier(n_estimators=100, oob_score=True, random_state=42)
>>> clf.fit(X, y)
RandomForestClassifier(...)
>>> predictions = clf.predict(X)
Methods
Random Forest regressor - ensemble of random regression trees.
__init__( self, n_estimators: int = 100, max_features: Any = 'sqrt', max_depth: Optional[int] = None, min_samples_split: int = 2, min_samples_leaf: int = 1, bootstrap: bool = True, oob_score: bool = False, random_state: Optional[int] = None, n_jobs: int = ..., criterion: str = 'squared_error', )
Overview
The Random Forest regression algorithm works as follows:
- For each of the T trees, draw a bootstrap sample
- Build a fully-grown randomized tree on each bootstrap sample,
- For prediction, average the outputs of all trees
- Optionally compute the out-of-bag (OOB) R-squared using samples not
Theory
Each tree h_t is trained on a bootstrap sample S_t. The ensemble prediction is the mean of individual tree predictions:
The out-of-bag R-squared is computed using each sample's predictions only from trees that did not include it in their bootstrap:
Parameters
n_estimators
max_features
max_depth
min_samples_split
min_samples_leaf
bootstrap
oob_score
random_state
n_jobs
criterion
'squared_error' or 'friedman_mse').
Attributes
estimators_
n_features_
oob_score_
oob_score=True).
feature_importances_
Notes
Complexity:
- Training: O(T \cdot n \cdot k \cdot \log(n)) where T =
- Prediction: O(T \cdot \log(n)) per sample
- When you need a robust, general-purpose regressor
- High-dimensional datasets where feature selection is implicit
- When out-of-bag R-squared estimation is desired
- When training can be parallelized across multiple cores
References
>>> from tuiml.algorithms.trees import RandomForestRegressor
>>> import numpy as np
>>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [2, 3], [4, 5]])
>>> y = np.array([1.0, 2.0, 3.0, 4.0, 1.5, 2.5])
>>> reg = RandomForestRegressor(n_estimators=100, oob_score=True, random_state=42)
>>> reg.fit(X, y)
RandomForestRegressor(...)
>>> predictions = reg.predict(X)