changeset 962:0fee974dca1d

work on pca file
author James Bergstra <bergstrj@iro.umontreal.ca>
date Fri, 20 Aug 2010 09:29:30 -0400
parents 4e0982189fc9
children 06f21a964bd8
files pylearn/preprocessing/pca.py
diffstat 1 files changed, 12 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/preprocessing/pca.py	Fri Aug 20 09:28:54 2010 -0400
+++ b/pylearn/preprocessing/pca.py	Fri Aug 20 09:29:30 2010 -0400
@@ -71,7 +71,7 @@
 
 
 def pca_from_examples(X, max_components=None, max_energy_fraction=None, x_centered=False):
-    """Return (eigvals, eigvecs) of observations `X` (1-per-row)
+    """Return (eigvals, eigvecs), centered_X of observations `X` (1-per-row)
 
     This function exists to wrap several algorithms for getting the principle components.
 
@@ -91,10 +91,10 @@
     else:
         centered_X = X - numpy.mean(X, axis=0)
     return pca_from_cov( numpy.cov(centered_X.T), max_components=max_components,
-            max_energy_fraction=max_energy_fraction)
+            max_energy_fraction=max_energy_fraction), centered_X
 
 
-def pca_whiten(X, pca):
+def pca_whiten((eigvals, eigvecs), centered_X,eps=1e-8):
     """
     Return the projection of X onto it's principle components.  
     
@@ -102,22 +102,18 @@
     of principle components.  Columns of the return value have mean 0, variance 1, and are
     uncorrelated.
 
-
-    See also fft_whiten.py
-
     :param pca: the (w,v) pair returned by e.g. pca_from_examples(X)
 
     """
-    w,v = pca
-
-    centered_X = X - numpy.mean(X, axis=0)
-    eigvals, eigvecs = pca_from_examples(centered_X, 
-            max_components=max_components, max_energy_fraction=max_energy_fraction,
-            x_centered=True)
+    pca_of_X = numpy.dot(centered_X, eigvecs)
+    pca_of_X /= numpy.sqrt(eigvals)+eps
+    return pca_of_X
 
-    rotated_X = numpy.dot(centered_X, eigvecs)
-    rotated_X /= numpy.sqrt(eigvals)
+def zca_whiten((eigvals, eigvecs), centered_X):
+    """Return the PCA of X but rotated back into the original vector space.
 
-    return rotated_X
+    See also fft_whiten.py
+    """
+    pca_of_X = pca_whiten((eigvals,eigvecs), centered_X)
+    return numpy.dot(pca_of_X, eigvecs.T)
 
-