Skip to main content

MLCore::MLSupervisedDimReductionOperation

MLCore::MLSupervisedDimReductionOperation

Abstract base for supervised dimensionality reduction operations. More…

#include <MLSupervisedDimReductionOperation.hpp>

Inherits from MLCore::MLModelOperation

Inherited by MLCore::LARSProjectionOperation, MLCore::LogitProjectionOperation, MLCore::SupervisedPCAOperation, MLCore::SupervisedRobustPCAOperation

Public Functions

Name
~MLSupervisedDimReductionOperation() override =default
virtual bool fitTransform(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params, arma::mat & result) =0
Fit the model on labeled data and project to reduced space.
virtual bool transform(arma::mat const & features, arma::mat & result) =0
Project new unlabeled data using the fitted weight matrix.
virtual std::size_t outputDimensions() const =0
Number of output dimensions (equals number of classes for logit projection).
virtual std::vector< std::string > outputColumnNames() const =0
Column names for the output reduced matrix.
virtual bool requiresAllClassesForFitting() const
Whether the model requires all class labels for fitting.
virtual MLTaskType getTaskType() const override
Supervised dimensionality reduction task type.
virtual bool train(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params) override
Route train() to fitTransform() discarding the projected output.

Additional inherited members

Public Functions inherited from MLCore::MLModelOperation

Name
virtual ~MLModelOperation() =default
virtual std::string getName() const =0
Unique human-readable name for this model (e.g., “Random Forest”).
virtual std::unique_ptr< MLModelParametersBase > getDefaultParameters() const =0
Create a default parameter set for this model.
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::MLSupervisedDimReductionOperation;

Abstract base for supervised dimensionality reduction operations.

Unlike [MLDimReductionOperation](/api_generated/Classes/classMLCore_1_1MLDimReductionOperation.md), these algorithms require class labels during fitting to learn a discriminative projection. The output dimensionality is determined by the number of classes (not a user parameter).

Workflow


op->fitTransform(features, labels, params, result);  // fit + project in one call
op->transform(new_features, result);                 // project new unlabeled data
auto names = op->outputColumnNames();                // "Logit:0", "Logit:1", ...

All matrices use mlpack convention: (features × observations) — each column is one observation. The output result has [outputDimensions()](/api_generated/Classes/classMLCore_1_1MLSupervisedDimReductionOperation.md#function-outputdimensions) rows and the same number of columns as the input.

[transform()](/api_generated/Classes/classMLCore_1_1MLSupervisedDimReductionOperation.md#function-transform) requires a prior call to [fitTransform()](/api_generated/Classes/classMLCore_1_1MLSupervisedDimReductionOperation.md#function-fittransform). It uses the fitted weight matrix directly and does not require labels.

Public Functions Documentation

function ~MLSupervisedDimReductionOperation

~MLSupervisedDimReductionOperation() override =default

function fitTransform

virtual bool fitTransform(
    arma::mat const & features,
    arma::Row< std::size_t > const & labels,
    MLModelParametersBase const * params,
    arma::mat & result
) =0

Fit the model on labeled data and project to reduced space.

Parameters:

  • features Input matrix (D × N) in mlpack convention
  • labels Label row vector (N,) with class indices in [0, C)
  • params Algorithm-specific parameters
  • result Output: reduced matrix (C × N) — one row per class logit

Return: true on success

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: runtime_check)
  • At least 2 distinct classes must appear in labels (enforcement: runtime_check)
  • params must be of the correct concrete type for this operation (enforcement: none)
  • Features are expected to be z-score normalized (mean≈0, std≈1 per feature) when scale-sensitive comparison across features is desired. Normalization is the caller’s responsibility (via FeatureConverter). Centering is handled internally by each algorithm. (enforcement: none) [IMPORTANT]

Postcondition:

Reimplemented by: MLCore::LARSProjectionOperation::fitTransform, MLCore::LogitProjectionOperation::fitTransform, MLCore::SupervisedPCAOperation::fitTransform, MLCore::SupervisedRobustPCAOperation::fitTransform

function transform

virtual bool transform(
    arma::mat const & features,
    arma::mat & result
) =0

Project new unlabeled data using the fitted weight matrix.

Parameters:

  • features Input matrix (D × N) — must have same D as training data
  • result Output: reduced matrix (C × N)

Return: true on success, false if model not fitted or dimension mismatch

Precondition:

  • isTrained() must be true (enforcement: runtime_check)
  • features.n_rows must equal numFeatures() (enforcement: runtime_check)
  • 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 fitTransform(). Use FeatureConverter::applyZscoreNormalization() with stored parameters. (enforcement: none) [IMPORTANT]

Reimplemented by: MLCore::LARSProjectionOperation::transform, MLCore::LogitProjectionOperation::transform, MLCore::SupervisedPCAOperation::transform, MLCore::SupervisedRobustPCAOperation::transform

function outputDimensions

virtual std::size_t outputDimensions() const =0

Number of output dimensions (equals number of classes for logit projection).

Reimplemented by: MLCore::LARSProjectionOperation::outputDimensions, MLCore::LogitProjectionOperation::outputDimensions, MLCore::SupervisedPCAOperation::outputDimensions, MLCore::SupervisedRobustPCAOperation::outputDimensions

function outputColumnNames

virtual std::vector< std::string > outputColumnNames() const =0

Column names for the output reduced matrix.

Return: Vector of column names (one per output dimension), e.g. [“Logit:0”, “Logit:1”]

Reimplemented by: MLCore::LARSProjectionOperation::outputColumnNames, MLCore::LogitProjectionOperation::outputColumnNames, MLCore::SupervisedPCAOperation::outputColumnNames, MLCore::SupervisedRobustPCAOperation::outputColumnNames

Default names are generated as “Logit:0”, “Logit:1”, etc. Concrete implementations may override with class-specific names after fitting.

function requiresAllClassesForFitting

inline virtual bool requiresAllClassesForFitting() const

Whether the model requires all class labels for fitting.

Reimplemented by: MLCore::SupervisedPCAOperation::requiresAllClassesForFitting, MLCore::SupervisedRobustPCAOperation::requiresAllClassesForFitting

Discriminative models (e.g. LogitProjection) need samples from every class to learn a decision boundary ⇒ return true (the default).

Non-discriminative models (e.g. SupervisedPCA) only need the “positive” class subset to fit a projection ⇒ return false. The pipeline uses this flag to subset to only the positive class (label > 0) when all rows already carry a label (e.g. interval-based labeling).

function getTaskType

inline virtual MLTaskType getTaskType() const override

Supervised dimensionality reduction task type.

Reimplements: MLCore::MLModelOperation::getTaskType

Reimplemented by: MLCore::SupervisedPCAOperation::getTaskType, MLCore::SupervisedRobustPCAOperation::getTaskType

function train

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

Route train() to fitTransform() discarding the projected output.

Reimplements: MLCore::MLModelOperation::train

Allows supervised dim reduction ops to be registered in MLModelRegistry and trained via the standard supervised interface. The projected result is discarded; call fitTransform() directly to obtain it.


Updated on 2026-11-13 at 20:25:41 +0000