Skip to content

Commit 3cefb7a

Browse files
committed
Merge pull request matplotlib#2239 from blackw1ng/patch-1
Update of mlab.pca - updated docstring, added saving the eigenvalues.
2 parents ad3eae2 + 38c6035 commit 3cefb7a

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

lib/matplotlib/mlab.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,19 +1668,23 @@ def __init__(self, a):
16681668
16691669
*numrows*, *numcols*: the dimensions of a
16701670
1671-
*mu* : a numdims array of means of a
1671+
*mu* : a numdims array of means of a. This is the vector that points to the
1672+
origin of PCA space.
16721673
1673-
*sigma* : a numdims array of atandard deviation of a
1674+
*sigma* : a numdims array of standard deviation of a
16741675
16751676
*fracs* : the proportion of variance of each of the principal components
1677+
1678+
*s* : the actual eigenvalues of the decomposition
16761679
16771680
*Wt* : the weight vector for projecting a numdims point or array into PCA space
16781681
16791682
*Y* : a projected into PCA space
16801683
16811684
16821685
The factor loadings are in the Wt factor, ie the factor
1683-
loadings for the 1st principal component are given by Wt[0]
1686+
loadings for the 1st principal component are given by Wt[0].
1687+
This row is also the 1st eigenvector.
16841688
16851689
"""
16861690
n, m = a.shape
@@ -1697,15 +1701,27 @@ def __init__(self, a):
16971701

16981702
U, s, Vh = np.linalg.svd(a, full_matrices=False)
16991703

1700-
1701-
Y = np.dot(Vh, a.T).T
1702-
1703-
vars = s**2/float(len(s))
1704-
self.fracs = vars/vars.sum()
1705-
1706-
1704+
# Note: .H indicates the conjugate transposed / Hermitian.
1705+
1706+
# The SVD is commonly written as a = U s V.H.
1707+
# If U is a unitary matrix, it means that it satisfies U.H = inv(U).
1708+
1709+
# The rows of Vh are the eigenvectors of a.H a.
1710+
# The columns of U are the eigenvectors of a a.H.
1711+
# For row i in Vh and column i in U, the corresponding eigenvalue is s[i]**2.
1712+
17071713
self.Wt = Vh
1714+
1715+
# save the transposed coordinates
1716+
Y = np.dot(Vh, a.T).T
17081717
self.Y = Y
1718+
1719+
# save the eigenvalues
1720+
self.s = s**2
1721+
1722+
# and now the contribution of the individual components
1723+
vars = self.s/float(len(s))
1724+
self.fracs = vars/vars.sum()
17091725

17101726

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

0 commit comments

Comments
 (0)