stnmf.mf.MF
- class stnmf.mf.MF(v, r, w0='nnsvdlrc', seed=0, rng=None, dtype='float32')
Bases:
objectAbstract 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 decomposer (
int) – Number of features to recoverw0 (
(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 0rng (
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 Nonedtype (
strortype, optional) – Number format with which to calculate. Default is ‘float32’
- Raises:
ValueError – If matrix sizes mismatch.
ValueError – If r is smaller than two.
See also
stnmf.initInitialization procedures
stnmf.util.create_rngDefault RNG instance
Methods
__init__(v, r[, w0, seed, rng, dtype])Matrix factorization
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
Readable analysis name
Reconstruction error (residual) based on Frobenius norm
Encodings
Features
- h
Encodings
- Type:
- w
Features
- Type:
- init(w0='nnsvdlrc', lrc_kwargs=None)
Initialize features w
- Parameters:
w0 (
(n,r) array_likeor{'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
- 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 1000callback (
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 Nonecallback_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 Nonecallback_kwargs (
dict, optional) – Dictionary serving as additional keyword arguments passed to the callback if specified. Ignored, if callback is None. Default is Nonedisp (
bool, optional) – Show progress bar. Equivalent to passing disabled=True in tqdm_args. Default is Truetqdm_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 mutablei (
int) – Index of the current (completed) iteration. Initialization is zeroitor (
tqdm.tqdm) – Iterator used during the factorization. Attributes are mutablecallback_data (
dict) – Dictionary to store and preserve callback data. Mutable
- reseed(seed=None)
Re-seed the random number generator (RNG)
- Parameters:
seed (
intorNone, 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_rngCreate an RNG instance