RandomForestClassifier<T>
Models & Types · AiDotNet.Classification.Ensemble
Random Forest classifier that combines multiple decision trees trained on random subsets.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → EnsembleClassifierBase<T> → RandomForestClassifier<T>
Implements: ITreeBasedClassifier<T>
For Beginners
Random Forest is one of the most popular and powerful machine learning algorithms. It works by creating a “forest” of decision trees, where each tree:
- Is trained on a random subset of the data (bootstrap sampling)
- Considers only a random subset of features at each split
- Votes on the final prediction
This randomness makes the trees different from each other, and when combined, they create a robust classifier that:
- Is resistant to overfitting
- Handles both numerical and categorical features
- Works well with default parameters
- Provides feature importance scores
Example: Predicting customer churn
- Tree 1 might focus on usage patterns and account age
- Tree 2 might focus on customer service calls and billing
- Tree 3 might focus on contract type and payment history
- Together, they give a more reliable prediction than any single tree
How It Works
Random Forest is a meta estimator that fits a number of decision tree classifiers on various sub-samples of the dataset and uses averaging to improve predictive accuracy and control overfitting.
Example
// Create random forest classifier with bootstrap aggregation
var options = new RandomForestClassifierOptions<double>();
// Prepare training data
var features = new Matrix<double>(new double[,] { { 1.0, 1.1 }, { 1.2, 0.9 }, { 0.8, 1.0 }, { 5.0, 5.1 }, { 5.2, 4.9 }, { 4.8, 5.0 } });
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1 });
// Train multiple trees on random subsets of data and features
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new RandomForestClassifier<double>(options))
.Build(features, labels);
// Predict using majority vote across all trees in the forest
var newSample = new Matrix<double>(new double[,] { { 1.1, 1.0 } });
var prediction = result.Predict(newSample);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
RandomForestClassifier(RandomForestClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) |
Initializes a new instance of the RandomForestClassifier class. |
Properties
| Property | Summary |
|---|---|
double OobScore_ { get; set; } |
Out-of-bag accuracy score (only available if OobScore is enabled). |
int LeafCount { get; } |
|
int MaxDepth { get; } |
|
int NodeCount { get; } |
Methods
| Method | Summary |
|---|---|
Matrix<T> PredictProbabilities(Matrix<T> input) |
Averages the trees’ class probabilities. |
ModelMetadata<T> GetModelMetadata() |
|
void Train(Matrix<T> x, Vector<T> y) |
Returns the model type identifier for this classifier. |