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). |
Classes
Section titled “Classes”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,). |
PCA.fit
Section titled “PCA.fit”fit( data, n_components: int, *, runtime: str | None = None,) -> PCAFits 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,1tochannels.runtime:str, defaultNone
A runtime fromruntime.available("cuda","hip","wgpu","cpu") for this call. By default: the one chosen withruntime.set, elseDSPK_RUNTIME, else the first available (GPUs first).
Returns
PCA
Raises
ValueError: Forn_componentsoutside1..channels, or fewer than two samples.RuntimeError: When the runtime has no device.
PCA.transform
Section titled “PCA.transform”transform(data, *, runtime: str | None = None) -> np.ndarrayProjects 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, defaultNone
A runtime fromruntime.available("cuda","hip","wgpu","cpu") for this call. By default: the one chosen withruntime.set, elseDSPK_RUNTIME, else the first available (GPUs first).
Returns
np.ndarray—(n_components, samples).
Raises
ValueError: Whendatahas 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. |
PPCA.fit
Section titled “PPCA.fit”fit( data, n_components: int, *, runtime: str | None = None,) -> PPCAFits 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,1tochannels.runtime:str, defaultNone
A runtime fromruntime.available("cuda","hip","wgpu","cpu") for this call. By default: the one chosen withruntime.set, elseDSPK_RUNTIME, else the first available (GPUs first).
Returns
PPCA
Raises
ValueError: Forn_componentsoutside1..channels, or fewer than two samples.RuntimeError: When the runtime has no device.
PPCA.transform
Section titled “PPCA.transform”transform(data, *, runtime: str | None = None) -> np.ndarrayThe 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, defaultNone
A runtime fromruntime.available("cuda","hip","wgpu","cpu") for this call. By default: the one chosen withruntime.set, elseDSPK_RUNTIME, else the first available (GPUs first).
Returns
np.ndarray—(n_components, samples).
Raises
ValueError: Whendatahas another channel count than the fit.