Matrices and 2D Arrays
Data science relies heavily on matrices to represent images, tabular features, and vectors. A **2D array** in NumPy extends the 1D structure into rows and columns. In this chapter, we will master 2D array coordinates, index slicing syntax, transposing, and both element-wise and linear algebra matrix multiplications.
17.1 Basics & Layout: Rank and Sizing
A 2D array is constructed by passing nested lists of equal size into np.array(). The resulting matrix has two axes: **Axis 0** represents rows, and **Axis 1** represents columns. The attribute ndim is always 2, and shape returns a tuple in the format (rows, columns).
import numpy as np
# Create a 2D matrix (3 rows, 3 columns)
a = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
A = np.array(a)
print("Dimensions (ndim):", A.ndim)
print("Shape:", A.shape)
Axes Coordinates: By convention, moving vertically down rows is Axis 0, and moving horizontally across columns is Axis 1. Multiplying rows by columns yields the total element size (e.g., $3 \times 3 = 9$).
Click the highlight buttons to see how Axis 0 (rows) and Axis 1 (columns) map to coordinates in lists versus tables.
[11, 12, 13],
[21, 22, 23],
[31, 32, 33]
]
| 11 | 12 | 13 |
| 21 | 22 | 23 |
| 31 | 32 | 33 |
17.2 Indexing and Slicing in 2D
To access an element, we use double brackets A[row][col] or single brackets with a comma separator: A[row, col]. Slicing works by applying ranges (using colons) to rows and columns independently.
import numpy as np
# A 3x3 array
A = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]])
# Access cell A[1, 2]
print("Cell (1,2):", A[1, 2])
# Slice first row, columns index 0 to 1
print("First row slice:", A[0, 0:2])
# Slice last two rows, column index 2
print("Last rows slice:\n", A[1:3, 2])
Try Slicing: Click "Try It Yourself" to run this indexing code. Slices return smaller sub-arrays by specifying row and column ranges.
Click cells directly on the table to inspect their indexing coordinate code, or select predefined slice options to watch coordinates highlight on the grid.
| 11 | 12 | 13 |
| 21 | 22 | 23 |
| 31 | 32 | 33 |
17.3 Basic Operations & Matrix Multiplication
Performing operations on 2D arrays differs based on operators:
- Addition / Subtraction: Adds elements at matching indices (matrix addition).
- Hadamard Product (
X * Y): Multiplies elements at matching coordinates (entrywise product). - Matrix Multiplication (
np.dot(A, B)orA @ B): Multiplies rows of A by columns of B. Columns of A must equal rows of B.
import numpy as np
X = np.array([[1, 2], [0, 1]])
Y = np.array([[0, 2], [1, 0]])
# 1. Element-wise Addition
print("Addition:\n", X + Y)
# 2. Hadamard (element-wise) Product
print("Hadamard Product:\n", X * Y)
# 3. Matrix Multiplication (Dot Product)
print("Matrix Multiplication:\n", X @ Y) # or np.dot(X, Y)
Play with Operations: Click "Try It Yourself" to run matrix arithmetic. Notice the difference between element-wise multiplication (*) and true matrix multiplication (@).
Edit values directly in the matrix cells, then click an operation to see step-by-step calculation log for every result cell — with highlighted source rows and columns.
Practice Quiz
Validate your understanding of 2D dimensions, row-column coordinate index structures, and matrix dot products.