Recommended Free Tools
NumPy’s numpy.linalg module handles the core linear-algebra tasks in Python: matrix products, equation solving, least squares, decompositions, eigenvalues, and matrix diagnostics. Choose an operation from the shape and structure of your data: use solve for a square system, lstsq for a least-squares fit, and pinv when you specifically need a Moore–Penrose pseudoinverse.
What NumPy provides for linear algebra
The numpy.linalg module brings common linear-algebra operations to NumPy arrays, including products, decompositions, eigenvalue calculations, norms, determinants, ranks, condition numbers, equation solving, inverses, and pseudoinverses. Its low-level implementations rely on BLAS and LAPACK, libraries that provide standard numerical algorithms. See the NumPy linear algebra reference for the supported functions.
For ordinary matrix work, use standard two-dimensional numpy.ndarray arrays. The @ operator expresses matrix multiplication clearly and is preferable for products between two-dimensional arrays; NumPy implements it with numpy.matmul.
import numpy as np
A = np.array([[3.0, 1.0], [1.0, 2.0]])
b = np.array([9.0, 8.0])
product = A @ A
Do not build new code around numpy.matrix: NumPy no longer recommends that class, including for linear algebra. Use arrays and explicit operators instead. See NumPy array types.
#1 Best Overall
How to choose a solver
The right routine depends on the mathematical problem—not just on wanting a numerical answer. Check whether the coefficient array is square or rectangular, whether the system is over- or under-determined, and whether you need a least-squares solution or a particular generalized inverse.
| Goal | Use | Shape or condition | What it returns |
|---|---|---|---|
Solve a direct system Ax = b |
np.linalg.solve(A, b) |
A must be square; the system must be solvable by the routine. |
The solution x. |
| Find a least-squares solution | np.linalg.lstsq(A, b, rcond=None) |
Suitable for rectangular, overdetermined, or underdetermined systems. | A solution, residual information, the effective rank, and singular values. |
| Construct the Moore–Penrose pseudoinverse | np.linalg.pinv(A) |
Useful when the generalized inverse itself is required, including rank-deficient cases. | The pseudoinverse matrix. |
Use solve for a square system
For a square coefficient matrix and a direct system, call solve rather than computing inv(A) @ b. The latter explicitly forms an inverse when the goal is only to find x. Use inv only when the inverse matrix itself is needed for a further operation.
Rank #2
x = np.linalg.solve(A, b)
Use lstsq for fitting or rectangular systems
Least squares finds a solution that minimizes the residual between A @ x and b, which is useful when equations outnumber unknowns, measurements are noisy, or a rectangular system has no exact solution. With rcond=None, NumPy applies its documented default cutoff for small singular values. Inspect the returned rank and singular values when diagnosing whether the data matrix is effectively rank-deficient.
x, residuals, rank, singular_values = np.linalg.lstsq(A, b, rcond=None)
Residual output depends on the matrix shape and rank; it is not always a populated array. Consult the lstsq reference when code depends on its exact return behavior.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsRank #3
Use pinv when the pseudoinverse is the object you need
The pseudoinverse generalizes the inverse to matrices that may be rectangular or rank-deficient. It is valuable in some estimation and inverse problems, but it is not a universal replacement for solve or lstsq. If your aim is to solve a least-squares problem, lstsq states that goal directly and also reports rank and singular-value information.
Choose a decomposition that matches the matrix structure
QR for orthogonal-factor methods
QR factorization writes a matrix as a product of an orthogonal (or unitary) factor and an upper-triangular factor. It is useful in numerical methods that exploit that structure, including some least-squares algorithms.
Cholesky for positive-definite matrices
Cholesky factorization applies when the matrix has the required positive-definite structure (and is symmetric in real-valued problems or Hermitian in complex-valued ones). It can be an efficient choice when that condition is guaranteed; it is not a general decomposition for arbitrary square matrices.
SVD for singular values and low-rank structure
The singular value decomposition (SVD) factors a matrix into left singular vectors, singular values, and right singular vectors. Singular values help reveal effective rank and low-rank structure, and can inform compression or approximation choices. Do not treat rank as a purely visual property: results can depend on a numerical threshold for small singular values.
Best Value
- NumPy is perfect for data scientists and engineers using Python. NumPy powers machine learning, financial modeling, and AI development. NumPy is essential for data analysis, physics research, big data processing in tech, and science research analytics
- NumPy offers mathematical functions, random number generators, linear algebra routines, Fourier transforms. NumPy Python library adds support for large multi-dimensional arrays and matrices, with high-level mathematical functions to operate on these arrays
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
U, s, Vh = np.linalg.svd(A, full_matrices=False)
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Eigenvalues and matrix diagnostics
Match eigenvalue routines to the matrix
- Use
eigoreigvalsfor a general square array when you need eigenvectors or eigenvalues, respectively. - Use
eighoreigvalshfor symmetric or Hermitian arrays; these routines use the matrix’s structure.
eigenvalues, eigenvectors = np.linalg.eigh(A)
Use diagnostics to understand scale and solvability
normmeasures a vector or matrix norm, according to the selected norm.condestimates a condition number, helping assess how sensitive a problem may be to perturbations.matrix_rankestimates rank numerically, so its result can depend on a tolerance.detcomputes a determinant, but a determinant alone is not a reliable test of numerical conditioning.
These quantities answer different questions. A small determinant does not by itself establish that a matrix is unusable, and a computed rank should be understood in light of numerical tolerance and scale.
Matrix multiplication and other products
For two-dimensional matrix multiplication, use A @ B. Other operations serve distinct contraction patterns: dot and multi_dot cover dot products and chains of matrix products; inner and outer compute different vector or array products; tensordot contracts specified axes; and einsum expresses indexed contractions. Choose based on the intended axes and output shape rather than treating these functions as interchangeable.
Run the same operation over a stack of matrices
Many NumPy linear-algebra routines accept stacks of matrices. In an input shaped like (..., M, N), the final two dimensions represent each matrix, while preceding dimensions identify the stack. For example, an array shaped (batch, M, N) represents batch matrices, each with M rows and N columns. Vector dimensions follow the routine’s own rules.
# A_stack has shape (batch, M, M)
# b_stack has shape (batch, M)
x_stack = np.linalg.solve(A_stack, b_stack)
print(x_stack.shape) # (batch, M)
Confirm the selected function supports the shapes and broadcasting pattern you intend, then verify the output shape. The NumPy linear algebra reference documents these conventions and individual function behavior.
Free tools Windows power users keep installed
One-click scans. No signup required.
When SciPy is a better fit
NumPy is a strong first choice for standard array-based operations and batched calculations. Use scipy.linalg when the problem calls for additional tools such as LU or Schur decompositions, matrix transcendental functions, or generalized eigenvalue problems. Some overlapping functions also have augmented functionality in SciPy, while NumPy can offer more flexible broadcasting for certain operations. Compare the specific function and input-shape requirements before switching; the SciPy linear algebra reference describes its broader toolbox.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

