pyNNMF - Non-Negative Matrix Factorization
pyNNMF is a Python library for computing Non-Negative Matrix Factorization (NMF) with built-in support for missing value imputation. Unlike standard NMF libraries (e.g., scikit-learn), pyNNMF is resilient to missing data (NaN values and unobserved entries) and handles them natively using optimized NumPy routines.
Features
- Native Python & NumPy: Highly optimized vectorized linear algebra operations without compiled or external C/C++ dependencies.
- Missing Value Resiliency: Handles missing values (
NaN) and observed/unobserved zeroes without failing or distorting optimization gradients. - Multiple Solvers: Supports Multiplicative Updates (MU), Alternating Least Squares (ALS), and Hierarchical Alternating Least Squares (HALS).
- Multiple Cost Functions: Minimizes Frobenius Norm (Euclidean), Kullback-Leibler (KL) Divergence, and Itakura-Saito (IS) Divergence.
Installation
To install pyNNMF locally or prepare it for development:
git clone https://github.com/mariolpantunes/pyNNMF.git
cd pyNNMF
pip install -e .
Solver & Cost Function Selection Guide
To get the best speed and accuracy (matrix completion / imputation) out of pyNNMF, select the solver and cost function pair according to the data's noise distribution and missingness ratio:
1. By Noise Distribution
| Noise Type | Recommended Cost Function | Recommended Solver | Rationale |
|---|---|---|---|
| Additive / Gaussian | Frobenius Norm (cost_fb) |
nmf_als (or nmf_mu) |
Frobenius norm represents the true negative log-likelihood for Gaussian noise. ALS converges fast. |
| Count / Poisson / Sparse | KL Divergence (cost_kl) |
nmf_mu(cost='kl') (or nmf_mu_kl) |
KL divergence corresponds to Poisson likelihood and enforces sparsity naturally. |
| Scale-Invariant / Audio | IS Divergence (cost_is) |
nmf_mu(cost='is') (or nmf_mu_is) |
IS divergence measures relative rather than absolute errors, protecting small-magnitude values. |
2. By Missingness Ratio
- Low-to-Moderate Missingness (< 30%): HALS solver (
nmf_hals) is recommended. It updates variables coordinate-wise and converges to the lowest training objective minima very fast. (Note: HALS only supports Frobenius norm). - High Missingness (> 30%) / Highly Noisy: MU and ALS solvers (
nmf_mu,nmf_als,rwnmf) are recommended. Their slower, diagonally-scaled update trajectories act as an implicit regularizer, preventing overfitting on the small number of observed entries.
Usage Examples
Demonstration scripts are available in the examples directory:
1. Basic Imputation Example
Demonstrates how to initialize a low-rank matrix, mask entries as missing (NaN), and reconstruct/impute them:
PYTHONPATH=src python examples/imputation_example.py
2. Solver Validation CLI
Benchmark execution times and validate prediction accuracy (out-of-sample RMSE/MAE) across different noise distributions and missingness ratios using your exectimeit library:
PYTHONPATH=src python examples/validate_solvers.py --size 100 --noise gaussian --ratio 0.15
3. Initialization Comparison
Evaluate the impact of different initialization strategies (random, nndsvd, svd_impute) on the convergence speed and final reconstruction error:
PYTHONPATH=src python examples/init_comparison.py
Running Tests & Checks
The test suite can be run using the standard Python unittest module:
PYTHONPATH=src python -m unittest discover -s test
To run formatting and static type checking:
ruff check src/ test/ examples/
npx pyright src/ test/ examples/
Documentation
Detailed package documentation is hosted on GitHub Pages
Authors
- Mário Antunes - mariolpantunes
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for pyNNMF 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pynnmf-0.2.0.tar.gz | 12.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pynnmf-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.7 kB
Release files / pynnmf-0.2.0.tar.gz
| Download URL | pynnmf-0.2.0.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49e0b737e1c641c791729bb7ed69a36a9714104812592d25d5d7fc5dba7b2f71
|
|
BLAKE2b-256 checksum How to use checksums |
393af0ebebdefef7f063abc54d6301fe1fc0a11046b3d757157b94e6edf05fb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / pynnmf-0.2.0-py3-none-any.whl
| Download URL | pynnmf-0.2.0-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2be74a89161bfbdb906c9c0c10c0b055fff993c66f8afaa18ff86d462db3f3f8
|
|
BLAKE2b-256 checksum How to use checksums |
af1b01ccd9178b6b9d3e69a653c2234031c1ced23014728976bbd86b971b74fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Security for pyNNMF 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.