Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I have a large, sparse Matrix of features I would like to use in a machine learning algorithm:

library(Matrix)
set.seed(42)
rows <- 500000
cols <- 10000
i <- unlist(lapply(1:rows, function(i) rep(i, sample(1:5,1))))
j <- sample(1:cols, length(i), replace=TRUE)
M <- sparseMatrix(i, j)

Because this matrix has many columns, I would like to reduce it's dimensonality to something more manageable. I can use the excellent irlba package to perform SVD and return the first n principle components (5 shown here; I'll probably use 100 or 500 on my actual dataset):

library(irlba)
pc <- irlba(M, nu=5)$u

However, I've read that prior to performing PCA, one should center the matrix (subtract the column mean from each column). This is very difficult to do on my dataset, and furthermore would destroy the sparsity of the matrix.

How "bad" is it to perform SVD on the un-scaled data, and feed it straight into a machine learning algorithm? Are there any efficient ways I could scale this data, while preserving the sparsity of the matrix?

share|improve this question

1 Answer

First of all, you really do want to center the data. If not, the geometric interpretation of PCA shows that the first principal component will be close to the vector of means and all subsequent PCs will be orthogonal to it, which will prevent them from approximating any PCs that happen to be close to that first vector. We can hope that most of the later PCs will be approximately correct, but the value of that is questionable when it's likely the first several PCs--the most important ones--will be quite wrong.

So, what to do? PCA proceeds by means of a singular value decomposition of the matrix $X$. The essential information will be contained in $X X'$, which in this case is a $10000$ by $10000$ matrix: that may be manageable. Its computation involves about 50 million calculations of dot products of one column with the next.

Consider any two columns, then, $Y$ and $Z$ (each one of them is a $500000$-vector; let this dimension be $n$). Let their means be $m_Y$ and $m_Z$, respectively. What you want to compute is, writing $\mathbf{1}$ for the $n$-vector of $1$'s,

$$(Y - m_Y\mathbf{1}) \cdot (Z - m_Z\mathbf{1}) = Y\cdot Z - m_Z\mathbf{1}\cdot Y - m_Y\mathbf{1}.Z + m_Z m_Y \mathbf{1}\cdot \mathbf{1}\\ = Y\cdot Z -n (m_Ym_Z),$$

because $m_Y = \mathbf{1}\cdot Y / n$ and $m_Z = \mathbf{1}\cdot Z/n$.

This allows you to use sparse matrix techniques to compute $X X'$, whose entries provide the values of $Y\cdot Z$, and then adjust its coefficients based on the $10000$ column means. The adjustment shouldn't hurt, because it seems unlikely $X X'$ will be very sparse.

share|improve this answer
Thank you for the detailed answer. One of the advantages of irlba is that you can specify nu to limit the algorithm to the first n principle components, which greatly increases it's efficacy and (I think) bypasses the calculation of the XX' matrix. – Zach Aug 27 '12 at 20:50
But what do you want to work with? A sparse $10000$ by $500000$ matrix with $5\times 10^9$ coefficients that does not represent the problem you need to solve, or a $10000$ by $10000$ with $10^8$ coefficients that does represent the problem you want to solve? irlba can be applied to the latter to obtain just the first few principal components, anyway, so you get the best of both worlds. – whuber Aug 27 '12 at 20:54
I suppose the latter. =). So I need to calculate the dot product for each pair of columns in my sparse matrix, subtract the colMeans of the sparse matrix from the dot product matrix, then run irlba on the result? – Zach Aug 27 '12 at 21:11
Almost: notice you're subtracting products of column means, not the column means themselves. Your formulation of the algorithm otherwise is excellent, because although abstractly you're computing $X X'$, you don't really want R to create $X'$ in order to do the matrix multiplication. Instead, if RAM is really limited, you can perform the column dot products in batches by reading in subsets of the columns at a time. It would be wise to experiment with much smaller matrices at first :-). – whuber Aug 27 '12 at 21:20
what exactly do you mean by "products of column means?" Lets say I have a vector of column means... what now? – Zach Aug 28 '12 at 0:51
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.