1D vs 2D vs 3D Arrays
Understand dimensions with cricket data: one player's scores (1D), the whole team (2D), and two seasons (3D) — how to build, read and index each.
A 1D array is one player's scores across matches. But real data is usually a table (players × matches), and some data has even more layers (seasons × players × matches, or an image: height × width × colour). NumPy handles all of these with the same tools — you just add dimensions.
Definition
The dimension count (
ndim) of an array is the number of indices you need to locate one element. A 1D array is a sequence (vector) indexed by[i]; a 2D array is a table (matrix) of rows and columns indexed by[row, col]; a 3D array is a stack of 2D tables indexed by[block, row, col]. In general an N-D array needs N indices.
- Vector — a 1D array: shape
(n,). - Matrix — a 2D array: shape
(rows, cols). - Tensor — the general term (used in ML) for arrays of 3 or more dimensions.
- Index per dimension — NumPy uses one bracket with commas:
arr[1, 2], notarr[1][2](which also works but is slower and less clear).
Overview
| 1D | 2D | 3D | |
|---|---|---|---|
| Picture | a row of numbers | a table | a stack of tables |
| Cricket meaning | one player's 14 matches | 3 players × 14 matches | 2 seasons × 3 players × 8 matches |
| Shape | (14,) | (3, 14) | (2, 3, 8) |
| ndim | 1 | 2 | 3 |
| Index | a[match] | a[player, match] | a[season, player, match] |
| Brackets when printed | [ ... ] | [[ ... ]] | [[[ ... ]]] |
| Other examples | a time series | spreadsheet, grayscale image | colour image (H × W × 3), video frame batch |
1D — a single player's scores
python
import numpy as np
virat_runs = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93, 28, 66])
print(virat_runs)
print("Shape:", virat_runs.shape)
print(virat_runs[0], virat_runs[-1], virat_runs[:5])text
[ 72 45 88 31 64 0 103 52 19 77 41 93 28 66]
Shape: (14,)
72 66 [72 45 88 31 64]One index picks one match. Indexing and slicing work exactly like Python lists.
2D — the whole team's scores
Rows = players, columns = matches:
text
M1 M2 M3 M4 ... M14
Virat row 0: 72 45 88 31 ... 66
Rohit row 1: 56 0 74 88 ... 49
Hardik row 2: 45 32 0 67 ... 27python
import numpy as np
team_scores = np.array([
[72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93, 28, 66],
[56, 0, 74, 88, 32, 45, 61, 17, 90, 38, 72, 5, 83, 49],
[45, 32, 0, 67, 18, 54, 78, 22, 41, 33, 60, 15, 88, 27],
])
print(team_scores)
print("Shape:", team_scores.shape)
print(team_scores[0, 0], team_scores[1, 2], team_scores[2, -1])
print(team_scores[0])
print(team_scores[:, 0])text
[[ 72 45 88 31 64 0 103 52 19 77 41 93 28 66]
[ 56 0 74 88 32 45 61 17 90 38 72 5 83 49]
[ 45 32 0 67 18 54 78 22 41 33 60 15 88 27]]
Shape: (3, 14)
72 74 27
[ 72 45 88 31 64 0 103 52 19 77 41 93 28 66]
[72 56 45]How it works:
team_scores[0, 0]= Virat, match 1 = 72.[1, 2]= Rohit, match 3 = 74.[2, -1]= Hardik, last match = 27.team_scores[0]with one index returns a whole row — Virat's season (a 1D array).team_scores[:, 0]—:means "all rows", so this is column 0: every player's match-1 score.
3D — two seasons of data
A 3D array is a stack of 2D tables. Here: 2 seasons, each a 3-player × 8-match table.
text
season 0 (2023) season 1 (2024)
+--------------------------+ +--------------------------+
Virat | 72 45 88 31 64 0 103 52 | | 65 80 44 91 33 58 22 71 |
Rohit | 56 0 74 88 32 45 61 17 | | 39 55 82 0 77 21 64 48 |
Hardik | 45 32 0 67 18 54 78 22 | | 28 44 63 37 82 0 55 71 |
+--------------------------+ +--------------------------+
shape = (2 seasons, 3 players, 8 matches)python
import numpy as np
two_seasons = np.array([
[[72, 45, 88, 31, 64, 0, 103, 52],
[56, 0, 74, 88, 32, 45, 61, 17],
[45, 32, 0, 67, 18, 54, 78, 22]],
[[65, 80, 44, 91, 33, 58, 22, 71],
[39, 55, 82, 0, 77, 21, 64, 48],
[28, 44, 63, 37, 82, 0, 55, 71]],
])
print(two_seasons)
print(two_seasons.shape, two_seasons.ndim, two_seasons.size)
print(two_seasons[0, 0, 0], two_seasons[1, 1, 2])
print(two_seasons[0, :, 0])
print(two_seasons[:, 0, :])text
[[[ 72 45 88 31 64 0 103 52]
[ 56 0 74 88 32 45 61 17]
[ 45 32 0 67 18 54 78 22]]
[[ 65 80 44 91 33 58 22 71]
[ 39 55 82 0 77 21 64 48]
[ 28 44 63 37 82 0 55 71]]]
(2, 3, 8) 3 48
72 82
[72 56 45]
[[ 72 45 88 31 64 0 103 52]
[ 65 80 44 91 33 58 22 71]]How it works: the index order matches the shape order: [season, player, match].
[0, 0, 0]→ season 1, Virat, match 1 = 72.[1, 1, 2]→ season 2, Rohit, match 3 = 82.[0, :, 0]→ season 1, all players, match 1 =[72 56 45].[:, 0, :]→ all seasons, Virat, all matches → a 2×8 table of Virat's two seasons.- The blank line in the printout separates the two 2D blocks; size = 2 × 3 × 8 = 48.
Reading a printed array
Count the opening brackets at the start: [ → 1D, [[ → 2D, [[[ → 3D. The innermost brackets are always the last dimension (here: matches).
Common mistakes
- ❌ Mixing up the index order. ✅ Index in the same order as the shape: for
(2, 3, 8)it's[season, player, match]. - ❌ Thinking
arr[0]on a 2D array returns one number. ✅ It returns the whole first row. - ❌ Using
arr[1][2]everywhere. ✅ Preferarr[1, 2]— one lookup, and needed for slicing likearr[:, 0](arr[:][0]gives row 0, not column 0!). - ❌ Building a 2D array from rows of different lengths. ✅ Every row must be the same length.
Interview tip
Q: A colour image has shape (480, 640, 3). What does each dimension mean and how many values does it hold?
A: 480 rows (height) × 640 columns (width) × 3 colour channels (R, G, B). It is a 3D array with 480 × 640 × 3 = 921,600 values, and img[0, 0] is the RGB triple of the top-left pixel.
Key points to remember
- ndim is the number of indices needed to reach one element: 1D [i], 2D [row, col], 3D [block, row, col].
- 1D = one player's scores (vector), 2D = whole team table (matrix), 3D = stack of tables, e.g. seasons × players × matches.
- Index in the same order as the shape; use commas inside one bracket: arr[1, 2].
- arr[0] on a 2D array returns a whole row; arr[:, 0] returns a whole column.
- In 3D, the colon selects everything along that dimension: two_seasons[:, 0, :] = Virat in all seasons.
- Count the opening brackets of a printed array to know its dimensions.
Try it yourself
1D → 2D → 3D side by side
Indexing the team table
Indexing two seasons (3D)
Practice: Hardik across seasons
Revision Flashcards
Tap a card to flip it and see the answer.
