Linear algebra with NumPy
Linear algebra is an important subdivision of mathematics. We can use linear algebra, for instance, to perform linear regression. The numpy.linalg
subpackage holds linear algebra routines. With this subpackage, you can invert matrices, compute eigenvalues, solve linear equations, and find determinants, among other things. Matrices in NumPy are represented by a subclass of ndarray
.
Inverting matrices with NumPy
The inverse of a square and invertible matrix A
in linear algebra is the matrix A-1
, which, when multiplied with the original matrix, is equal to the identity matrix I
. This can be written down as the following mathematical equation:
A A-1 = I
The inv()
function in the numpy.linalg
subpackage can do this for us. Let's invert an example matrix. To invert matrices, follow these steps:
- Create the demonstration matrix with the
mat()
function:A = np.mat("2 4 6;4 2 6;10 -4 18") print("A\n", A)
The
A
matrix is printed as follows...