Skip to main content

MLCore::MLModelOperation

MLCore::MLModelOperation

Abstract base for all ML model operations. More…

#include <MLModelOperation.hpp>

Inherited by MLCore::DBSCANOperation, MLCore::GaussianMixtureOperation, MLCore::HiddenMarkovModelOperation, MLCore::KMeansOperation, MLCore::LogisticRegressionOperation, MLCore::MLDimReductionOperation, MLCore::MLSupervisedDimReductionOperation, MLCore::NaiveBayesOperation, MLCore::RandomForestOperation, MLCore::SoftmaxRegressionOperation

Public Functions

Name
virtual ~MLModelOperation() =default
virtual std::string getName() const =0
Unique human-readable name for this model (e.g., “Random Forest”).
virtual MLTaskType getTaskType() const =0
The ML task type this operation supports.
virtual std::unique_ptr< MLModelParametersBase > getDefaultParameters() const =0
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)
Train the model on labeled data.
virtual bool predict(arma::mat const & features, arma::Row< std::size_t > & predictions)
Predict class labels for new observations.
virtual bool predictProbabilities(arma::mat const & features, arma::mat & probabilities)
Predict class probabilities for new observations.
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.
virtual bool save(std::ostream & out) const
Save the trained model to a stream.
virtual bool load(std::istream & in)
Load a previously saved model from a stream.
virtual bool isTrained() const =0
Whether the model has been trained (supervised) or fit (unsupervised).
virtual std::size_t numClasses() const
Number of classes (supervised) or clusters (unsupervised).
virtual std::size_t numFeatures() const
Number of features the model was trained on.

Detailed Description

class MLCore::MLModelOperation;

Abstract base for all ML model operations.

A model operation encapsulates a single ML algorithm. It can be trained (supervised) or fit (unsupervised), then used for prediction or cluster assignment. Each operation declares its task type so the registry can categorize it.

Supervised workflow

op->train(features, labels, params);
op->predict(features, predictions);
// Optional:
op->predictProbabilities(features, probabilities);

Unsupervised workflow

op->fit(features, params);
op->assignClusters(features, assignments);

Lifecycle

  1. Construct via factory (MLModelRegistry::create)
  2. Train/fit with parameters
  3. Query [isTrained()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-istrained) to confirm success
  4. Predict/assign on new data
  5. Optionally save/load for persistence

Public Functions Documentation

function ~MLModelOperation

virtual ~MLModelOperation() =default

function getName

virtual std::string getName() const =0

Unique human-readable name for this model (e.g., “Random Forest”).

Reimplemented by: MLCore::DBSCANOperation::getName, MLCore::GaussianMixtureOperation::getName, MLCore::HiddenMarkovModelOperation::getName, MLCore::ICAOperation::getName, MLCore::KMeansOperation::getName, MLCore::LARSProjectionOperation::getName, MLCore::LogisticRegressionOperation::getName, MLCore::LogitProjectionOperation::getName, MLCore::NaiveBayesOperation::getName, MLCore::PCAOperation::getName, MLCore::RandomForestOperation::getName, MLCore::RobustPCAOperation::getName, MLCore::SoftmaxRegressionOperation::getName, MLCore::SupervisedPCAOperation::getName, MLCore::SupervisedRobustPCAOperation::getName, MLCore::TSNEOperation::getName

Used as the key in MLModelRegistry and displayed in the UI.

function getTaskType

virtual MLTaskType getTaskType() const =0

The ML task type this operation supports.

Reimplemented by: MLCore::DBSCANOperation::getTaskType, MLCore::GaussianMixtureOperation::getTaskType, MLCore::HiddenMarkovModelOperation::getTaskType, MLCore::KMeansOperation::getTaskType, MLCore::LARSProjectionOperation::getTaskType, MLCore::LogisticRegressionOperation::getTaskType, MLCore::LogitProjectionOperation::getTaskType, MLCore::MLDimReductionOperation::getTaskType, MLCore::MLSupervisedDimReductionOperation::getTaskType, MLCore::NaiveBayesOperation::getTaskType, MLCore::RandomForestOperation::getTaskType, MLCore::SoftmaxRegressionOperation::getTaskType, MLCore::SupervisedPCAOperation::getTaskType, MLCore::SupervisedRobustPCAOperation::getTaskType

Enables the registry to filter models by task (classification vs clustering).

function getDefaultParameters

