Matrix Factorisation

audio_samples.factorization holds non-negative matrix factorisation and the decomposition of spectrograms.

Non-negative matrix factorisation of spectrograms.

class audio_samples.factorization.Nmf(n_components, *, init=None, solver='cd', beta_loss=None, tol=0.0001, max_iter=200, random_state=0, alpha_W=0.0, alpha_H=None, l1_ratio=0.0, shuffle=False)

Bases: object

Non-negative matrix factorisation X = W H.

Parameters:
  • n_components (int) – Number of components.

  • init (Optional[str]) – "random", "nndsvd", "nndsvda", "nndsvdar", or None: "nndsvda" when the components fit the matrix, else "random".

  • solver (str) – "cd" (coordinate descent, default) or "mu" (multiplicative).

  • beta_loss (float | str) – β of the divergence, or "frobenius", "kullback-leibler", "itakura-saito". Default "frobenius".

  • tol (float) – Relative stopping tolerance. Default 1e-4.

  • max_iter (int) – Most iterations. Default 200.

  • random_state (int) – Seed. Default 0.

  • alpha_W (float) – Regularisation strength of W. Default 0.

  • alpha_H (float | str) – Regularisation strength of H; "same" uses alpha_W.

  • l1_ratio (float) – Share of L1 in the regularisation. Default 0.

  • shuffle (bool) – Visit coordinates in random order. Default false.

components_

(n_components, n_features) factor H. Raises ValueError before fitting.

fit_transform(X, W=None, H=None)

Fits the factorisation and returns W.

Parameters:
  • X (array_like) – (n_samples, n_features), non-negative.

  • W (Optional[array_like]) – Starting W; give together with H.

  • H (Optional[array_like]) – Starting H.

Returns:

(n_samples, n_components), of X’s precision.

Return type:

numpy.ndarray

n_iter_

Iterations of the last fit.

reconstruction_err_

The final error of the last fit.

transform(X)

Finds W for the fitted H.

Parameters:

X (array_like) – (n_samples, n_features), non-negative.

Returns:

(n_samples, n_components), of X’s precision.

Return type:

numpy.ndarray

audio_samples.factorization.decompose(S, *, n_components=None, transformer=None, sort=False, fit=True, **kwargs)

Factorises a spectrogram into templates and activations.

Parameters:
  • S (array_like) – Non-negative (..., n_features, n_frames) spectrogram. Dimensions before the last are unrolled into one feature axis.

  • n_components (Optional[int]) – Number of components. Default: n_frames features of the unrolled matrix.

  • transformer (Optional[object]) – Any object with fit_transform, transform and components_. Default: a new Nmf.

  • sort (bool) – Order the components by the bin of their largest value (2-D only).

  • fit (bool) – Fit transformer; false uses it as already fitted.

  • **kwargs – Passed to Nmf.

Returns:

components (..., n_features, k) and activations (k, n_frames).

Return type:

tuple[numpy.ndarray, numpy.ndarray]