Weighted Principal Component Analysis
Project description
A scikit-learn compatible implementation of Weighted Principal Component Analysis
Overview
WeightedPCA is an extension to scikit-learn that implements Weighted Principal Component Analysis. It follows the scikit-learn API conventions, making it a drop-in replacement for sklearn.decomposition.PCA when you need to assign different weights to your samples.
This is a simplified implementation that supports sample-wise (row) weighting only. Each sample can have a different weight, but all features within a sample share the same weight. For full element-wise weighting (where each individual measurement can have its own weight), see the wpca package, which implements the complete Delchambre (2014) algorithm.
This package is not part of scikit-learn, but is designed to be compatible with the scikit-learn ecosystem:
Following scikit-learn’s fit/transform/fit_transform API
Installation
pip install weightedpca
Or install from source:
git clone https://github.com/byoungj/weightedpca.git
cd weightedpca
pip install -e .
Quick Start
import numpy as np
from weightedpca import WeightedPCA
# Your data
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
# Sample weights (e.g., based on data quality or importance)
weights = np.array([1.0, 1.0, 2.0, 2.0])
# Fit weighted PCA
wpca = WeightedPCA(n_components=2)
wpca.fit(X, sample_weight=weights)
# Transform data
X_transformed = wpca.transform(X)
# Or use fit_transform
X_transformed = wpca.fit_transform(X, sample_weight=weights)
Usage with scikit-learn Pipelines
WeightedPCA can be used in pipelines. Note that sample_weight should be applied during the WeightedPCA fitting step before the pipeline:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from weightedpca import WeightedPCA
# Fit WeightedPCA separately with sample weights
wpca = WeightedPCA(n_components=10)
X_reduced = wpca.fit_transform(X, sample_weight=weights)
# Then use pipeline for downstream processing
pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', LogisticRegression())
])
pipeline.fit(X_reduced, y)
Comparison with Standard PCA
from sklearn.decomposition import PCA
from weightedpca import WeightedPCA
import numpy as np
X = np.random.randn(100, 10)
weights = np.random.uniform(0.5, 1.5, 100)
# Standard PCA (unweighted)
pca = PCA(n_components=5)
X_pca = pca.fit_transform(X)
# Weighted PCA
wpca = WeightedPCA(n_components=5)
X_wpca = wpca.fit_transform(X, sample_weight=weights)
print(f"Standard PCA explained variance: {pca.explained_variance_ratio_}")
print(f"Weighted PCA explained variance: {wpca.explained_variance_ratio_}")
When to Use Weighted PCA
Weighted PCA is useful when:
Class Imbalance: Weight samples to balance class representation
Importance Differs: Certain observations should have more influence
Data Quality Varies: Some samples are more reliable than others
Uncertainty Quantification: Use inverse variance as weights
Temporal Data: Weight recent observations more heavily
Relationship to scikit-learn
This package is an independent extension to scikit-learn, not part of the core library. We follow scikit-learn’s API design principles, coding conventions, and documentation standards.
However, we are not affiliated with or endorsed by the scikit-learn project. For the official scikit-learn PCA implementation, see sklearn.decomposition.PCA.
License
This project is licensed under the BSD 3-Clause License - the same license as scikit-learn. See the LICENSE file for details.
References
Delchambre, L. (2014). “Weighted Principal Component Analysis: A Weighted Covariance Eigendecomposition Approach” arXiv:1412.4533
wpca package (full element-wise implementation): https://github.com/jakevdp/wpca
scikit-learn documentation: https://scikit-learn.org
Project details
Release history Release notifications | RSS feed
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 weightedpca-0.1.0.tar.gz.
File metadata
- Download URL: weightedpca-0.1.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
defb04fd764810fd9833eddbb608d4a453a53804ebf49762457bb80b4acb3895
|
|
| MD5 |
c17b412de4a61091ebe0be8b41a65b9e
|
|
| BLAKE2b-256 |
3e67af37641b4587dcf9f44e9414b762dd8b7250c1efc5ed5e5490d94a880fd4
|
File details
Details for the file weightedpca-0.1.0-py3-none-any.whl.
File metadata
- Download URL: weightedpca-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb860db0d34f10c059d21dffb87c2750e8fcf732d4926475835e5512bb946fde
|
|
| MD5 |
70a588295779e84b81b6586ecd137b01
|
|
| BLAKE2b-256 |
ac59365f3bba3f2b157998824815617dedf21e5ad69f58f8f9fda85c5d28d7db
|