NumPy: why does np.linalg.eig and np.linalg.svd give different V values of SVD? NumPy: why does np.linalg.eig and np.linalg.svd give different V values of SVD? numpy numpy

NumPy: why does np.linalg.eig and np.linalg.svd give different V values of SVD?


For linalg.eig your Eigenvalues are stored in w. These are:

>>> warray([20., 80.])

For your singular value decomposition you can get your Eigenvalues by squaring your singular values (C is invertible so everything is easy here):

>>> s**2array([80., 20.])

As you can see their order is flipped.

From the linalg.eig documentation:

The eigenvalues are not necessarily ordered

From the linalg.svd documentation:

Vector(s) with the singular values, within each vector sorted in descending order. ...

In general routines that give you Eigenvalues and Eigenvectors do not "sort" them necessarily the way you might want them. So it is always important to make sure you have the Eigenvector for the Eigenvalue you want. If you need them sorted (e.g. by Eigenvalue magnitude) you can always do this yourself (see here: sort eigenvalues and associated eigenvectors after using numpy.linalg.eig in python).

Finally note that the rows in vh contain the Eigenvectors, whereas in v it's the columns.

So that means that e.g.:

>>> v[:,0].flatten()matrix([[-0.9486833 ,  0.31622777]])>>> vh[1].flatten()matrix([[ 0.9486833 , -0.31622777]])

give you both the Eigenvector for the Eigenvalue 20.