Table of Contents

Class RandomForestClassifier<T>

Namespace
AiDotNet.Classification.Ensemble
Assembly
AiDotNet.dll

Random Forest classifier that combines multiple decision trees trained on random subsets.

public class RandomForestClassifier<T> : EnsembleClassifierBase<T>, ITreeBasedClassifier<T>, IProbabilisticClassifier<T>, IClassifier<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>

Type Parameters

T

The numeric data type used for calculations (e.g., float, double).

Inheritance
RandomForestClassifier<T>
Implements
IFullModel<T, Matrix<T>, Vector<T>>
IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>
IParameterizable<T, Matrix<T>, Vector<T>>
ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>
IGradientComputable<T, Matrix<T>, Vector<T>>
Inherited Members
Extension Methods

Remarks

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.

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:

  1. Is trained on a random subset of the data (bootstrap sampling)
  2. Considers only a random subset of features at each split
  3. 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

Constructors

RandomForestClassifier(RandomForestClassifierOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)

Initializes a new instance of the RandomForestClassifier class.

public RandomForestClassifier(RandomForestClassifierOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)

Parameters

options RandomForestClassifierOptions<T>

Configuration options for the Random Forest.

regularization IRegularization<T, Matrix<T>, Vector<T>>

Optional regularization strategy.

Properties

LeafCount

Gets the number of leaf nodes in the tree.

public int LeafCount { get; }

Property Value

int

The count of terminal nodes (leaves) in the trained tree. Returns 0 if the model has not been trained.

MaxDepth

Gets the maximum depth of the tree.

public int MaxDepth { get; }

Property Value

int

The maximum depth reached during training, or the configured maximum depth.

NodeCount

Gets the number of internal (decision) nodes in the tree.

public int NodeCount { get; }

Property Value

int

The count of non-terminal nodes that make decisions. Returns 0 if the model has not been trained.

OobScore_

Out-of-bag accuracy score (only available if OobScore is enabled).

public double OobScore_ { get; }

Property Value

double

Options

Gets the Random Forest specific options.

protected RandomForestClassifierOptions<T> Options { get; }

Property Value

RandomForestClassifierOptions<T>

Methods

Clone()

Creates a clone of the classifier model.

public override IFullModel<T, Matrix<T>, Vector<T>> Clone()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new instance of the model with the same parameters and options.

CreateNewInstance()

Creates a new instance of the same type as this classifier.

protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new instance of the same classifier type.

GetModelMetadata()

Gets metadata about the model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetadata object containing information about the model.

Remarks

This method returns metadata about the model, including its type, feature count, complexity, description, and additional information specific to classification.

For Beginners: Model metadata provides information about the model itself, rather than the predictions it makes. This includes details about the model's structure (like how many features it uses) and characteristics (like how many classes it can predict). This information can be useful for understanding and comparing different models.

GetModelType()

Returns the model type identifier for this classifier.

protected override ModelType GetModelType()

Returns

ModelType

Train(Matrix<T>, Vector<T>)

Trains the Random Forest on the provided data.

public override void Train(Matrix<T> x, Vector<T> y)

Parameters

x Matrix<T>
y Vector<T>