Chapter 10: NumPy Array Shape
NumPy Array Shape — written exactly like a patient teacher sitting next to you, showing examples on the screen, drawing little diagrams with words, explaining the meaning behind the numbers, and showing the most common real-world patterns.
Let’s go step by step.
|
0 1 2 3 4 5 6 |
import numpy as np |
1. What does .shape actually mean?
.shape is the single most important property of a NumPy array.
It tells you:
How many elements are there along each dimension?
The number you see in .shape is always a tuple.
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
a = np.array([10, 20, 30, 40]) # 1D array print(a.shape) # (4,) b = np.array([[1, 2, 3], [4, 5, 6]]) # 2D array print(b.shape) # (2, 3) c = np.zeros((4, 5, 6)) # 3D array print(c.shape) # (4, 5, 6) |
Think of .shape as the sizes of each axis, from outermost to innermost.
2. How to read .shape — the real meaning
Common dimensions people use:
- 1D array → like a vector or list shape = (n,) → n elements in a row
- 2D array → like a matrix or table shape = (rows, columns) → rows first, columns second
- 3D array → most common interpretations:
- (batch_size, height, width) → images without color
- (batch_size, height, width, channels) → RGB images
- (time_steps, rows, columns) → time series of matrices
- (samples, timesteps, features) → time series data for ML
3. Visual way to understand shape (very helpful)
Let’s draw with words:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
1D shape: (7,) [ 0 1 2 3 4 5 6 ] ← just one row 2D shape: (3, 4) ← 3 rows × 4 columns [ 0 1 2 3 ] [ 4 5 6 7 ] [ 8 9 10 11 ] 3D shape: (2, 3, 4) ← 2 blocks × 3 rows × 4 columns Block 0: [ 0 1 2 3 ] [ 4 5 6 7 ] [ 8 9 10 11 ] Block 1: [12 13 14 15 ] [16 17 18 19 ] [20 21 22 23 ] |
So shape (2,3,4) means:
- 2 → number of “layers” or “blocks” or “samples”
- 3 → number of rows in each block
- 4 → number of columns in each row
4. The most important properties together
|
0 1 2 3 4 5 6 7 8 9 10 11 |
x = np.random.randint(0, 100, size=(4, 5, 6)) print(x.shape) # (4, 5, 6) ← dimensions print(x.ndim) # 3 ← how many axes print(x.size) # 120 ← total number of elements (4×5×6) print(len(x)) # 4 ← length of first dimension (same as shape[0]) |
Quick rule:
|
0 1 2 3 4 5 6 |
x.size == product of all numbers in x.shape |
5. Very common real-world shape examples (2025 style)
Machine Learning / Deep Learning
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Tabular data X.shape → (10000, 25) # 10,000 samples, 25 features # Images (very frequent) single_image.shape → (256, 256, 3) # height × width × RGB channels batch_of_images.shape → (32, 224, 224, 3) # 32 images of 224×224 pixels grayscale_batch.shape → (64, 128, 128) # 64 grayscale images 128×128 # Time series / sequences stock_prices.shape → (1000,) # 1000 days multivariate_ts.shape → (1000, 8) # 1000 timesteps, 8 features sequence_data.shape → (5000, 30, 64) # 5000 samples, 30 timesteps each, 64 features |
Scientific / Image processing
|
0 1 2 3 4 5 6 7 |
volume = np.zeros((128, 128, 128)) # 3D medical image (MRI/CT slice stack) video = np.zeros((300, 720, 1280, 3)) # 300 frames × 720p video |
6. Changing shape — the most used operations
.reshape() — most important method
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
a = np.arange(24) # shape (24,) print(a.reshape(4, 6)) # → (4,6) print(a.reshape(3, 8)) # → (3,8) print(a.reshape(2, 3, 4)) # → (2,3,4) # Very common trick: -1 means "automatically calculate this dimension" print(a.reshape(-1, 8)) # → (3, 8) print(a.reshape(4, -1)) # → (4, 6) |
Important rule about reshape:
You can only reshape if the total number of elements stays the same (new shape product == old shape product)
.ravel() / .flatten() — make everything 1D
|
0 1 2 3 4 5 6 7 8 9 |
img = np.random.rand(128, 128, 3) # shape (128,128,3) flat = img.ravel() # usually view → very fast flat_copy = img.flatten() # always copy print(flat.shape) # (49152,) |
Transpose — change order of axes
|
0 1 2 3 4 5 6 7 8 9 10 11 |
mat = np.arange(12).reshape(3,4) print(mat.T.shape) # (4,3) image = np.random.rand(64, 64, 3) # H,W,C image_trans = image.transpose(2,0,1) # C,H,W (very common in deep learning) print(image_trans.shape) # (3,64,64) |
7. Common beginner mistakes with shape
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Mistake 1: forgetting that shape is a tuple x = np.zeros((3,4)) print(x.shape[0]) # correct: 3 print(x.shape(0)) # TypeError! ← don't use () here # Mistake 2: reshape with wrong number of elements a = np.arange(10) a.reshape(3,4) # ValueError: cannot reshape array of size 10 into shape (3,4) # Mistake 3: thinking reshape copies data (most cases it doesn't) b = a.reshape(2,5) b[0,0] = 999 print(a) # a also changed! (view) |
Summary – Shape Quick Reference
| Question | Answer / Example |
|---|---|
| How many dimensions? | x.ndim |
| Size of each dimension? | x.shape |
| Total elements? | x.size |
| First/last dimension size? | x.shape[0], x.shape[-1] |
| Make it 1D quickly? | x.ravel() or x.flatten() |
| Change dimensions order? | x.reshape(…), x.transpose(…) |
| Automatic dimension? | reshape(-1, 8) or reshape(4, -1) |
| Typical image shape? | (height, width, 3) or (batch, height, width, 3) |
| Typical ML table shape? | (n_samples, n_features) |
Would you like to continue with any of these next?
- Very common reshape patterns in machine learning
- Difference between reshape / ravel / flatten / view vs copy
- How shape works in 4D+ arrays (video, batches of volumes…)
- Debugging shape mismatch errors
- Mini-exercise: reshape images / time series together
Just tell me what feels most useful right now! 😊
