SpectralSpaceTransform#

class chemotools.adaptation.SpectralSpaceTransform(n_components: int = 2, with_mean: bool = True, with_std: bool = False)[源代码]

基类:DocLinkMixin, OneToOneFeatureMixin, TransformerMixin, BaseEstimator

Spectral Space Transform (SST) is a transformer used for domain adaptation (calibration) applications.

SST is a linear transformation method used to transfer spectral data from a target domain to a source (reference) domain, allowing calibration models to remain valid across different instruments or acquisition conditions, following the implementation by [1].

The method constructs a shared latent space using singular value decomposition (SVD) of concatenated source and target data, and derives projection matrices to align the target data to the source space.

参数:
  • n_components (int, default=2) -- Number of latent components retained from the singular value decomposition (SVD). Controls the dimensionality of the shared subspace.

  • with_mean (bool, default=True) -- If True, center each domain's data by subtracting its mean before computing the SVD. The source mean is added back after transforming.

  • with_std (bool, default=False) -- If True, scale each domain's data by its standard deviation after centering. Features with zero variance are left unchanged (std set to 1). Following the same convention as StandardScaler.

变量:
  • n_features_in (int) -- Number of features seen during fit.

  • X_target_mean (ndarray of shape (n_features,)) -- Per-feature mean of the target data. Zero vector when with_mean=False.

  • X_source_mean (ndarray of shape (n_features,)) -- Per-feature mean of the source data. Zero vector when with_mean=False.

  • X_target_std (ndarray of shape (n_features,)) -- Per-feature standard deviation of the target data. Ones vector when with_std=False.

  • X_source_std (ndarray of shape (n_features,)) -- Per-feature standard deviation of the source data. Ones vector when with_std=False.

  • P1 (ndarray of shape (n_components, n_features), or None) -- Projection matrix associated with the source domain. It maps the shared latent space back to the source spectral space.

  • P2 (ndarray of shape (n_components, n_features), or None) -- Projection matrix associated with the target domain. It maps target data into the shared latent space.

  • A (ndarray of shape (n_features, n_components), or None) -- Pseudoinverse of P2_. Left factor of the low-rank correction; stored to avoid recomputing the pseudoinverse on every transform call.

  • A_eff (ndarray of shape (n_features, n_components), or None) -- Effective left factor A_ / X_target_std_[:, None] with target scaling absorbed. Equals A_ when with_std=False.

  • dP_eff (ndarray of shape (n_components, n_features), or None) -- Effective right factor (P1_ - P2_) * X_source_std_[None, :] with source scaling absorbed. Equals P1_ - P2_ when with_std=False.

  • scale (ndarray of shape (n_features,), or None) -- Per-feature combined scale X_source_std_ / X_target_std_. Ones vector when with_std=False.

  • bias (ndarray of shape (n_features,), or None) -- Precomputed per-feature bias that absorbs all mean and std shifts, allowing transform() to avoid intermediate array allocations.

  • T (ndarray of shape (n_features, n_features), or None) -- Full transformation matrix I + A_ @ (P1_ - P2_) (property, computed on demand). Equivalent to X_scaled @ T_ in the SST formula but never materialised during transform().

  • x_source_provided (bool) -- Boolean flag indicating if X_source was provided during fitting.

抛出:
  • ValueError -- If X and X_source do not have the same shape.

  • ValueError -- If n_components exceeds min(n_samples, 2 * n_features) of the concatenated matrix.

参见

PiecewiseDirectStandardization

Local standardization using moving windows.

DirectStandardization

Global linear transformation without local windows.

引用

示例

Basic usage >>> import numpy as np >>> from chemotools.adaptation import SpectralSpaceTransform >>> >>> rng = np.random.default_rng(17) >>> X_source = rng.normal(size=(100, 20)) >>> X_target = X_source * 2 - rng.normal(size=(100, 20)) * 0.02 >>> >>> sst = SpectralSpaceTransform(n_components=2).fit(X_target, X_source=X_source) >>> X_transf = sst.transform(X_target)

Attributes

T_

n_features_in_

P1_

P2_

A_

n_features_in_: int
P1_: ndarray | None
P2_: ndarray | None
A_: ndarray | None
fit(X: ndarray, y=None, *, X_source: ndarray | None = None) SpectralSpaceTransform[源代码]

Fit the SpectralSpaceTransform model.

参数:
  • X (np.ndarray of shape (n_samples, n_features)) -- Data from the target instrument.

  • y (None) -- Ignored to align with API.

  • X_source (np.ndarray of shape (n_samples, n_features), optional) -- Data from the source instrument. If None, the transformer defaults to an identity transformation.

返回:

self

返回类型:

SpectralSpaceTransform

transform(X: ndarray) ndarray[源代码]

Use the trained model to transform the target data

参数:

X (np.ndarray of shape (n_samples, n_features)) -- Input data to transform

返回:

X_transformed -- Data transformed

返回类型:

np.ndarray of shape (n_samples, n_features)

property T_: ndarray | None
set_fit_request(*, X_source: bool | None | str = '$UNCHANGED$') SpectralSpaceTransform

Configure whether metadata should be requested to be passed to the fit 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 fit 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 fit.

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

在 1.3 版本加入.

参数:

X_source (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) -- Metadata routing for X_source parameter in fit.

返回:

self -- The updated object.

返回类型:

object