changeset 1356:26644a775a0d

pca - added some assertions
author James Bergstra <bergstrj@iro.umontreal.ca>
date Fri, 05 Nov 2010 23:06:27 -0400
parents ba8a32b71356
children ffa2932a8cba
files pylearn/preprocessing/pca.py
diffstat 1 files changed, 12 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/preprocessing/pca.py	Fri Nov 05 13:29:07 2010 -0400
+++ b/pylearn/preprocessing/pca.py	Fri Nov 05 23:06:27 2010 -0400
@@ -40,12 +40,20 @@
     #  a * v[:,i] = w[i] * vr[:,i]
     #  v.H * v = identity
 
+    assert w.min() >= -1e-12 # assert w is all pretty much positive
+    if w.min() < 0:
+        for i,wi in enumerate(w):
+            if wi < 0:
+                w[i]=0
+
 
     # total variance can be computed at this point:
-    # note that vartot == d.sum()
-    vartot = diag_as_vector(cov).sum()
-
-    assert numpy.allclose(vartot, w.sum())
+    # note that vartot == w.sum()
+    vartot = w.sum()
+    if 0: 
+        # you can do this if you want, but it just slows things down
+        vartot_cov = diag_as_vector(cov).sum()
+        assert numpy.allclose(vartot_cov, vartot)
 
     a = numpy.argsort(w)[::-1]
 
@@ -68,8 +76,6 @@
             if i < len(w):
                 w = w[:i]
                 v = v[:,:i]
-
-
     return w,v