# HG changeset patch # User James Bergstra # Date 1282310970 14400 # Node ID 0fee974dca1d2638b56725e33652fe710f2bb3fd # Parent 4e0982189fc95ae50b6fd2c26855c9cad7919ec0 work on pca file diff -r 4e0982189fc9 -r 0fee974dca1d pylearn/preprocessing/pca.py --- 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) -