In SciPy, we calculate the eigenvector and eigenvalue of a given matrix by using the eig function in scipy.linalg.
Using the following code, let us look at calculating the eigenvector and the corresponding eigenvalue of a given matrix:
- Initialize a matrix:
a = np.array([[1, 2], [3, 4]])
- Calculate the eigenvalue and eigenvector of the matrix:
la, v = linalg.eig(a)
- The output of the preceding code is:
print(la)
[-0.37228132+0.j 5.37228132+0.j]
print(v)
[[-0.82456484 -0.41597356] [ 0.56576746 -0.90937671]]
Note that la is the eigenvalue and the v matrix is the eigenvector.
Let us cross-check the preceding output based on the intuition we laid out at the start of this section.
The eigenvector for a given matrix A satisfies the following criterion:
The following applies:
- A is the matrix
- v is the eigenvector
- λ is the eigenvalue
Let us cross-check the output based on the intuition we laid out earlier.
- Calculate the value of the matrix multiplication of A•v:
np.dot(a,v)
array([[ 0.30697009, -2.23472698], [-0.21062466, -4.88542751]])
- Calculate the value of the matrix multiplication of λ v:
la*v
array([[ 0.30697009-0.j, -2.23472698+0.j],
[-0.21062466+0.j, -4.88542751+0.j]])
Note that the output satisfies the initially laid out equation: A•v = λ v.