changeset 1450:c421bac46a97

added inplace option for pca_from_examples
author James Bergstra <bergstrj@iro.umontreal.ca>
date Thu, 31 Mar 2011 18:29:02 -0400
parents 22f48914b3f8
children 8110ca3cec3f
files pylearn/preprocessing/pca.py
diffstat 1 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/preprocessing/pca.py	Thu Mar 31 18:28:07 2011 -0400
+++ b/pylearn/preprocessing/pca.py	Thu Mar 31 18:29:02 2011 -0400
@@ -72,7 +72,8 @@
     return w,v
 
 
-def pca_from_examples(X, max_components=None, max_energy_fraction=None, x_centered=False):
+def pca_from_examples(X, max_components=None, max_energy_fraction=None,
+        x_centered=False, inplace=False):
     """Return ((eigvals, eigvecs), centered_X) of observations `X` (1-per-row)
 
     This function exists to wrap several algorithms for getting the principle components.
@@ -90,10 +91,11 @@
     :returns: ((eigvals, eigvecs), centered_X) of PCA decomposition
 
     """
-    if x_centered:
-        centered_X = X
-    else:
-        centered_X = X - numpy.mean(X, axis=0)
+    if not inplace:
+        X = X.copy()
+    centered_X = X
+    if not x_centered:
+        centered_X -= numpy.mean(centered_X, axis=0)
     cov_X = numpy.dot(centered_X.T, centered_X) / (len(X)- 1)
     evals, evecs = pca_from_cov(cov_X, max_components=max_components,
             max_energy_fraction=max_energy_fraction)