pytorch-pca


Namepytorch-pca JSON
Version 0.1.3 PyPI version JSON
download
home_pageNone
SummaryComprehensive PyTorch implementation of PCA.
upload_time2025-07-15 21:36:55
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseNone
keywords pca principal component analysis pytorch torch svd pca svd singular value decomposition nipals nonlinear iterative partial least squares non-linear iterative partial least squares rnipals robust nipals bpca bayesian pca ppca probabilistic pca svd impute iterative pca robust pca nonlinear pca non-linear pca
VCS
bugtrack_url
requirements torch
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyTorch PCA

A comprehensive PCA implementation using PyTorch, inspired by the R package [pcaMethods](https://github.com/bioc/pcaMethods).

## Overview

This package provides a unified interface to eight PCA algorithms, all accessible via the `pca` function. The main entry point is `pytorch_pca.pca`, and the package exposes `pca`, `PCAResult`, and `AllowedMethod` in its public API.

[![Release](https://img.shields.io/github/v/tag/ricayanzon/pytorch_pca?label=Pypi&logo=pypi&logoColor=yellow)](https://pypi.org/project/pytorch_pca/)
![PythonVersion](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13-informational)
![PytorchVersion](https://img.shields.io/badge/pytorch-2.7.1-blue)

## Installation

```bash
pip install pytorch_pca
```

## Features

This package provides 8 different PCA algorithms optimized for various scenarios:

- **`svd`**: Standard SVD-based PCA (fastest, complete data only)
- **`nipals`**: NIPALS algorithm (handles missing values effectively)
- **`rnipals`**: Robust NIPALS (resistant to outliers and missing values)
- **`ppca`**: Probabilistic PCA (classical probabilistic model)
- **`bpca`**: Bayesian PCA (probabilistic approach with uncertainty quantification)
- **`svd_impute`**: SVD-based PCA with missing value imputation
- **`rpca`**: Robust PCA using iterative outlier detection
- **`nlpca`**: Non-linear PCA using autoencoder neural network architecture

## Quick Start

```python
import torch
from pytorch_pca import pca

# Generate sample data
X = torch.randn(100, 20)

# Basic PCA using the unified interface
result = pca(X, method="svd", n_components=5)
print(f"Transformed data shape: {result.transformed_data.shape}")
print(f"Components shape: {result.components.shape}")
print(f"Explained variance: {result.explained_variance_ratio}")

# Alternative: use pcaMethods-style naming
print(f"Scores shape: {result.scores.shape}")
print(f"Loadings shape: {result.loadings.shape}")

# Handle missing data with NIPALS
X_missing = X.clone()
X_missing[10:20, 5:10] = float('nan')
result = pca(X_missing, method="nipals", n_components=3)

# Robust PCA for data with outliers
result = pca(X, method="rpca", n_components=3)

# Probabilistic approaches
result = pca(X, method="ppca", n_components=3)
result = pca(X, method="bpca", n_components=3)

# Non-linear PCA with neural networks
result = pca(X, method="nlpca", n_components=3)

# Reconstruct data
X_reconstructed = result.reconstruct(n_components=3)
```

## API Reference

### Unified Interface

```python
result = pca(data, method="svd", n_components=2, center=True, scale=False, **kwargs)
```

### Method-Specific Parameters

#### NIPALS Methods
```python
nipals(data, max_iter=1000, tol=1e-6, ...)
rnipals(data, max_iter=1000, tol=1e-6, ...)
```

#### Probabilistic Methods
```python
ppca(data, max_iter=1000, tol=1e-6, ...)
bpca(data, max_iter=1000, tol=1e-6, ...)
```

#### Robust PCA
```python
rpca(data, max_iter=100, tol=1e-6, ...)
```

#### Non-linear PCA
```python
nlpca(data, hidden_dims=[10, 5], max_iter=1000, lr=0.01, ...)
```

### Result Object

The `PCAResult` object provides:

- `transformed_data`: Data projected onto principal components `(n_samples, n_components)`
- `components`: Principal components (eigenvectors) `(n_components, n_features)`
- `eigenvalues`: Eigenvalues of the covariance matrix
- `explained_variance_ratio`: Proportion of variance explained by each component
- `method`: Name of the method used
- `scores`: Alias for `transformed_data` (pcaMethods compatibility)
- `loadings`: Transposed components (pcaMethods compatibility)
- `reconstruct(n_components=None)`: Reconstruct data using selected components

## Method Selection Guide

- **Complete data, speed priority**: `svd`
- **Missing values**: `nipals` or `svd_impute`
- **Outliers present**: `rpca` or `rnipals`
- **Uncertainty quantification**: `bpca`
- **Probabilistic modeling**: `ppca`
- **Non-linear relationships**: `nlpca`

## Dependencies

- **torch**: The only required dependency (>= 2.7.1)

## Development & Testing

- **black**: Code formatting
- **flake8**: Code linting
- **mypy**: Type checking
- **pytest**: Test runner
- **scikit-learn**: For test comparisons
- **setuptools**, **setuptools-scm**: Packaging

Comprehensive tests are provided in the `tests/` directory, covering all algorithms, edge cases, and robust/non-linear PCA scenarios.

## Testing

```bash
# Run all tests from root
pytest ./tests
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Author

**Ricardo Yanzon** - [ricayanzon](https://github.com/ricayanzon)

## Acknowledgments

- Inspired by the R package [pcaMethods](https://github.com/bioc/pcaMethods)
- Built with [PyTorch](https://pytorch.org/)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytorch-pca",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "PCA, principal component analysis, pytorch, torch, svd pca, SVD, Singular Value Decomposition, NIPALS, Nonlinear Iterative Partial Least Squares, Non-linear Iterative Partial Least Squares, RNIPALS, robust NIPALS, bpca, Bayesian PCA, ppca, Probabilistic PCA, SVD Impute, Iterative PCA, Robust PCA, Nonlinear PCA, Non-linear PCA",
    "author": null,
    "author_email": "Ricardo Yanzon <ricayanzon@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/a7/28/d377b1335abd413edc48fd4d52880735857d900e5d70d1992ea1939d677a/pytorch_pca-0.1.3.tar.gz",
    "platform": null,
    "description": "# PyTorch PCA\n\nA comprehensive PCA implementation using PyTorch, inspired by the R package [pcaMethods](https://github.com/bioc/pcaMethods).\n\n## Overview\n\nThis package provides a unified interface to eight PCA algorithms, all accessible via the `pca` function. The main entry point is `pytorch_pca.pca`, and the package exposes `pca`, `PCAResult`, and `AllowedMethod` in its public API.\n\n[![Release](https://img.shields.io/github/v/tag/ricayanzon/pytorch_pca?label=Pypi&logo=pypi&logoColor=yellow)](https://pypi.org/project/pytorch_pca/)\n![PythonVersion](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13-informational)\n![PytorchVersion](https://img.shields.io/badge/pytorch-2.7.1-blue)\n\n## Installation\n\n```bash\npip install pytorch_pca\n```\n\n## Features\n\nThis package provides 8 different PCA algorithms optimized for various scenarios:\n\n- **`svd`**: Standard SVD-based PCA (fastest, complete data only)\n- **`nipals`**: NIPALS algorithm (handles missing values effectively)\n- **`rnipals`**: Robust NIPALS (resistant to outliers and missing values)\n- **`ppca`**: Probabilistic PCA (classical probabilistic model)\n- **`bpca`**: Bayesian PCA (probabilistic approach with uncertainty quantification)\n- **`svd_impute`**: SVD-based PCA with missing value imputation\n- **`rpca`**: Robust PCA using iterative outlier detection\n- **`nlpca`**: Non-linear PCA using autoencoder neural network architecture\n\n## Quick Start\n\n```python\nimport torch\nfrom pytorch_pca import pca\n\n# Generate sample data\nX = torch.randn(100, 20)\n\n# Basic PCA using the unified interface\nresult = pca(X, method=\"svd\", n_components=5)\nprint(f\"Transformed data shape: {result.transformed_data.shape}\")\nprint(f\"Components shape: {result.components.shape}\")\nprint(f\"Explained variance: {result.explained_variance_ratio}\")\n\n# Alternative: use pcaMethods-style naming\nprint(f\"Scores shape: {result.scores.shape}\")\nprint(f\"Loadings shape: {result.loadings.shape}\")\n\n# Handle missing data with NIPALS\nX_missing = X.clone()\nX_missing[10:20, 5:10] = float('nan')\nresult = pca(X_missing, method=\"nipals\", n_components=3)\n\n# Robust PCA for data with outliers\nresult = pca(X, method=\"rpca\", n_components=3)\n\n# Probabilistic approaches\nresult = pca(X, method=\"ppca\", n_components=3)\nresult = pca(X, method=\"bpca\", n_components=3)\n\n# Non-linear PCA with neural networks\nresult = pca(X, method=\"nlpca\", n_components=3)\n\n# Reconstruct data\nX_reconstructed = result.reconstruct(n_components=3)\n```\n\n## API Reference\n\n### Unified Interface\n\n```python\nresult = pca(data, method=\"svd\", n_components=2, center=True, scale=False, **kwargs)\n```\n\n### Method-Specific Parameters\n\n#### NIPALS Methods\n```python\nnipals(data, max_iter=1000, tol=1e-6, ...)\nrnipals(data, max_iter=1000, tol=1e-6, ...)\n```\n\n#### Probabilistic Methods\n```python\nppca(data, max_iter=1000, tol=1e-6, ...)\nbpca(data, max_iter=1000, tol=1e-6, ...)\n```\n\n#### Robust PCA\n```python\nrpca(data, max_iter=100, tol=1e-6, ...)\n```\n\n#### Non-linear PCA\n```python\nnlpca(data, hidden_dims=[10, 5], max_iter=1000, lr=0.01, ...)\n```\n\n### Result Object\n\nThe `PCAResult` object provides:\n\n- `transformed_data`: Data projected onto principal components `(n_samples, n_components)`\n- `components`: Principal components (eigenvectors) `(n_components, n_features)`\n- `eigenvalues`: Eigenvalues of the covariance matrix\n- `explained_variance_ratio`: Proportion of variance explained by each component\n- `method`: Name of the method used\n- `scores`: Alias for `transformed_data` (pcaMethods compatibility)\n- `loadings`: Transposed components (pcaMethods compatibility)\n- `reconstruct(n_components=None)`: Reconstruct data using selected components\n\n## Method Selection Guide\n\n- **Complete data, speed priority**: `svd`\n- **Missing values**: `nipals` or `svd_impute`\n- **Outliers present**: `rpca` or `rnipals`\n- **Uncertainty quantification**: `bpca`\n- **Probabilistic modeling**: `ppca`\n- **Non-linear relationships**: `nlpca`\n\n## Dependencies\n\n- **torch**: The only required dependency (>= 2.7.1)\n\n## Development & Testing\n\n- **black**: Code formatting\n- **flake8**: Code linting\n- **mypy**: Type checking\n- **pytest**: Test runner\n- **scikit-learn**: For test comparisons\n- **setuptools**, **setuptools-scm**: Packaging\n\nComprehensive tests are provided in the `tests/` directory, covering all algorithms, edge cases, and robust/non-linear PCA scenarios.\n\n## Testing\n\n```bash\n# Run all tests from root\npytest ./tests\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Author\n\n**Ricardo Yanzon** - [ricayanzon](https://github.com/ricayanzon)\n\n## Acknowledgments\n\n- Inspired by the R package [pcaMethods](https://github.com/bioc/pcaMethods)\n- Built with [PyTorch](https://pytorch.org/)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Comprehensive PyTorch implementation of PCA.",
    "version": "0.1.3",
    "project_urls": {
        "GitHub": "https://github.com/ricayanzon/pytorch_pca",
        "Issues": "https://github.com/ricayanzon/pytorch_pca/issues"
    },
    "split_keywords": [
        "pca",
        " principal component analysis",
        " pytorch",
        " torch",
        " svd pca",
        " svd",
        " singular value decomposition",
        " nipals",
        " nonlinear iterative partial least squares",
        " non-linear iterative partial least squares",
        " rnipals",
        " robust nipals",
        " bpca",
        " bayesian pca",
        " ppca",
        " probabilistic pca",
        " svd impute",
        " iterative pca",
        " robust pca",
        " nonlinear pca",
        " non-linear pca"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0c41c4ff30a1895ab35705072b0b7228740212903dfa149d70814a55d76e5d68",
                "md5": "503bb5b4cd177a46db884145447cd035",
                "sha256": "21ecb9c79e358626b337a4597578d9eb57e6532ccea0921d859737b0a5d2694d"
            },
            "downloads": -1,
            "filename": "pytorch_pca-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "503bb5b4cd177a46db884145447cd035",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 19244,
            "upload_time": "2025-07-15T21:36:54",
            "upload_time_iso_8601": "2025-07-15T21:36:54.299690Z",
            "url": "https://files.pythonhosted.org/packages/0c/41/c4ff30a1895ab35705072b0b7228740212903dfa149d70814a55d76e5d68/pytorch_pca-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a728d377b1335abd413edc48fd4d52880735857d900e5d70d1992ea1939d677a",
                "md5": "ba33c42900f11667fb4f6ebbecb8dbb4",
                "sha256": "0fbfb20a283093a8d5460bd5e932ae32c1ee47034cf38da8d588f0a7da6ba1a6"
            },
            "downloads": -1,
            "filename": "pytorch_pca-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "ba33c42900f11667fb4f6ebbecb8dbb4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 24969,
            "upload_time": "2025-07-15T21:36:55",
            "upload_time_iso_8601": "2025-07-15T21:36:55.460457Z",
            "url": "https://files.pythonhosted.org/packages/a7/28/d377b1335abd413edc48fd4d52880735857d900e5d70d1992ea1939d677a/pytorch_pca-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-15 21:36:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ricayanzon",
    "github_project": "pytorch_pca",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "torch",
            "specs": [
                [
                    "==",
                    "2.7.1"
                ]
            ]
        }
    ],
    "lcname": "pytorch-pca"
}
        
Elapsed time: 0.80817s