Skip to content

dspk.ops.linalg

Models fitted on the device: PCA and probabilistic PCA, scikit-learn style.

Data is (channels, samples): channels are the features, samples the observations. FastICA follows once its iterations run on the device.

PCA Principal component analysis, fitted on the device.
PPCA Probabilistic PCA (Tipping & Bishop).

Principal component analysis, fitted on the device.

Channels are the features and samples the observations. Make one with fit.

Examples

>>> import numpy as np
>>> import dspk
>>> x = np.random.default_rng(0).standard_normal((8, 5_000)).astype(np.float32)
>>> pca = dspk.ops.linalg.PCA.fit(x, 3)
>>> pca.transform(x).shape
(3, 5000)

Properties

Name Type Description
components np.ndarray The components (channels, n_components), one per column.
mean np.ndarray Mean of each channel (channels,), removed before projecting.
explained_variance np.ndarray Variance along each component (n_components,).
explained_variance_ratio np.ndarray Each component’s share of the total variance (n_components,).
fit(
data,
n_components: int,
*,
runtime: str | None = None,
) -> PCA

Fits n_components principal components.

Parameters

  • data : array_like
    Signal (channels, samples), converted to float32 on the host first (these models read host data).
  • n_components : int
    Components to keep, 1 to channels.
  • runtime : str, default None
    A runtime from runtime.available ("cuda", "hip", "wgpu", "cpu") for this call. By default: the one chosen with runtime.set, else DSPK_RUNTIME, else the first available (GPUs first).

Returns

  • PCA

Raises

  • ValueError: For n_components outside 1..channels, or fewer than two samples.
  • RuntimeError: When the runtime has no device.
transform(data, *, runtime: str | None = None) -> np.ndarray

Projects data onto the components.

Parameters

  • data : array_like
    Signal (channels, samples), converted to float32 on the host first (these models read host data).
  • runtime : str, default None
    A runtime from runtime.available ("cuda", "hip", "wgpu", "cpu") for this call. By default: the one chosen with runtime.set, else DSPK_RUNTIME, else the first available (GPUs first).

Returns

  • np.ndarray — (n_components, samples).

Raises

  • ValueError: When data has another channel count than the fit.

Probabilistic PCA (Tipping & Bishop): principal components plus an isotropic noise level.

Closed-form maximum likelihood, fitted on the device. Make one with fit.

Properties

Name Type Description
weights np.ndarray The loadings W (channels, n_components).
mean np.ndarray Mean of each channel (channels,).
noise_variance float The isotropic noise variance, sigma squared.
fit(
data,
n_components: int,
*,
runtime: str | None = None,
) -> PPCA

Fits n_components latent dimensions.

Parameters

  • data : array_like
    Signal (channels, samples), converted to float32 on the host first (these models read host data).
  • n_components : int
    Latent dimensions, 1 to channels.
  • runtime : str, default None
    A runtime from runtime.available ("cuda", "hip", "wgpu", "cpu") for this call. By default: the one chosen with runtime.set, else DSPK_RUNTIME, else the first available (GPUs first).

Returns

  • PPCA

Raises

  • ValueError: For n_components outside 1..channels, or fewer than two samples.
  • RuntimeError: When the runtime has no device.
transform(data, *, runtime: str | None = None) -> np.ndarray

The posterior mean of the latent variables, E[z | x], for data.

Parameters

  • data : array_like
    Signal (channels, samples), converted to float32 on the host first (these models read host data).
  • runtime : str, default None
    A runtime from runtime.available ("cuda", "hip", "wgpu", "cpu") for this call. By default: the one chosen with runtime.set, else DSPK_RUNTIME, else the first available (GPUs first).

Returns

  • np.ndarray — (n_components, samples).

Raises

  • ValueError: When data has another channel count than the fit.