stnmf.mf.MF

class stnmf.mf.MF(v, r, w0='nnsvdlrc', seed=0, rng=None, dtype='float32')

Bases: object

Abstract matrix factorization class

__init__(v, r, w0='nnsvdlrc', seed=0, rng=None, dtype='float32')

Matrix factorization

Parameters:
  • v ((n,m) array_like) – Input matrix to decompose

  • r (int) – Number of features to recover

  • w0 ((n,r) array_like, or {'random', 'nnsvdlrc'}, optional) – If provided, serves as the argument w0 for the init function to initialize the features w. If None, no automatic initialization will be performed, and will have to be done manually with init. Default is ‘nnsvdlrc’

  • seed (int, optional) – Random number generator (RNG) seed for reproducibility. If None, the RNG is initialized without a seed based on OS context. The RNG is only used if initialization is random or if using custom callbacks. Default is 0

  • rng (numpy.random.Generator, optional) – Random number generator (RNG) instance. If provided, this overrides seed. Otherwise, the default RNG (twister) will be initialized with seed. The RNG is only used if initialization is random or if using custom callbacks. Default is None

  • dtype (str or type, optional) – Number format with which to calculate. Default is ‘float32’

Raises:

See also

stnmf.init

Initialization procedures

stnmf.util.create_rng

Default RNG instance

Methods

__init__(v, r[, w0, seed, rng, dtype])

Matrix factorization

factorize

init([w0, lrc_kwargs])

Initialize features w

reseed([seed])

Re-seed the random number generator (RNG)

residual()

Re-compute reconstruction error

update_h()

Update encodings h using Moore–Penrose pseudoinverse

update_w()

Update features w

Attributes

desc

Readable analysis name

res

Reconstruction error (residual) based on Frobenius norm

h

Encodings

w

Features

desc = 'Matrix factorization'

Readable analysis name

Type:

str

h

Encodings

Type:

numpy.ndarray

w

Features

Type:

numpy.ndarray

init(w0='nnsvdlrc', lrc_kwargs=None)

Initialize features w

Parameters:
  • w0 ((n,r) array_like or {'random', 'nnsvdlrc'}, optional) – Initial features w. If array_like, serves as the initial w directly. If ‘random’ or ‘nnsvdlrd’, calls the functions from stnmf.init. Default is ‘nnsvdlrc’

  • lrc_kwargs (dict, optional) – Keyword arguments for low-rank correction. Ignored if w0 is not ‘nnsvdlrc’. Default is None

Raises:

ValueError – If array sizes mismatch.

update_w()

Update features w

update_h()

Update encodings h using Moore–Penrose pseudoinverse

residual()

Re-compute reconstruction error

See also

res

Reconstruction error (residual)

property res

Reconstruction error (residual) based on Frobenius norm

Type:

float

factorize(num_iter=1000, callback=None, callback_data=None, callback_kwargs=None, disp=True, tqdm_args=None)

Factorize by iterative updates

Parameters:
  • num_iter (int, optional) – Number of update iterations. Default is 1000

  • callback (function (**kwargs) -> bool, optional) – Callback function to be called after each iteration. See notes below for details on the provided function arguments. A return value of False will terminate the iteration prematurely. Default is None

  • callback_data (dict, optional) – Dictionary to store callback data. Depending on the callback this dictionary will contain the callback results. Ignored, if callback is None. Default is None

  • callback_kwargs (dict, optional) – Dictionary serving as additional keyword arguments passed to the callback if specified. Ignored, if callback is None. Default is None

  • disp (bool, optional) – Show progress bar. Equivalent to passing disabled=True in tqdm_args. Default is True

  • tqdm_args (dict, optional) – Keyword arguments for tqdm progress bar. Ignored if disp if False. Default is None

Notes

The callback function will be supplied with the following keyword arguments. Additional arguments can be passed through callback_kwargs.

Callback Args:
  • self (stnmf.mf.MF) – Current MF object. Attributes are mutable

  • i (int) – Index of the current (completed) iteration. Initialization is zero

  • itor (tqdm.tqdm) – Iterator used during the factorization. Attributes are mutable

  • callback_data (dict) – Dictionary to store and preserve callback data. Mutable

reseed(seed=None)

Re-seed the random number generator (RNG)

Parameters:

seed (int or None, optional) – Random number generator (RNG) seed. If None, the RNG is initialized without a seed based on OS context. Default is None

Notes

The RNG in this class is only used if initialization is random. Calling this method will overwrite the attribute rng. It is not compatible with using a custom rng.

See also

stnmf.util.create_rng

Create an RNG instance