MLCore::SupervisedPCAOperation
MLCore::SupervisedPCAOperation
Supervised PCA dimensionality reduction. More…
#include <SupervisedPCAOperation.hpp>
Inherits from MLCore::MLSupervisedDimReductionOperation, MLCore::MLModelOperation
Public Classes
| Name | |
|---|---|
| struct | Impl |
Public Functions
| Name | |
|---|---|
| SupervisedPCAOperation() | |
| ~SupervisedPCAOperation() override | |
| SupervisedPCAOperation(SupervisedPCAOperation const & ) =delete | |
| SupervisedPCAOperation & | operator=(SupervisedPCAOperation const & ) =delete |
| SupervisedPCAOperation(SupervisedPCAOperation && ) | |
| SupervisedPCAOperation & | operator=(SupervisedPCAOperation && ) |
| virtual std::string | getName() const override Unique human-readable name for this model (e.g., “Random Forest”). |
| virtual MLTaskType | getTaskType() const override Supervised dimensionality reduction task type. |
| virtual std::unique_ptr< MLModelParametersBase > | getDefaultParameters() const override Create a default parameter set for this model. |
| virtual bool | fitTransform(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params, arma::mat & result) override Fit PCA on labeled observations and project ALL data. |
| virtual bool | transform(arma::mat const & features, arma::mat & result) override Project new data using the fitted eigenvectors. |
| virtual std::size_t | outputDimensions() const override Number of output dimensions (equals number of classes for logit projection). |
| virtual std::vector< std::string > | outputColumnNames() const override Column names for the output reduced matrix. |
| 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. |
| virtual bool | requiresAllClassesForFitting() const override Supervised PCA fits only on the positive class. |
| std::vector< double > | explainedVarianceRatio() const Explained variance ratio per component (of the labeled subset). |
| 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. |
Additional inherited members
Public Functions inherited from MLCore::MLSupervisedDimReductionOperation
| Name | |
|---|---|
| ~MLSupervisedDimReductionOperation() override =default | |
| 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. |
Public Functions inherited from MLCore::MLModelOperation
| Name | |
|---|---|
| virtual | ~MLModelOperation() =default |
| 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. |
Detailed Description
class MLCore::SupervisedPCAOperation;Supervised PCA dimensionality reduction.
Fits PCA on the labeled subset of observations, then projects all data (labeled and unlabeled) into the resulting principal-component space.
Usage
SupervisedPCAOperation spca;
SupervisedPCAParameters params;
params.n_components = 10;
arma::mat result;
spca.fitTransform(features, labels, ¶ms, result);
// result is (n_components × N) for ALL observations
// Re-project new data
arma::mat new_result;
spca.transform(new_features, new_result);
// Explained variance ratio (of the labeled subset)
auto ratios = spca.explainedVarianceRatio();Output convention: (n_components × N), each column is one observation. Unlike LogitProjectionOperation where output dims = num_classes, here the user controls n_components directly.
Public Functions Documentation
function SupervisedPCAOperation
SupervisedPCAOperation()function ~SupervisedPCAOperation
~SupervisedPCAOperation() overridefunction SupervisedPCAOperation
SupervisedPCAOperation(
SupervisedPCAOperation const &
) =deletefunction operator=
SupervisedPCAOperation & operator=(
SupervisedPCAOperation const &
) =deletefunction SupervisedPCAOperation
SupervisedPCAOperation(
SupervisedPCAOperation &&
)function operator=
SupervisedPCAOperation & operator=(
SupervisedPCAOperation &&
)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 overrideSupervised dimensionality reduction task type.
Reimplements: MLCore::MLSupervisedDimReductionOperation::getTaskType
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 fitTransform
virtual bool fitTransform(
arma::mat const & features,
arma::Row< std::size_t > const & labels,
MLModelParametersBase const * params,
arma::mat & result
) overrideFit PCA on labeled observations and project ALL data.
Parameters:
- features (D × N) feature matrix in mlpack convention
- labels (N,) label row with class indices in [0, C)
- params SupervisedPCAParameters or nullptr for defaults
- result (n_components × N) output — all observations projected
Return: true on success, false on validation failure or eigendecomposition error
Precondition:
- features must not be empty (enforcement: runtime_check)
- features must have at least 2 columns (observations) (enforcement: runtime_check)
- features must not contain NaN or Inf — covariance computation and eigendecomposition produce garbage otherwise (enforcement: none) [IMPORTANT]
- labels must not be empty (enforcement: runtime_check)
- labels.n_elem must equal features.n_cols (enforcement: runtime_check)
- labels must contain at least 2 distinct class values (enforcement: runtime_check)
- label values must be class indices and must not overflow std::size_t (enforcement: none) [LOW]
- params, if non-null, must be dynamic_castable to SupervisedPCAParameters (enforcement: runtime_check — falls back to defaults if cast fails)
- SupervisedPCAParameters::n_components must be > 0 (enforcement: runtime_check)
- SupervisedPCAParameters::n_components must be <= features.n_rows (enforcement: runtime_check)
Postcondition:
- On success: isTrained() == true, result has n_components rows and features.n_cols columns, numFeatures() == features.n_rows
- On failure: isTrained() unchanged (may be set to false), result is unmodified
Reimplements: MLCore::MLSupervisedDimReductionOperation::fitTransform
Only the observations whose indices appear with any label in [0, C) are used to compute the covariance matrix and eigenvectors. The resulting projection is then applied to ALL columns in features. Internally centers on the labeled-subset mean before eigendecomposition. Feature scaling (z-score) is intentionally NOT performed here — it is the caller’s responsibility to pre-scale via FeatureConverter if desired.
function transform
virtual bool transform(
arma::mat const & features,
arma::mat & result
) overrideProject new data using the fitted eigenvectors.
Parameters:
- features (D × N) feature matrix — D must match training data
- result (n_components × N) output
Return: true on success, false if not trained or dimension mismatch
Precondition:
- isTrained() must be true (enforcement: runtime_check)
- features must not be empty (enforcement: runtime_check)
- features.n_rows must equal numFeatures() (the feature count used during fitting) (enforcement: runtime_check)
- features must not contain NaN or Inf — they propagate directly into the projected result (enforcement: none) [IMPORTANT]
- If the training data was z-score normalized, features should be normalized with the SAME mean/std via FeatureConverter::applyZscoreNormalization() (enforcement: none) [IMPORTANT]
Postcondition: result has outputDimensions() rows and features.n_cols columns
Reimplements: MLCore::MLSupervisedDimReductionOperation::transform
Centers the input using the labeled-subset mean computed during fitting, then projects through the stored eigenvectors.
function outputDimensions
virtual std::size_t outputDimensions() const overrideNumber of output dimensions (equals number of classes for logit projection).
Reimplements: MLCore::MLSupervisedDimReductionOperation::outputDimensions
function outputColumnNames
virtual std::vector< std::string > outputColumnNames() const overrideColumn names for the output reduced matrix.
Return: Vector of column names (one per output dimension), e.g. [“Logit:0”, “Logit:1”]
Reimplements: MLCore::MLSupervisedDimReductionOperation::outputColumnNames
Default names are generated as “Logit:0”, “Logit:1”, etc. Concrete implementations may override with class-specific names after fitting.
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_1MLSupervisedDimReductionOperation.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_1SupervisedPCAOperation.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.
function requiresAllClassesForFitting
inline virtual bool requiresAllClassesForFitting() const overrideSupervised PCA fits only on the positive class.
Reimplements: MLCore::MLSupervisedDimReductionOperation::requiresAllClassesForFitting
Returns false so that the pipeline subsets to label > 0 (the “Inside” class for interval-based labeling) instead of using all observations.
function explainedVarianceRatio
std::vector< double > explainedVarianceRatio() constExplained variance ratio per component (of the labeled subset).
Return: Vector of length outputDimensions(), or empty if not trained
Each element is the fraction of total variance (in the labeled subset) explained by the corresponding principal component.
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_1SupervisedPCAOperation.md#function-istrained) should return true. The default implementation returns false (no persistence support).
Updated on 2026-11-13 at 20:25:42 +0000