In NumPy, matrices are essentially two - dimensional arrays. The numpy.ndarray
object is used to represent these matrices. It stores homogeneous data (i.e., all elements have the same data type) in a contiguous block of memory, which allows for fast computation.
Matrices in NumPy have properties such as shape, which is a tuple indicating the number of rows and columns. For example, a matrix with shape (3, 2)
has 3 rows and 2 columns.
import numpy as np
# Create a 2D array (matrix)
matrix = np.array([[1, 2], [3, 4], [5, 6]])
print("Matrix:")
print(matrix)
print("Shape of the matrix:", matrix.shape)
In this code, we first import the numpy
library. Then we create a 2D array using np.array()
and pass a list of lists. Finally, we print the matrix and its shape.
# Create a matrix of zeros
zeros_matrix = np.zeros((3, 2))
print("Matrix of zeros:")
print(zeros_matrix)
# Create a matrix of ones
ones_matrix = np.ones((2, 3))
print("Matrix of ones:")
print(ones_matrix)
# Create a random matrix
random_matrix = np.random.rand(2, 2)
print("Random matrix:")
print(random_matrix)
In this code, we use np.zeros()
, np.ones()
, and np.random.rand()
to create matrices filled with zeros, ones, and random values respectively.
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix addition
C = A + B
print("Matrix addition:")
print(C)
# Matrix subtraction
D = A - B
print("Matrix subtraction:")
print(D)
Matrix addition and subtraction in NumPy are straightforward. We can use the +
and -
operators as long as the matrices have the same shape.
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Element - wise multiplication
element_wise = A * B
print("Element - wise multiplication:")
print(element_wise)
# Matrix multiplication
matrix_mult = np.dot(A, B)
print("Matrix multiplication:")
print(matrix_mult)
In NumPy, the *
operator performs element - wise multiplication. For matrix multiplication, we use np.dot()
. Note that for matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix.
A = np.array([[1, 2, 3], [4, 5, 6]])
transpose_A = A.T
print("Original matrix:")
print(A)
print("Transposed matrix:")
print(transpose_A)
The .T
attribute of a NumPy array is used to get the transpose of a matrix, which flips the rows and columns.
A = np.array([[1, 2], [3, 4]])
try:
inv_A = np.linalg.inv(A)
print("Inverse of the matrix:")
print(inv_A)
except np.linalg.LinAlgError:
print("The matrix is not invertible.")
The np.linalg.inv()
function is used to calculate the inverse of a matrix. However, a matrix must be square and non - singular (i.e., its determinant is non - zero) to be invertible.
LinAlgError
.shape
attribute to do this.NumPy provides a powerful and efficient way to perform matrix operations in Python. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, you can use NumPy effectively in various real - world applications such as data analysis, machine learning, and computer graphics. With its rich set of functions and high - performance implementation, NumPy is an essential tool for anyone working with numerical data.