[Tutor] Help- Regarding python
Oscar Benjamin
oscar.j.benjamin at gmail.com
Tue Feb 5 16:27:24 CET 2013
On 5 February 2013 05:08, eryksun <eryksun at gmail.com> wrote:
> On Mon, Feb 4, 2013 at 7:21 PM, Oscar Benjamin
> <oscar.j.benjamin at gmail.com> wrote:
>> eigenvalues, eigenvectors = np.linalg.eig(C)
>
> First sort by eigenvalue magnitude:
>
> >>> idx = np.argsort(eigenvalues)[::-1]
> >>> print idx
> [ 0 1 2 3 8 10 11 12 14 22 20 21 18 19 23 24 17 16 15 13 9 7 5 6 4]
>
> >>> eigenvalues = eigenvalues[idx]
> >>> eigenvectors = eigenvectors[:, idx]
>
>> # 2D PCA - get the two eigenvectors with the largest eigenvalues
>> v1, v2 = eigenvectors[:,:2].T
Thanks. I thought that eig already sorted them. The doc claims says
that the values are "not necessarily ordered" but when I run it they
are in descending order of absolute value.
Also I should have used eigh since the covariance matrix is Hermitian
(eigh seems to give the eigenvalues in ascending order).
Oscar
More information about the Tutor
mailing list