algorithms for large-scale matrix factorization
Many common matrix factorization algorithms can benefit from parallelization. This package provides distributed implementations of PCA, ICA, NMF, and others that target the distributed computing engine spark. It also wraps local implementations from scikit-learn, to support factorization in both a local and distributed setting with a common API.
The package includes a collection of algorithms that can be fit to data, all of which return matrices with the results of the factorization. Compatible with Python 2.7+ and 3.4+. Built on numpy, scipy, and scikit-learn. Works well alongside thunder and supprts parallelization via spark, but can also be used on local numpy arrays.
Use pip
pip install thunder-factorization
Here's an example computing PCA
# create high-dimensional low-rank matrix
from sklearn.datasets import make_low_rank_matrix
X = make_low_rank_matrix(n_samples=100, n_features=100, effective_rank=5)
# use PCA to recover low-rank structure
from factorization import PCA
algorithm = PCA(k=5)
T, W_T = algorithm.fit(X)All algorithms have a fit method with fits the algorithm and returns the components of the factorization.
Input
Xdata matrix as anumpyndarray, aboltarray, orthunderseriesorimagesdatareturn_parallelwhether or not to keep the output parallelized, only valid if the input matrix is already parallelized viaboltorthunder, default isFalsemeaning thta all returned arrays will be local
Output
- Two or more arrays representing the estimated factors.
 
Here are all the available algorithms with their options.
Factors the matrix into statistically independent sources X = S * A. Note: it is the columns of S that represent the independent sources, linear combinations of which reconstruct the columns of X.
Parameters
- 
knumber of sources - 
max_itermaximum number of iterations - 
toltolerance for stopping iterations - 
seedseed for random number generator that initializes algorithm - 
k_pcanumber of principal components used for initial dimensionality reduction, default is no dimensionality reduction (sparkmode only) - 
svd_methodhow to compute the distributed SVD'auto','direct', or'em', see SVD documentation for details (sparkmode only) 
Output
Ssources, shapenrows x kAmixing matrix, shapek x ncols
Factors a non-negative matrix as the product of two small non-negative matrices X = W * H.
Parameters
knumber of componentsmax_itermaximum number of iterationstoltolerance for stopping iterationsseedseed for random number generator that initializes algorithm
Output
Wleft factor, shapenrows x kHright factor, shapek x ncols
Performs dimensionality reduction by finding an ordered set of components formed by an orthogonal projection that successively explain the maximum amount of remaining variance T = X * W^T
Parameters
knumber of componentsmax_itermaximum number of iterationstoltolerance for stopping iterationsseedseed for random number generator that initializes algorithm.svd_methodhow to compute the distributed SVD'auto','direct', or'em', see SVD documentation for details (sparkmode only)
Output
Tcomponents, shape(nrows, k)Wweights, shape(k, ncols)
Generalization of the eigen-decomposition for non-square matrices X = U * diag(S) * V.
Parameters
- 
knumber of components - 
max_itermaximum number of iterations - 
toltolerance for stopping iterations - 
seedseed for random number generator that initializes algorithm. - 
svd_methodhow to compute the distributed SVD (sparkmode only)'direct'explicit computation based eigenvalue decomposition of the covariance matrix.'em'approximate iterative method based on expectation-maximization algorithm.'auto'usesdirectforncols< 750, otherwise usesem.
 
Output
Uleft singular vectors, shape(nrows, k)Ssingular values, shape(k,)Vright singular vectors, shape(k, ncols)
All numpy array and bolt array inputs to factorization algorithms must be two-dimensional. Thunder images and series can also be factored, even though they technically have more than two dimensions, because in each case one dimension is treated as primary. For images, each image will be flattened, creating a two-dimensional matrix with shape (key,pixels), and for series, the keys will be flattened, creating a two-dimensional matrix with shape (key,time). After facorization, outputs will be reshaped back to their original form.
Run tests with
py.testTests run locally with numpy by default, but the same tests can be run against a local spark installation using
py.test --engine=spark