Chapter 1 of ?
python 13 min read

Python for Data Science — Chapter 17: Numpy 2D Arrays

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$).

2D Axes & Dimensions Visualizer

Click the highlight buttons to see how Axis 0 (rows) and Axis 1 (columns) map to coordinates in lists versus tables.

a = [
  [11, 12, 13],
  [21, 22, 23],
  [31, 32, 33]
]
11 12 13
21 22 23
31 32 33
Layout Inspector ConsoleAttributes
>>> Click an Axis button to inspect dimensions.

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.

Interactive Slicing & Indexing Grid

Click cells directly on the table to inspect their indexing coordinate code, or select predefined slice options to watch coordinates highlight on the grid.

Slicing CodeReturned Value
>>> Click a cell or select a slice to check outputs.
Matrix Indexing Grid (A)
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) or A @ 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 (@).

Matrix Operations Playground
SIZE:

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.

Matrix A
+
Matrix B
=
Result C
Step-by-Step Calculation Log
>>> Select an operation to visualize math matrix calculations.

Practice Quiz

Validate your understanding of 2D dimensions, row-column coordinate index structures, and matrix dot products.

1. For a matrix A created from a nested list containing 3 lists of size 4, what is the value of A.ndim and A.shape?
2. Which syntax correctly extracts the first two elements of the first row from a 2D array?
3. What is the fundamental requirement to multiply matrix A by matrix B (matrix multiplication)?
Chapter 16
Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *