Library for computing Non-Negative Matrix Factorisation with missing values imputation.
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pynnmf-0.2.0.tar.gz.
File metadata
- Download URL: pynnmf-0.2.0.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49e0b737e1c641c791729bb7ed69a36a9714104812592d25d5d7fc5dba7b2f71
|
|
| MD5 |
cebf9c6fcd48de79cb7e0267cb247a7a
|
|
| BLAKE2b-256 |
393af0ebebdefef7f063abc54d6301fe1fc0a11046b3d757157b94e6edf05fb9
|
File details
Details for the file pynnmf-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pynnmf-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2be74a89161bfbdb906c9c0c10c0b055fff993c66f8afaa18ff86d462db3f3f8
|
|
| MD5 |
eb4e773995cdc97097c4bbe2472216cf
|
|
| BLAKE2b-256 |
af1b01ccd9178b6b9d3e69a653c2234031c1ced23014728976bbd86b971b74fb
|