virtual std::unique_ptr< MLModelParametersBase > getDefaultParameters() const =0

Create a default parameter set for this model.

Reimplemented by: MLCore::DBSCANOperation::getDefaultParameters, MLCore::GaussianMixtureOperation::getDefaultParameters, MLCore::HiddenMarkovModelOperation::getDefaultParameters, MLCore::ICAOperation::getDefaultParameters, MLCore::KMeansOperation::getDefaultParameters, MLCore::LARSProjectionOperation::getDefaultParameters, MLCore::LogisticRegressionOperation::getDefaultParameters, MLCore::LogitProjectionOperation::getDefaultParameters, MLCore::NaiveBayesOperation::getDefaultParameters, MLCore::PCAOperation::getDefaultParameters, MLCore::RandomForestOperation::getDefaultParameters, MLCore::RobustPCAOperation::getDefaultParameters, MLCore::SoftmaxRegressionOperation::getDefaultParameters, MLCore::SupervisedPCAOperation::getDefaultParameters, MLCore::SupervisedRobustPCAOperation::getDefaultParameters, MLCore::TSNEOperation::getDefaultParameters

Returns a new instance with algorithm-specific defaults. The caller owns the returned pointer.

function train

inline virtual bool train(
    arma::mat const & features,
    arma::Row< std::size_t > const & labels,
    MLModelParametersBase const * params
)

Train the model on labeled data.

Parameters:

  • features Feature matrix (features × observations). Each column is one observation with features.n_rows feature 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]

Reimplemented by: MLCore::HiddenMarkovModelOperation::train, MLCore::LogisticRegressionOperation::train, MLCore::MLSupervisedDimReductionOperation::train, MLCore::NaiveBayesOperation::train, MLCore::RandomForestOperation::train, MLCore::SoftmaxRegressionOperation::train

After successful training, [isTrained()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-istrained) returns true, and [numClasses()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-numclasses) and [numFeatures()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-numfeatures) reflect the training data dimensions.

The default implementation returns false (unsupervised models).

function predict

inline virtual bool predict(
    arma::mat const & features,
    arma::Row< std::size_t > & predictions
)

Predict 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]

Reimplemented by: MLCore::HiddenMarkovModelOperation::predict, MLCore::LogisticRegressionOperation::predict, MLCore::NaiveBayesOperation::predict, MLCore::RandomForestOperation::predict, MLCore::SoftmaxRegressionOperation::predict

The default implementation returns false (unsupervised models).

function predictProbabilities

inline virtual bool predictProbabilities(
    arma::mat const & features,
    arma::mat & probabilities
)

Predict 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

Reimplemented by: MLCore::HiddenMarkovModelOperation::predictProbabilities, MLCore::LogisticRegressionOperation::predictProbabilities, MLCore::NaiveBayesOperation::predictProbabilities, MLCore::RandomForestOperation::predictProbabilities, MLCore::SoftmaxRegressionOperation::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 trainSequences

inline 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.

Parameters:

  • featureSequences Vector of feature matrices, one per contiguous temporal segment. Each matrix is (features × segment_length).
  • labelSequences Vector of label row vectors, aligned 1:1 with featureSequences. Each row is (1 × segment_length).
  • params Algorithm-specific parameters. May be nullptr.

Return: true if training succeeded

Reimplemented by: MLCore::HiddenMarkovModelOperation::trainSequences

The default implementation concatenates all sequences into a single matrix and delegates to [train()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-train). Sequence-aware models (e.g., HMM) override this to preserve temporal structure.

function predictSequences

inline virtual bool predictSequences(
    std::vector< arma::mat > const & featureSequences,
    std::vector< arma::Row< std::size_t > > & predictionSequences
)

Predict labels for pre-segmented temporal sequences.

Parameters:

  • featureSequences Vector of feature matrices, one per segment
  • predictionSequences [out] Vector of prediction row vectors, one per segment, aligned with featureSequences

Return: true if prediction succeeded

Reimplemented by: MLCore::HiddenMarkovModelOperation::predictSequences

The default implementation concatenates, calls [predict()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-predict), and splits the result back. Sequence-aware models override to preserve temporal continuity (e.g., Viterbi decoding per sequence).

function predictSequencesConstrained

inline 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.

