Matrices are fundamental in mathematical computations, especially in areas like linear algebra, machine learning, and engineering. In this tutorial, you’ll learn important NumPy operations for matrix mathematics, including matrix multiplication, transposition, inverse calculation, determinants, and solving linear systems.
1. Matrix Operations
NumPy provides easy-to-use functions for common matrix operations such as matrix multiplication and transposition.
Matrix Multiplication (np.dot())
The np.dot()
function performs matrix multiplication between two compatible arrays.
Syntax:
numpy.dot(matrix_a, matrix_b)
matrix_a: This is the first NumPy array, like a table of hours worked. For example, matrix_a = np.array([[2, 3], [4, 1]])—rows are employees (Alice, Bob), columns are days (Monday, Tuesday).
matrix_b: This is the second NumPy array, like a table of points per day. For example, matrix_b = np.array([[5, 10], [8, 2]])—rows are days (Monday, Tuesday), columns are projects (X, Y).
numpy.dot(matrix_a, matrix_b) multiplies and sums these arrays to combine them. It matches each row of matrix_a with each column of matrix_b, calculating totals—like Alice’s points for Project X: (2 × 5) + (3 × 8) = 34. The result is a new array, like [[34, 26], [28, 42]], showing points per employee per project. The number of columns in matrix_a must equal the number of rows in matrix_b for it to work.
Example:
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix multiplication
result = np.dot(A, B)
print("Matrix Multiplication Result:\n", result)
Output:
Matrix Multiplication Result:
[[19 22]
[43 50]]
Transpose Matrix (np.transpose())
The np.transpose()
function flips a matrix over its diagonal, switching rows and columns.
Syntax:
numpy.transpose(matrix)
matrix: This is the matrix you want to flip.
Example:
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Transpose of the matrix
transposed = np.transpose(matrix)
print("Transposed Matrix:\n", transposed)
Output:
Transposed Matrix:
[[1 4]
[2 5]
[3 6]]
2. Inverse Matrix and Determinant
NumPy offers functions for advanced mathematical operations such as finding inverse matrices and determinants using the np.linalg
module.
Inverse Matrix (np.linalg.inv())
The inverse of a matrix is found using the np.linalg.inv()
function. An inverse matrix multiplies with the original matrix to give the identity matrix.
Example:
matrix = np.array([[4, 7], [2, 6]])
# Inverse matrix
inv_matrix = np.linalg.inv(matrix)
print("Inverse Matrix:\n", inv_matrix)
Output:
Inverse Matrix:
[[ 0.6 -0.7]
[-0.2 0.4]]
Determinant (np.linalg.det())
The determinant of a matrix is a scalar value computed using np.linalg.det()
. It helps in determining matrix invertibility.
matrix = np.array([[4, 7], [2, 6]])
# Determinant of the matrix
det_matrix = np.linalg.det(matrix)
print("Determinant:", det_matrix)
Output:
Determinant: 10.000000000000002
3. Solving Linear Equations (np.linalg.solve())
NumPy simplifies solving systems of linear equations using the np.linalg.solve()
function.
For equations of the form Ax = B, you can solve for x easily.
Example:
# Matrix A and result vector B
A = np.array([[3, 1], [1, 2]])
B = np.array([9, 8])
# Solving linear equations
solution = np.linalg.solve(A, B)
print("Solution of the system:", solution)
Output:
Solution of the system: [2. 3.]
Summary Table
Function | Description |
---|---|
np.dot() | Matrix multiplication |
np.transpose() | Matrix transposition |
np.linalg.inv() | Inverse matrix calculation |
np.linalg.det() | Matrix determinant calculation |
np.linalg.solve() | Solve systems of linear equations |