🧱 NumPy Array Shape – How to Understand and Use shape
in NumPy
🧲 Introduction – Why Learn Array Shape in NumPy?
The shape of a NumPy array defines its structure—how many dimensions it has and how many elements exist in each dimension. Understanding and manipulating the .shape
attribute is fundamental to working with arrays, especially in data preprocessing, machine learning, and scientific computing.
🎯 In this guide, you’ll learn:
- What
.shape
means and how to read it - How to access and change the shape of an array
- Shape-related functions like
reshape()
,ravel()
,flatten()
- Real-world scenarios and best practices for array shaping
📐 What Is the shape
of a NumPy Array?
The .shape
attribute returns a tuple showing the number of elements in each dimension of the array.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
👉 Output:
(2, 3)
✅ This means: 2 rows × 3 columns (2D array)
📦 Examples of Array Shapes
1D Array
arr1d = np.array([10, 20, 30])
print(arr1d.shape)
👉 Output:
(3,)
2D Array
arr2d = np.array([[1, 2], [3, 4]])
print(arr2d.shape)
👉 Output:
(2, 2)
3D Array
arr3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3d.shape)
👉 Output:
(2, 2, 2)
✅ It’s a cube: 2 blocks, each with 2 rows and 2 columns
🧰 Reshaping Arrays with reshape()
You can change the shape of an array using the reshape()
method.
arr = np.arange(6)
reshaped = arr.reshape((2, 3))
print(reshaped)
👉 Output:
[[0 1 2]
[3 4 5]]
⚠️ Rule: The total number of elements must remain the same.
🔄 Other Shape-Related Functions
ravel()
– Returns Flattened View
a = np.array([[1, 2], [3, 4]])
flat = a.ravel()
print(flat)
👉 Output:
[1 2 3 4]
flatten()
– Returns Flattened Copy
flat_copy = a.flatten()
🔁 ravel()
returns a view (shared memory), flatten()
returns a copy.
🧠 Shape and Dimensionality
Use .ndim
to get the number of dimensions:
print(arr3d.ndim) # Output: 3
Use .size
to get the total number of elements:
print(arr3d.size) # Output: 8
🚧 Common Mistakes to Avoid
- ❌ Changing shape without matching element count:
np.arange(10).reshape(3, 4) # Error! 10 ≠ 12
- ❌ Using incorrect shape format:
np.reshape(arr, 2, 3) # Wrong np.reshape(arr, (2, 3)) # ✅ Correct
📊 Shape Comparison Table
Shape Output | Interpretation |
---|---|
(5,) | 1D array with 5 elements |
(3, 4) | 2D array: 3 rows × 4 columns |
(2, 3, 4) | 3D array: 2 blocks of 3×4 |
(1, n) | Row vector |
(n, 1) | Column vector |
🔍 Summary – Key Takeaways
.shape
gives array dimensions as a tuple- Use
reshape()
to change shape without altering data ravel()
= flattened view,flatten()
= flattened copy- Always match total elements when reshaping
⚙️ Real-World Applications
- Feeding data into ML models (e.g., reshape to
(n_samples, n_features)
) - Image processing (e.g., reshape pixel arrays)
- Creating input batches for neural networks
❓ FAQs – NumPy Array Shape
❓ What does .shape
return?
✅ A tuple of dimensions: e.g., (3, 2)
means 3 rows × 2 columns.
❓ Can I use -1 in reshape()?
✅ Yes, NumPy infers the correct value automatically:
np.arange(8).reshape((2, -1)) # Output shape: (2, 4)
❓ What’s the difference between shape and size?
✅ shape
returns the structure, size
returns total elements:
arr.shape # (2, 3)
arr.size # 6
❓ Can I reshape an array to more dimensions?
✅ Yes, if total element count matches. E.g., 1D → 3D:
np.arange(8).reshape((2, 2, 2))
❓ Is .shape
writable?
✅ Yes, you can directly change shape:
arr.shape = (3, 2)
Share Now :