MLCore::RandomForestOperation
MLCore::RandomForestOperation
Random Forest classifier wrapping mlpack::RandomForest. More…
#include <RandomForestOperation.hpp>
Inherits from MLCore::MLModelOperation
Public Classes
| Name | |
|---|---|
| struct | Impl |
Public Functions
| Name | |
|---|---|
| RandomForestOperation() | |
| ~RandomForestOperation() override | |
| RandomForestOperation(RandomForestOperation const & ) =delete | |
| RandomForestOperation & | operator=(RandomForestOperation const & ) =delete |
| RandomForestOperation(RandomForestOperation && ) | |
| RandomForestOperation & | operator=(RandomForestOperation && ) |
| virtual std::string | getName() const override Unique human-readable name for this model (e.g., “Random Forest”). |
| virtual MLTaskType | getTaskType() const override The ML task type this operation supports. |
| virtual std::unique_ptr< MLModelParametersBase > | getDefaultParameters() const override Create a default parameter set for this model. |
| virtual bool | train(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params) override Train the model on labeled data. |
| virtual bool | predict(arma::mat const & features, arma::Row< std::size_t > & predictions) override Predict class labels for new observations. |
| virtual bool | predictProbabilities(arma::mat const & features, arma::mat & probabilities) override Predict class probabilities for new observations. |
| virtual bool | save(std::ostream & out) const override Save the trained model to a stream. |
| virtual bool | load(std::istream & in) override Load a previously saved model from a stream. |
| virtual bool | isTrained() const override Whether the model has been trained (supervised) or fit (unsupervised). |
| virtual std::size_t | numClasses() const override Number of classes (supervised) or clusters (unsupervised). |
| virtual std::size_t | numFeatures() const override Number of features the model was trained on. |
Additional inherited members
Public Functions inherited from MLCore::MLModelOperation
| Name | |
|---|---|
| virtual | ~MLModelOperation() =default |
| virtual bool | trainSequences(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > const & labelSequences, MLModelParametersBase const * params) Train the model on pre-segmented temporal sequences. |
| virtual bool | predictSequences(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > & predictionSequences) Predict labels for pre-segmented temporal sequences. |
| virtual bool | predictSequencesConstrained(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > & predictionSequences, std::vector< std::optional< std::size_t > > const & initial_state_constraints) Predict labels with per-sequence initial state constraints. |
| virtual bool | predictSequencesBidirectionalConstrained(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > & predictionSequences, std::vector< std::optional< std::size_t > > const & initial_state_constraints, std::vector< std::optional< std::size_t > > const & terminal_state_constraints) Predict labels with both initial and terminal state constraints. |
| virtual bool | predictProbabilitiesPerSequence(std::vector< arma::mat > const & featureSequences, std::vector< arma::mat > & probabilitySequences, std::vector< std::optional< std::size_t > > const & initial_state_constraints, std::vector< std::optional< std::size_t > > const & terminal_state_constraints) Compute per-sequence state probabilities with boundary constraints. |
| virtual bool | isSequenceModel() const Whether this model natively handles temporal sequences. |
| virtual bool | fit(arma::mat const & features, MLModelParametersBase const * params) Fit the model to unlabeled data (unsupervised). |
| virtual bool | assignClusters(arma::mat const & features, arma::Row< std::size_t > & assignments) Assign cluster labels to observations. |
Detailed Description
class MLCore::RandomForestOperation;Random Forest classifier wrapping mlpack::RandomForest.
Supervised binary/multi-class classification using an ensemble of decision trees with Gini impurity and random dimension selection.
Usage
RandomForestOperation rf;
RandomForestParameters params;
params.num_trees = 100;
params.maximum_depth = 10;
rf.train(features, labels, ¶ms);
rf.predict(new_features, predictions);
// Optional: get per-class probabilities
arma::mat probs;
rf.predictProbabilities(new_features, probs);Public Functions Documentation
function RandomForestOperation
RandomForestOperation()function ~RandomForestOperation
~RandomForestOperation() overridefunction RandomForestOperation
RandomForestOperation(
RandomForestOperation const &
) =deletefunction operator=
RandomForestOperation & operator=(
RandomForestOperation const &
) =deletefunction RandomForestOperation
RandomForestOperation(
RandomForestOperation &&
)function operator=
RandomForestOperation & operator=(
RandomForestOperation &&
)function getName
virtual std::string getName() const overrideUnique human-readable name for this model (e.g., “Random Forest”).
Reimplements: MLCore::MLModelOperation::getName
Used as the key in MLModelRegistry and displayed in the UI.
function getTaskType
virtual MLTaskType getTaskType() const overrideThe ML task type this operation supports.
Reimplements: MLCore::MLModelOperation::getTaskType
Enables the registry to filter models by task (classification vs clustering).
function getDefaultParameters
virtual std::unique_ptr< MLModelParametersBase > getDefaultParameters() const overrideCreate a default parameter set for this model.
Reimplements: MLCore::MLModelOperation::getDefaultParameters
Returns a new instance with algorithm-specific defaults. The caller owns the returned pointer.
function train
virtual bool train(
arma::mat const & features,
arma::Row< std::size_t > const & labels,
MLModelParametersBase const * params
) overrideTrain the model on labeled data.
Parameters:
- features Feature matrix (features × observations). Each column is one observation with
features.n_rowsfeature values. - labels Label row vector (1 × observations). Each element is a class index in [0, num_classes).
- params Algorithm-specific parameters. May be nullptr to use defaults. Must be castable to the concrete parameter type expected by this model.
Return: true if training succeeded
Precondition:
- features must not be empty (enforcement: runtime_check)
- features must not contain NaN or Inf values (enforcement: none) [IMPORTANT]
- labels.n_elem must equal features.n_cols (enforcement: none) [IMPORTANT]
- Features are expected to be z-score normalized for scale-sensitive classifiers (Logistic Regression, Softmax Regression). Tree-based methods (Random Forest) are scale-invariant. (enforcement: none) [IMPORTANT]
Reimplements: MLCore::MLModelOperation::train
After successful training, [isTrained()](/api_generated/Classes/classMLCore_1_1RandomForestOperation.md#function-istrained) returns true, and [numClasses()](/api_generated/Classes/classMLCore_1_1RandomForestOperation.md#function-numclasses) and [numFeatures()](/api_generated/Classes/classMLCore_1_1RandomForestOperation.md#function-numfeatures) reflect the training data dimensions.
The default implementation returns false (unsupervised models).
function predict
virtual bool predict(
arma::mat const & features,
arma::Row< std::size_t > & predictions
) overridePredict class labels for new observations.
Parameters:
- features Feature matrix (features × observations)
- predictions [out] Predicted label row vector (1 × observations)
Return: true if prediction succeeded
Precondition:
- isTrained() must be true (enforcement: runtime_check)
- features.n_rows must equal numFeatures() (enforcement: none) [IMPORTANT]
- features must not contain NaN or Inf values (enforcement: none) [IMPORTANT]
- Features must be z-score normalized using the SAME parameters (mean/std) that were used for the training data passed to train(). Use FeatureConverter::applyZscoreNormalization() with stored parameters. (enforcement: none) [IMPORTANT]
Reimplements: MLCore::MLModelOperation::predict
The default implementation returns false (unsupervised models).
function predictProbabilities
virtual bool predictProbabilities(
arma::mat const & features,
arma::mat & probabilities
) overridePredict class probabilities for new observations.
Parameters:
- features Feature matrix (features × observations)
- probabilities [out] Probability matrix (num_classes × observations). Each column sums to 1.0.
Return: true if probability estimation is supported and succeeded
Reimplements: MLCore::MLModelOperation::predictProbabilities
Not all models support probability output. The default returns false. Models that do support it (e.g., Logistic Regression, Random Forest) override this method.
function save
virtual bool save(
std::ostream & out
) const overrideSave the trained model to a stream.
Parameters:
- out Output stream (binary or text, model-dependent)
Return: true if save succeeded
Reimplements: MLCore::MLModelOperation::save
The default implementation returns false (no persistence support).
function load
virtual bool load(
std::istream & in
) overrideLoad a previously saved model from a stream.
Parameters:
- in Input stream
Return: true if load succeeded
Reimplements: MLCore::MLModelOperation::load
After successful loading, [isTrained()](/api_generated/Classes/classMLCore_1_1RandomForestOperation.md#function-istrained) should return true. The default implementation returns false (no persistence support).
function isTrained
virtual bool isTrained() const overrideWhether the model has been trained (supervised) or fit (unsupervised).
Reimplements: MLCore::MLModelOperation::isTrained
Returns true after a successful [train()](/api_generated/Classes/classMLCore_1_1RandomForestOperation.md#function-train) or [fit()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-fit) call, or after a successful [load()](/api_generated/Classes/classMLCore_1_1RandomForestOperation.md#function-load).
function numClasses
virtual std::size_t numClasses() const overrideNumber of classes (supervised) or clusters (unsupervised).
Reimplements: MLCore::MLModelOperation::numClasses
Returns 0 if the model has not been trained/fit. For binary classification, returns 2.
function numFeatures
virtual std::size_t numFeatures() const overrideNumber of features the model was trained on.
Reimplements: MLCore::MLModelOperation::numFeatures
Returns 0 if the model has not been trained/fit. Used for validating that prediction data has the correct dimensionality.
Updated on 2026-11-13 at 20:25:42 +0000