Skip to main content

Cosine similarity and PCA (cov + SVD) from scratch in NumPy with tests

Project description

Run in Google Colab

Open In Colab

Notebooks

Interactive demos are in the notebooks/ folder.

Run in Binder

Binder

Cosine_PCA

Python tests

linalg_fromscratch (NumPy) — Cosine + PCA with Tests

A tiny, dependency-light set of linear algebra utilities implemented from scratch in NumPy, with a pytest suite you can drop into any repo.

Structure

linalg_fromscratch/
├── src/
│   └── linalg_fromscratch/
│       ├── __init__.py
│       ├── cosine.py
│       └── pca.py
├── tests/
│   ├── test_cosine.py
│   └── test_pca.py
├── pytest.ini
├── requirements.txt
└── README.md

Run tests

pip install -r requirements.txt
pytest

Notes

  • Cosine similarity guards against zero vectors.
  • PCA_FromScratch: covariance eigendecomposition.
  • PCA_SVD: SVD-based PCA. Tests confirm equivalence up to sign flips.
  • Tests include:
    • Cosine edge cases
    • Orthonormality of components
    • Variance-along-PCs equals eigenvalues
    • Reconstruction error decreases with more components
    • Covariance-PCA vs SVD-PCA numerical equivalence

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

linalg_fromscratch-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linalg_fromscratch-0.1.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file linalg_fromscratch-0.1.1.tar.gz.

File metadata

  • Download URL: linalg_fromscratch-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for linalg_fromscratch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d4aff1a20205cd35c0c87b0834c98363500ce7dd9c5b7965d5cf6ca7cc2049ce
MD5 6f198213dea3b400616d237a05980d7e
BLAKE2b-256 69819d69b21b98fc300c54854003c8a547da0aeacd2c50c64aa77d6939db894d

See more details on using hashes here.

File details

Details for the file linalg_fromscratch-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for linalg_fromscratch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef4bb982fda73c94a6a14d530e6f67b0467d8da4991dfe6466a778721abd0854
MD5 98a086359f4fddc83fb2196a24022255
BLAKE2b-256 29b85247943e4e628743990d06931b2c590bde68179b2df6aaf86b10a3ea1bcb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page