nirs4all.operators.models.sklearn.oplsda module

Orthogonal PLS Discriminant Analysis (OPLS-DA) classifier for nirs4all.

See pls.py for full documentation and usage examples.

class nirs4all.operators.models.sklearn.oplsda.OPLSDA(n_components: int = 1, pls_components: int = 5, scale: bool = True)[source]

Bases: BaseEstimator, ClassifierMixin

Orthogonal PLS Discriminant Analysis (OPLS-DA) classifier.

# Explicitly declare estimator type for sklearn compatibility (e.g., StackingClassifier) _estimator_type = “classifier”

OPLS-DA combines OPLS filtering with PLS-DA classification. It removes Y-orthogonal variation from X before applying PLS-DA, improving class separation and model interpretability.

Parameters:
  • n_components (int, default=1) – Number of orthogonal components to remove.

  • pls_components (int, default=5) – Number of PLS components for the discriminant model.

  • scale (bool, default=True) – Whether to scale X before fitting.

classes_

Unique class labels.

Type:

ndarray of shape (n_classes,)

n_features_in_

Number of features seen during fit.

Type:

int

opls_

Fitted OPLS transformer.

Type:

pyopls.OPLS

plsda_

Fitted PLS-DA model on filtered data.

Type:

PLSDA

Examples

>>> from nirs4all.operators.models.sklearn.pls import OPLSDA
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=100, n_features=50, n_classes=2,
...                            n_informative=10, random_state=42)
>>> model = OPLSDA(n_components=1, pls_components=5)
>>> model.fit(X, y)
OPLSDA(n_components=1, pls_components=5)
>>> predictions = model.predict(X)

Notes

Requires the pyopls package: pip install pyopls

See also

PLSDA

Standard PLS-DA without orthogonal filtering.

OPLS

OPLS for regression tasks.

References

  • Bylesjö, M., et al. (2006). OPLS discriminant analysis: combining the strengths of PLS-DA and SIMCA classification. Journal of Chemometrics, 20(8-10), 341-351.

fit(X, y)[source]

Fit the OPLS-DA model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (array-like of shape (n_samples,)) – Target class labels.

Returns:

self – Fitted estimator.

Return type:

OPLSDA

Raises:

ImportError – If pyopls package is not installed.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

predict(X)[source]

Predict class labels for samples in X.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples to predict.

Returns:

y_pred – Predicted class labels.

Return type:

ndarray of shape (n_samples,)

predict_proba(X)[source]

Return pseudo-probabilities (PLS responses).

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples.

Returns:

proba – Pseudo-probability estimates.

Return type:

ndarray of shape (n_samples, n_classes)

set_params(**params)[source]

Set the parameters of this estimator.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

OPLSDA

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') OPLSDA

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

transform(X)[source]

Transform X by removing orthogonal variation.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples to transform.

Returns:

X_filtered – Transformed samples with orthogonal variation removed.

Return type:

ndarray of shape (n_samples, n_features)