Python Matrix
A matrix is a two-dimensional array-like structure in Python that is used extensively for mathematical computation, data representation, or even image processing. Python does not have a native matrix type; however, a matrix can be implemented using lists or specialized libraries like NumPy.
We can store strings, integers, and objects of other data types in a matrix. Data is stored in the stacks of rows and columns in a matrix. The matrix is a crucial data structure for calculations in mathematics and science. In Python, we consider a list of lists or a nested list as a matrix since Python doesn’t include any built-in type for a matrix object.
1. Representing a Matrix in Python
Using Lists:
A 3×3 matrix:
matrix = [
[1, 2, 3], # Row 0
[4, 5, 6], # Row 1
[7, 8, 9] # Row 2
]
- Accessing Elements: Use indexing to access elements. Python lists are zero-indexed.
element = matrix[1][2] # Row 1, Column 2 (value: 6)
print(element)
- Row and Column Count:
rows = len(matrix) # Number of rows
columns = len(matrix[0]) # Number of columns
print(f"Rows: {rows}, Columns: {columns}")
2. Traversing a Matrix
Traversal involves visiting each element systematically. Use nested loops:
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for i in range(len(matrix)): # Outer loop for rows
for j in range(len(matrix[0])): # Inner loop for columns
print(matrix[i][j], end=" ") # Access elements
print() # New line after each row
Output:
1 2 3
4 5 6
7 8 9
3. Common Matrix Operations
Addition
Matrix addition is done element-wise. Both matrices must have the same dimensions.
In Python:
A = [
[1, 2],
[3, 4]
]
B = [
[5, 6],
[7, 8]
]
result = [[A[i][j] + B[i][j] for j in range(len(A[0]))] for i in range(len(A))]
print(result)
Output:
[[6, 8], [10, 12]]
Transpose
The transpose of a matrix switches rows and columns.
In Python:
matrix = [
[1, 2, 3],
[4, 5, 6]
]
transpose = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print(transpose)
Output:
[[1, 4], [2, 5], [3, 6]]
Multiplication
Matrix multiplication is different from element-wise multiplication. The formula is:
In Python:
A = [
[1, 2],
[3, 4]
]
B = [
[5, 6],
[7, 8]
]
result = [[sum(A[i][k] * B[k][j] for k in range(len(B))) for j in range(len(B[0]))] for i in range(len(A))]
print(result)
Output:
[[19, 22], [43, 50]]
4. Using NumPy for Efficiency
Introduction to NumPy
NumPy is a powerful library to perform numerical computations. It offers extensive tools for dealing with matrices efficiently and performing operations.
Install it:
pip install numpy
Basic Operations
import numpy as np
A = np.array([
[1, 2],
[3, 4]
])
B = np.array([
[5, 6],
[7, 8]
])
# Addition
print(A + B)
# Transpose
print(A.T)
# Matrix Multiplication
print(np.dot(A, B)) # or use A @ B
# Inverse
print(np.linalg.inv(A))
5. Applications of Matrices
- Linear Algebra:
- Solve systems of linear equations: Ax=B.
Solve using np.linalg.solve(A, B).
2. Image Processing:
- Images are arrays of pixel values (grayscale) or three arrays for RGB.
3. Data Science:
- Matrices represent datasets, where rows = data points, columns = features.
4. Graphs:
- Represent graphs using adjacency matrices.