Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38c6035

Browse files
committed
Update of mlab.pca - updated docstring, added saving the eigenvalues.
The old version of pca class did not save the Eigenvalues - just the "fractions" that are just percentage values. Also added info which row of Wt one can find the Eigenvectors.
1 parent 5497d47 commit 38c6035

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

lib/matplotlib/mlab.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -827,19 +827,23 @@ def __init__(self, a):
827827
828828
*numrows*, *numcols*: the dimensions of a
829829
830-
*mu* : a numdims array of means of a
830+
*mu* : a numdims array of means of a. This is the vector that points to the
831+
origin of PCA space.
831832
832-
*sigma* : a numdims array of atandard deviation of a
833+
*sigma* : a numdims array of standard deviation of a
833834
834835
*fracs* : the proportion of variance of each of the principal components
836+
837+
*s* : the actual eigenvalues of the decomposition
835838
836839
*Wt* : the weight vector for projecting a numdims point or array into PCA space
837840
838841
*Y* : a projected into PCA space
839842
840843
841844
The factor loadings are in the Wt factor, ie the factor
842-
loadings for the 1st principal component are given by Wt[0]
845+
loadings for the 1st principal component are given by Wt[0].
846+
This row is also the 1st eigenvector.
843847
844848
"""
845849
n, m = a.shape
@@ -856,15 +860,27 @@ def __init__(self, a):
856860

857861
U, s, Vh = np.linalg.svd(a, full_matrices=False)
858862

859-
860-
Y = np.dot(Vh, a.T).T
861-
862-
vars = s**2/float(len(s))
863-
self.fracs = vars/vars.sum()
864-
865-
863+
# Note: .H indicates the conjugate transposed / Hermitian.
864+
865+
# The SVD is commonly written as a = U s V.H.
866+
# If U is a unitary matrix, it means that it satisfies U.H = inv(U).
867+
868+
# The rows of Vh are the eigenvectors of a.H a.
869+
# The columns of U are the eigenvectors of a a.H.
870+
# For row i in Vh and column i in U, the corresponding eigenvalue is s[i]**2.
871+
866872
self.Wt = Vh
873+
874+
# save the transposed coordinates
875+
Y = np.dot(Vh, a.T).T
867876
self.Y = Y
877+
878+
# save the eigenvalues
879+
self.s = s**2
880+
881+
# and now the contribution of the individual components
882+
vars = self.s/float(len(s))
883+
self.fracs = vars/vars.sum()
868884

869885

870886
def project(self, x, minfrac=0.):

0 commit comments

Comments
 (0)