Parameters:

  • featureSequences Vector of feature matrices, one per segment
  • predictionSequences [out] Predicted labels per segment
  • initial_state_constraints Per-sequence initial state override. If element i has a value, the model’s initial state distribution is clamped to a delta on that state for sequence i. If nullopt, the model’s learned initial distribution is used.

Return: true if prediction succeeded

Reimplemented by: MLCore::HiddenMarkovModelOperation::predictSequencesConstrained

The default implementation ignores constraints and delegates to predictSequences(). Sequence-aware models (e.g., HMM) override this to apply constrained Viterbi decoding.

function predictSequencesBidirectionalConstrained

inline 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.

Parameters:

  • featureSequences Vector of feature matrices, one per segment
  • predictionSequences [out] Predicted labels per segment
  • initial_state_constraints Per-sequence initial state override (nullopt = unconstrained)
  • terminal_state_constraints Per-sequence terminal state override (nullopt = unconstrained)

Return: true if prediction succeeded

Reimplemented by: MLCore::HiddenMarkovModelOperation::predictSequencesBidirectionalConstrained

For sequence models (e.g., HMM), constraining both ends of a Viterbi path produces tighter predictions in unlabeled gaps between labeled blocks. The terminal constraint changes the backtrace start from argmax to a forced state.

The default implementation ignores terminal constraints and delegates to predictSequencesConstrained().

function predictProbabilitiesPerSequence

inline 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.

Parameters:

  • featureSequences Vector of feature matrices, one per segment
  • probabilitySequences [out] Vector of probability matrices (num_states × segment_length), one per segment
  • initial_state_constraints Per-sequence initial state override (nullopt = unconstrained)
  • terminal_state_constraints Per-sequence terminal state override (nullopt = unconstrained)

Return: true if estimation succeeded

Reimplemented by: MLCore::HiddenMarkovModelOperation::predictProbabilitiesPerSequence

For sequence models, runs Forward-Backward per segment with clamped initial and terminal state distributions.

The default implementation calls predictProbabilities() on each segment independently without constraints.

function isSequenceModel

inline virtual bool isSequenceModel() const

Whether this model natively handles temporal sequences.

Reimplemented by: MLCore::HiddenMarkovModelOperation::isSequenceModel

Models that override trainSequences/predictSequences to exploit temporal structure should return true. Used by pipelines to decide whether to segment data before training.

function fit

inline virtual bool fit(
    arma::mat const & features,
    MLModelParametersBase const * params
)

Fit the model to unlabeled data (unsupervised).

Parameters:

  • features Feature matrix (features × observations)
  • params Algorithm-specific parameters (K, epsilon, etc.)

Return: true if fitting succeeded

Precondition:

  • features must not be empty (enforcement: runtime_check)
  • features must not contain NaN or Inf values (enforcement: none) [IMPORTANT]
  • Features are expected to be z-score normalized for distance-based clustering algorithms (K-Means, DBSCAN, GMM). (enforcement: none) [IMPORTANT]

Reimplemented by: MLCore::DBSCANOperation::fit, MLCore::GaussianMixtureOperation::fit, MLCore::KMeansOperation::fit, MLCore::MLDimReductionOperation::fit

After successful fitting, [isTrained()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-istrained) returns true (the model is ready for [assignClusters()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-assignclusters)), and [numClasses()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-numclasses) returns the number of clusters found/requested.

The default implementation returns false (supervised models).

function assignClusters

inline virtual bool assignClusters(
    arma::mat const & features,
    arma::Row< std::size_t > & assignments
)

Assign cluster labels to observations.

Parameters:

  • features Feature matrix (features × observations)
  • assignments [out] Cluster assignment row vector (1 × observations). Each element is a cluster index in [0, K).

Return: true if assignment 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 fit(). Use FeatureConverter::applyZscoreNormalization() with stored parameters. (enforcement: none) [IMPORTANT]

Reimplemented by: MLCore::DBSCANOperation::assignClusters, MLCore::GaussianMixtureOperation::assignClusters, MLCore::KMeansOperation::assignClusters

The default implementation returns false (supervised models).

function save

inline virtual bool save(
    std::ostream & out
) const

Save the trained model to a stream.

Parameters:

  • out Output stream (binary or text, model-dependent)

Return: true if save succeeded

