# HG changeset patch # User James Bergstra # Date 1289012787 14400 # Node ID 26644a775a0d0bf33d826ce9feb5c08be1bd18c7 # Parent ba8a32b71356fe67a43417c6083bfe4b55f65d1c pca - added some assertions diff -r ba8a32b71356 -r 26644a775a0d pylearn/preprocessing/pca.py --- 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