milia.represent.reduce#

Dimensionality reduction by name, and choosing how many components to keep.

Module Attributes

REDUCERS

Reducer names accepted by reducer().

Functions

components(ratios[, method, threshold])

How many components to keep from an ordered explained-variance spectrum.

likelihood(values)

Zhu and Ghodsi (2006) profile-likelihood change point of an ordered spectrum.

pca(X[, method, threshold, random_state])

PCA truncated at the number of components components() chooses.

project(X[, method, random_state])

Two-dimensional projection for plotting.

reducer(name[, n_components, random_state])

An unfitted dimensionality reducer by name.

milia.represent.reduce.REDUCERS = ('none', 'pca', 'kernelpca', 'tsne', 'umap', 'pacmap')#

Reducer names accepted by reducer().

milia.represent.reduce.likelihood(values)#

Zhu and Ghodsi (2006) profile-likelihood change point of an ordered spectrum.

The leading q values and the rest are modelled as two Gaussian samples with a shared variance; the q maximising the profile log-likelihood separates signal from noise, with no threshold to choose.

Parameters:

values (ArrayLike) – Descending eigenvalues or explained-variance ratios, at least 3.

Return type:

tuple[int, ndarray]

Returns:

(q, scores) – the number of components to keep and the log-likelihood at every candidate q = 1, ..., p - 1.

Raises:

ValueError – If fewer than 3 values are given.

Examples

>>> likelihood([10.0, 9.0, 8.5, 0.3, 0.2, 0.1])[0]
3
milia.represent.reduce.components(ratios, method='likelihood', threshold=0.9)#

How many components to keep from an ordered explained-variance spectrum.

Parameters:
  • ratios (ArrayLike) – Descending explained-variance ratios (or eigenvalues).

  • method (Literal['likelihood', 'variance']) – "variance" keeps the fewest components whose cumulative share reaches threshold; "likelihood" finds the profile likelihood change point (see likelihood()).

  • threshold (float) – Cumulative share for method="variance".

Return type:

int

Returns:

Number of components, at least 1.

Raises:

ValueError – If ratios is empty or method is unknown.

Examples

>>> ratios = [0.5, 0.2, 0.1, 0.08, 0.05, 0.04, 0.03]
>>> components(ratios, "variance", threshold=0.75), components(ratios, "likelihood")
(3, 1)
milia.represent.reduce.pca(X, method='variance', threshold=0.9, random_state=0)#

PCA truncated at the number of components components() chooses.

Parameters:
  • X (ArrayLike) – Data, shape (n_samples, n_features).

  • method (Literal['likelihood', 'variance']) – "variance" or "likelihood" (see components()).

  • threshold (float) – Cumulative share for "variance".

  • random_state (int) – Seed of the PCA solver.

Return type:

tuple[ndarray, int]

Returns:

(embedding, n) – the reduced data and the number of components kept.

Examples

>>> X = np.random.default_rng(0).normal(size=(50, 4)) @ np.diag([10.0, 1.0, 0.1, 0.1])
>>> embedding, n = pca(X, threshold=0.9)
>>> embedding.shape, n
((50, 1), 1)
milia.represent.reduce.reducer(name, n_components=2, random_state=0, **params)#

An unfitted dimensionality reducer by name.

Parameters:
  • name (str) – One of REDUCERS; "none" returns None (use the data as is).

  • n_components (int) – Output dimensionality.

  • random_state (int) – Seed.

  • **params (object) – Reducer-specific settings, e.g. perplexity (t-SNE), n_neighbors (UMAP, PaCMAP), kernel (kernel PCA).

Return type:

object | None

Returns:

A scikit-learn-compatible reducer, or None for "none". t-SNE switches to the exact method above three components.

Raises:

Examples

>>> type(reducer("pca", 3)).__name__, reducer("tsne", 4).method
('PCA', 'exact')
milia.represent.reduce.project(X, method='pacmap', random_state=0, **params)#

Two-dimensional projection for plotting.

Parameters:
  • X (ArrayLike) – Data, shape (n_samples, n_features).

  • method (str) – Reducer name (see reducer()); "none" keeps the first two columns.

  • random_state (int) – Seed.

  • **params (object) – Reducer settings, e.g. n_neighbors=24 for PaCMAP.

Return type:

ndarray

Returns:

Coordinates, shape (n_samples, 2).

Examples

>>> project(np.random.default_rng(0).normal(size=(20, 5)), "pca").shape
(20, 2)