Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eigenvectors created by numpy.linalg.eig don't seem correct

I create an arbitrary 2x2 matrix:

In [87]: mymat = np.matrix([[2,4],[5,3]])

In [88]: mymat
Out[88]: 
matrix([[2, 4],
        [5, 3]])

I attempt to calculate eigenvectors using numpy.linalg.eig:

In [91]: np.linalg.eig(mymat)
Out[91]: 
(array([-2.,  7.]),
 matrix([[-0.70710678, -0.62469505],
        [ 0.70710678, -0.78086881]]))

In [92]: eigvec = np.linalg.eig(mymat)[1][0].T

In [93]: eigvec
Out[93]: 
matrix([[-0.70710678],
        [-0.62469505]])

I multiply one of my eigenvectors with my matrix expecting the result to be a vector that is a scalar multiple of my eigenvector.

In [94]: mymat * eigvec
Out[94]: 
matrix([[-3.91299375],
        [-5.40961905]])

However it is not. Can anyone explain to me what is going wrong here?

like image 577
Selah Avatar asked Oct 03 '15 19:10

Selah


People also ask

What does the Numpy Linalg Eig () function return?

linalg. eigh. Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.

How does Numpy Linalg EIG work?

In NumPy we can compute the eigenvalues and right eigenvectors of a given square array with the help of numpy. linalg. eig(). It will take a square array as a parameter and it will return two values first one is eigenvalues of the array and second is the right eigenvectors of a given square array.

How do you normalize eigenvectors?

Normalized Eigenvector It can be found by simply dividing each component of the vector by the length of the vector. By doing so, the vector is converted into the vector of length one.


1 Answers

From the documentation for linalg.eig:

v : (..., M, M) array
The normalized (unit "length") eigenvectors, such that the column v[:,i] is the eigenvector corresponding to the eigenvalue w[i].

You want the columns, not the rows.

>>> mymat = np.matrix([[2,4],[5,3]])
>>> vals, vecs = np.linalg.eig(mymat)
>>> vecs[:,0]
matrix([[-0.70710678],
        [ 0.70710678]])
>>> (mymat * vecs[:,0])/vecs[:,0]
matrix([[-2.],
        [-2.]])
>>> vecs[:,1]
matrix([[-0.62469505],
        [-0.78086881]])
>>> (mymat * vecs[:,1])/vecs[:,1]
matrix([[ 7.],
        [ 7.]])
like image 116
DSM Avatar answered Oct 14 '22 18:10

DSM