Reimplemented by: MLCore::DBSCANOperation::save, MLCore::GaussianMixtureOperation::save, MLCore::HiddenMarkovModelOperation::save, MLCore::ICAOperation::save, MLCore::KMeansOperation::save, MLCore::LARSProjectionOperation::save, MLCore::LogisticRegressionOperation::save, MLCore::LogitProjectionOperation::save, MLCore::NaiveBayesOperation::save, MLCore::PCAOperation::save, MLCore::RandomForestOperation::save, MLCore::RobustPCAOperation::save, MLCore::SoftmaxRegressionOperation::save, MLCore::SupervisedPCAOperation::save, MLCore::SupervisedRobustPCAOperation::save

The default implementation returns false (no persistence support).

function load

inline virtual bool load(
    std::istream & in
)

Load a previously saved model from a stream.

Parameters:

  • in Input stream

Return: true if load succeeded

Reimplemented by: MLCore::DBSCANOperation::load, MLCore::GaussianMixtureOperation::load, MLCore::HiddenMarkovModelOperation::load, MLCore::ICAOperation::load, MLCore::KMeansOperation::load, MLCore::LARSProjectionOperation::load, MLCore::LogisticRegressionOperation::load, MLCore::LogitProjectionOperation::load, MLCore::NaiveBayesOperation::load, MLCore::PCAOperation::load, MLCore::RandomForestOperation::load, MLCore::RobustPCAOperation::load, MLCore::SoftmaxRegressionOperation::load, MLCore::SupervisedPCAOperation::load, MLCore::SupervisedRobustPCAOperation::load

After successful loading, [isTrained()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-istrained) should return true. The default implementation returns false (no persistence support).

function isTrained

virtual bool isTrained() const =0

Whether the model has been trained (supervised) or fit (unsupervised).

Reimplemented by: MLCore::DBSCANOperation::isTrained, MLCore::GaussianMixtureOperation::isTrained, MLCore::HiddenMarkovModelOperation::isTrained, MLCore::ICAOperation::isTrained, MLCore::KMeansOperation::isTrained, MLCore::LARSProjectionOperation::isTrained, MLCore::LogisticRegressionOperation::isTrained, MLCore::LogitProjectionOperation::isTrained, MLCore::NaiveBayesOperation::isTrained, MLCore::PCAOperation::isTrained, MLCore::RandomForestOperation::isTrained, MLCore::RobustPCAOperation::isTrained, MLCore::SoftmaxRegressionOperation::isTrained, MLCore::SupervisedPCAOperation::isTrained, MLCore::SupervisedRobustPCAOperation::isTrained, MLCore::TSNEOperation::isTrained

Returns true after a successful [train()](/api_generated/Classes/classMLCore_1_1MLModelOperation.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_1MLModelOperation.md#function-load).

function numClasses

inline virtual std::size_t numClasses() const

Number of classes (supervised) or clusters (unsupervised).

Reimplemented by: MLCore::DBSCANOperation::numClasses, MLCore::GaussianMixtureOperation::numClasses, MLCore::HiddenMarkovModelOperation::numClasses, MLCore::KMeansOperation::numClasses, MLCore::LARSProjectionOperation::numClasses, MLCore::LogisticRegressionOperation::numClasses, MLCore::LogitProjectionOperation::numClasses, MLCore::NaiveBayesOperation::numClasses, MLCore::RandomForestOperation::numClasses, MLCore::SoftmaxRegressionOperation::numClasses, MLCore::SupervisedPCAOperation::numClasses, MLCore::SupervisedRobustPCAOperation::numClasses

Returns 0 if the model has not been trained/fit. For binary classification, returns 2.

function numFeatures

inline virtual std::size_t numFeatures() const

Number of features the model was trained on.

Reimplemented by: MLCore::DBSCANOperation::numFeatures, MLCore::GaussianMixtureOperation::numFeatures, MLCore::HiddenMarkovModelOperation::numFeatures, MLCore::ICAOperation::numFeatures, MLCore::KMeansOperation::numFeatures, MLCore::LARSProjectionOperation::numFeatures, MLCore::LogisticRegressionOperation::numFeatures, MLCore::LogitProjectionOperation::numFeatures, MLCore::NaiveBayesOperation::numFeatures, MLCore::PCAOperation::numFeatures, MLCore::RandomForestOperation::numFeatures, MLCore::RobustPCAOperation::numFeatures, MLCore::SoftmaxRegressionOperation::numFeatures, MLCore::SupervisedPCAOperation::numFeatures, MLCore::SupervisedRobustPCAOperation::numFeatures, MLCore::TSNEOperation::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:41 +0000