Principal Component Analysis

Functionality for the PCA-decomposition of arbitrary data.

The classes defined here are meant to be lightweight: they do not store the data, instead deferring its management to the higher-level ModeModel class.

class PrincipalComponentTraining(dataset: Dataset, downsampling_indices: DownsamplingIndices, number_of_components: int)[source]

Training and usage of a Principal Component Analysis models.

Parameters:
  • dataset (Dataset) – Used to generate the data to be used for training.

  • downsampling_indices

  • number_of_components (int) – Number of components to keep when reducing the dimensionality of the data.

train(number_of_training_waveforms: int, n_jobs: int = 1) → PrincipalComponentData[source]

Generate a training set and fit the PCA on it.

n_jobs is sequential (1) by default – parallelism is opt-in; pass a higher value explicitly to use multiple workers.

train_on(residuals) → PrincipalComponentData[source]

Fit the PCA on residuals generated elsewhere.

Used by generate(), which produces the residuals for every mode from one shared EOB sweep.

class PrincipalComponentAnalysisModel(number_of_components: int)[source]
static calculate_cumulative_variance_ratio(pca_data: PrincipalComponentData) → ndarray[source]

Calculate the cumulative variance ratio explained by principal components.

Parameters:

pca_data (PrincipalComponentData) – The PCA data containing eigenvalues.

Returns:

Array of cumulative variance ratios, where each element represents the proportion of variance explained up to that principal component. Shape is (number_of_components,)

Return type:

np.ndarray

static calculate_individual_variance_ratio(pca_data: PrincipalComponentData) → ndarray[source]

Calculate the individual variance ratio explained by each principal component.

Parameters:

pca_data (PrincipalComponentData) – The PCA data containing eigenvalues.

Returns:

Array of individual variance ratios, where each element represents the proportion of variance explained by that principal component. Shape is (number_of_components,)

Return type:

np.ndarray

static calculate_total_variance(pca_data: PrincipalComponentData) → float[source]

Calculate the total variance explained by all principal components.

Parameters:

pca_data (PrincipalComponentData) – The PCA data containing eigenvalues.

Returns:

The total variance (sum of all eigenvalues).

Return type:

float

fit(data: ndarray) → PrincipalComponentData[source]

Fit the PCA model to this dataset.

Parameters:

data (np.ndarray) – Data to fit. Does not need to have zero mean. Should have shape (number_of_datapoints, number_of_dimensions)

Returns:

Data describing the trained PCA model.

Return type:

PrincipalComponentData

static reconstruct_data(reduced_data: ndarray, pca_data: PrincipalComponentData) → ndarray[source]

Reconstruct the data.

Parameters:
  • reduced_data (np.ndarray) – With shape (number_of_points, number_of_components).

  • pca_data (PrincipalComponentData) – To use in the reconstruction.

Returns:

reconstructed_data – With shape (number_of_points, number_of_dimensions).

Return type:

np.ndarray

static reduce_data(data: ndarray, pca_data: PrincipalComponentData) → ndarray[source]

Reduce a dataset to its principal-component representation.

Parameters:
  • data (np.ndarray) – With shape (number_of_points, number_of_dimensions).

  • pca_data (PrincipalComponentData) – To use in the reduction.

Returns:

reduced_data – With shape (number_of_points, number_of_components).

Return type:

np.ndarray

class CovarianceAccumulator[source]

The mean and scatter matrix of rows given in chunks: a PCA of more rows than fit in memory, in one pass over them.

The eigenvalues of the scatter \(X_0^\top X_0\) of the zero-mean rows are the squares of their singular values, which PrincipalComponentAnalysisModel.fit() finds by an SVD of \(X_0\); through the scatter, components below ~1e-16 of the largest eigenvalue are lost to rounding. The sums are taken about the mean of the first chunk, so that they do not cancel against the mean.

add(rows: ndarray) → None[source]

Accumulate the rows (n, number_of_dimensions).

principal_components(number_of_components: int)[source]

The (number_of_dimensions, number_of_components) eigenvectors, the eigenvalues (largest first) and the mean of the rows so far.