Written and maintained by CASRAI Editorial Board
Published
On this page
numpy.linalg.eig(a) computes the eigenvalues and right eigenvectors of a square array and returns them as a named tuple, eigenvalues first and eigenvectors second. The eigenvalues are not sorted, they are complex unless every imaginary part is zero, and column i of the eigenvector array pairs with eigenvalue i, so that a @ v[:, i] equals w[i] * v[:, i]. This guide covers the call, how to check the result, and when a symmetric matrix should go to eigh instead.
What numpy.linalg.eig is used for in research
Eigen-decomposition appears wherever a research dataset is summarised by a square matrix: covariance and correlation matrices in principal component analysis, adjacency matrices in network analysis, transition matrices in Markov models and Jacobians in stability analysis of dynamical systems. In each case the eigenvalues give the size of a mode (variance explained, growth rate, steady-state weight) and the eigenvectors give its direction. For the statistics behind the covariance case see the guide to a principal component analysis plot and the explanation of the scree plot; for the same operation in MATLAB see eigenvalue in MATLAB.
The NumPy reference describes eig as the general-purpose routine: it accepts any square array, symmetric or not, and it is built on the LAPACK routine _geev. That generality has a cost, because the output is allowed to be complex and unordered. Most mistakes with eig come from forgetting one of those two facts.
Install or access
NumPy is installed from the official site, numpy.org/install. The page gives pip install numpy for pip and conda install numpy for conda, and recommends working inside a virtual environment (for example one created with python -m venv my-env). To confirm the install, run:
import numpy as np
print(np.__version__)
eig lives in the linalg submodule, so after the import above it is called as np.linalg.eig. The reference page used for this guide is the documentation for NumPy v2.5; the signature is numpy.linalg.eig(a).
Step-by-step: eigenvalues and eigenvectors of a matrix
- Build a square array. The argument
ahas shape (…, M, M). The leading dimensions are optional: if you pass a stack of matrices, eig broadcasts over it and returns one set of eigenvalues per matrix. - Call the function and unpack the result. The return value is a named tuple with fields
eigenvaluesandeigenvectors, so both of these forms work:import numpy as np a = np.array([[2.0, 1.0], [1.0, 3.0]]) result = np.linalg.eig(a) w, v = result.eigenvalues, result.eigenvectors # or: w, v = np.linalg.eig(a) - Pair the right column with the right value. The eigenvector for
w[i]is the columnv[:, i], not the rowv[i]. Each eigenvector is normalised to unit length. - Check the result. Verify the defining equation with a tolerance rather than exact equality:
np.allclose(a @ v, v * w) # True when every column satisfies a v = w vSee the guide to numpy.allclose for how the tolerances work.
On the matrix above, a run with NumPy 2.5.4 returned eigenvalues of approximately 1.382 and 3.618, and the allclose check returned True. The values were printed as complex numbers with a zero imaginary part (dtype complex128), which is why step 5 below matters.
Output and how to read it
Order of the eigenvalues
The NumPy reference states that the eigenvalues are not necessarily ordered. Do not assume ascending or descending order. When order matters, for example when you want the leading principal component, sort explicitly with np.argsort(w.real)[::-1] and apply the same index to the columns of v.
Complex versus real output
According to the documentation, the eigenvalue array is complex unless the imaginary parts are all zero, in which case it is cast to a real type, and when a is real the eigenvalues are either real or occur in complex-conjugate pairs. The reference example for the real matrix [[1, -1], [1, 1]] returns the eigenvalues 1+1j and 1-1j, a conjugate pair. If your own run returns a complex dtype with zero imaginary parts, as the 2.5.4 run above did for a symmetric matrix, check w.dtype and take w.real (or np.real_if_close(w)) before plotting or sorting.
Eigenvector normalisation, sign and phase
Eigenvectors are returned with unit length, but their sign (and, for complex input, their phase) is not fixed. The reference marks the eigenvectors of its Hermitian example as “may vary”, so two NumPy builds can legitimately return v[:, 0] and -v[:, 0]. Compare eigenvectors across runs through the residual check or through absolute values, never by exact equality.
Reading the reference examples
The data table below lists the worked examples printed in the NumPy documentation, including a round-off case: for the matrix with diagonal entries 1 + 10-9 and 1 – 10-9, eig returns 1 and 1 although the theoretical eigenvalues are 1 ± 10-9. The lesson is that eigenvalues closer together than the floating-point error of the computation cannot be distinguished.
Left eigenvectors and reconstruction
eig returns right eigenvectors only. Left eigenvectors y satisfy y.T @ a = z * y.T and are not simply the transposes of the right ones. If all eigenvalues are distinct, the eigenvectors are linearly independent and inv(v) @ a @ v is diagonal, which gives a second check: np.allclose(np.linalg.inv(v) @ a @ v, np.diag(w)). The documentation also warns that the eigenvectors may not have maximal rank, in which case that diagonalisation does not hold.
Worked example: a covariance matrix
This is an illustrative example with made-up numbers, not data from a study. Suppose three measured variables have the covariance matrix C below. A covariance matrix is real and symmetric, which is exactly the case for which NumPy provides eigh. The documentation describes eigh as the routine for Hermitian or real symmetric input: it reads only one triangle of the array (the lower triangle by default, or the upper with UPLO='U'), returns real eigenvalues in ascending order, and returns unitary eigenvectors.
import numpy as np
C = np.array([[4.0, 2.0, 0.6],
[2.0, 3.0, 0.4],
[0.6, 0.4, 1.0]])
w, v = np.linalg.eigh(C) # ascending eigenvalues
order = np.argsort(w)[::-1] # make it descending
w, v = w[order], v[:, order]
explained = w / w.sum()
print(np.allclose(C @ v, v * w)) # residual check
In a local run with NumPy 2.5.4 the eigenvalues in descending order were approximately 5.672, 1.444 and 0.884, giving explained-variance fractions of about 0.709, 0.180 and 0.110, and the residual check returned True. Calling eig on the same matrix returned the same three values as complex numbers with zero imaginary part, ordered 5.672, 1.444, 0.884 in that run, an order the documentation does not promise. For symmetric input, eigh is therefore the safer call: its output is real and its ordering is specified.
eig, eigh, eigvals and the SciPy version: which to call
The NumPy page lists eigvals, eigh, eigvalsh, scipy.linalg.eig and scipy.linalg.schur as related routines. The table below compares the three that were checked against their documentation pages.
| Function | Accepts | Eigenvalue output | Eigenvectors |
|---|---|---|---|
| numpy.linalg.eig | Any square array, with broadcasting over leading dimensions | Not necessarily ordered; complex unless imaginary parts are all zero | Right eigenvectors, unit length, columns of the second return value |
| numpy.linalg.eigh | Hermitian or real symmetric arrays; only one triangle is read, the other is ignored | Real, ascending order | Unitary |
| scipy.linalg.eig | Square a, optionally with b for the generalised problem a @ vr = w * b @ vr | Always complex | Right vectors vr normalised; with left=True, vl is returned and is not normalised |
Use eigh for covariance, correlation and other symmetric or Hermitian matrices. Use eig for general matrices such as transition or Jacobian matrices. Use scipy.linalg.eig when you need the generalised problem or left eigenvectors; its signature is eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False, check_finite=True, homogeneous_eigvals=False). Setting check_finite=False can speed the call up but, per the SciPy documentation, may crash or fail to terminate if the input contains infs or NaNs. For a normal but non-Hermitian matrix the NumPy documentation prefers scipy.linalg.schur, because eig does not guarantee unitary eigenvectors in that case. A sparse matrix is a different problem: see the guide to scipy.sparse.
Troubleshooting
LinAlgError: eigenvalues did not converge
The documentation says eig raises LinAlgError if the eigenvalue computation does not converge. The same applies to eigh and scipy.linalg.eig. Check the input for NaN or inf with np.isfinite(a).all() first, and consider rescaling very badly scaled columns before the call.
Complex numbers appear in the output
For a real non-symmetric matrix, complex-conjugate pairs are legitimate: they signal oscillatory behaviour, as in the rotation-like matrix [[1, -1], [1, 1]]. For a symmetric matrix they should have zero imaginary part; switch to eigh, or take .real after confirming np.abs(w.imag).max() is negligible.
eigh gives a wrong answer for a matrix that is not symmetric
eigh does not check symmetry. Because it reads only one triangle, a non-symmetric array passed to it is silently treated as if the other triangle mirrored the first. Test np.allclose(a, a.T) (or a.conj().T for complex input) before choosing eigh.
Eigenvectors differ between machines
Signs and phases are not unique, and the reference notes that results may vary. Compare with the residual check from the step-by-step section, not with exact equality.
Matrix is defective
If the matrix does not have a full set of independent eigenvectors the documentation notes that diagonalisation fails. In a local check, the matrix [[1, 1], [0, 1]] returned the eigenvalue 1 twice and an eigenvector matrix of rank 1, so inv(v) is not meaningful there. Use the Jordan or Schur form instead, for example scipy.linalg.schur.
Free alternatives
| Tool | Function | Use it when |
|---|---|---|
| NumPy | numpy.linalg.eigh | The matrix is symmetric or Hermitian and you want real, ascending eigenvalues |
| SciPy | scipy.linalg.eig | You need the generalised problem (b), left eigenvectors or the overwrite and check_finite options |
| SciPy | scipy.linalg.schur | The matrix is normal but not Hermitian, per the NumPy documentation |
| GNU Octave and MATLAB | eig | You work outside Python; use the built-in function |
The residual checks in this guide use numpy.linalg.norm equally well: np.linalg.norm(a @ v - v * w) should be close to zero relative to the norm of a.
Frequently asked questions
Does numpy.linalg.eig sort the eigenvalues?
No. The NumPy documentation says the eigenvalues are not necessarily ordered. Sort them yourself with argsort and reorder the eigenvector columns with the same index. eigh, by contrast, returns eigenvalues in ascending order.
Which column of the eigenvector array belongs to which eigenvalue?
Column i, written v[:, i], belongs to eigenvalue w[i], and satisfies a @ v[:, i] = w[i] * v[:, i]. The eigenvectors are normalised to unit length.
Why are my eigenvalues complex?
Either the matrix really has complex-conjugate eigenvalue pairs, as any real non-symmetric matrix can, or the eigenvalues are real but stored with a zero imaginary part. Inspect the imaginary part; if it is zero, take the real part or use eigh for symmetric input.
When should I use eigh instead of eig?
Use eigh when the matrix is real symmetric or complex Hermitian, such as a covariance matrix. It guarantees real, ascending eigenvalues and unitary eigenvectors, and reads only the lower triangle unless UPLO=’U’ is set.
Does eig return left or right eigenvectors?
Right eigenvectors only. For left eigenvectors use scipy.linalg.eig with left=True; the returned vl is not normalised.
Can eig handle a stack of matrices?
Yes. The input may have shape (…, M, M) and broadcasting applies, so the eigenvalue array has shape (…, M) and the eigenvector array (…, M, M).
Sources
- NumPy documentation, numpy.linalg.eig (v2.5): https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html, read 2026-11-14.
- NumPy documentation, numpy.linalg.eigh: https://numpy.org/doc/stable/reference/generated/numpy.linalg.eigh.html, read 2026-11-14.
- SciPy documentation, scipy.linalg.eig: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eig.html, read 2026-11-14.
- NumPy, Installing NumPy: https://numpy.org/install/, read 2026-11-14.
Data: Eigenvalues returned in the NumPy documentation examples
Worked examples printed on the NumPy reference pages for numpy.linalg.eig and numpy.linalg.eigh, tabulated by input matrix and function, with the eigenvalues the documentation shows and the property each example illustrates. Compiled from the two NumPy reference pages.
| Input matrix | Function | Eigenvalues shown | What the example shows |
|---|---|---|---|
| np.diag((1, 2, 3)) | numpy.linalg.eig | 1, 2, 3 | Diagonal input gives the diagonal entries; eigenvectors are the identity columns |
| [[1, -1], [1, 1]] | numpy.linalg.eig | 1+1j, 1-1j | A real matrix with a complex-conjugate pair |
| [[1, 1j], [-1j, 1]] | numpy.linalg.eig | 2+0j, 0+0j | Complex Hermitian input; eigenvectors may vary |
| [[1+1e-9, 0], [0, 1-1e-9]] | numpy.linalg.eig | 1, 1 | Round-off: theoretical eigenvalues are 1 +/- 1e-9 |
| [[1, -2j], [2j, 5]] | numpy.linalg.eigh | 0.17157288, 5.82842712 | Hermitian input; real eigenvalues in ascending order |
Sources
- NumPy documentation: numpy.linalg.eig (accessed 2026-11-14)
- NumPy documentation: numpy.linalg.eigh (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







