📖 Scenario: Imagine you are working with image data in a simple computer vision project. Images are stored as arrays, but sometimes you need to add or remove extra dimensions to prepare the data for a model.
🎯 Goal: You will learn how to add a new dimension to a NumPy array using np.expand_dims() and how to remove single-dimensional entries using np.squeeze(). This helps in reshaping data correctly for processing.
📋 What You'll Learn
Create a 2D NumPy array called image with exact values
Create a variable called axis_to_expand to specify where to add a new dimension
Use np.expand_dims() to add a new dimension to image and save it as expanded_image
Use np.squeeze() to remove single-dimensional entries from expanded_image and save it as squeezed_image
Print the shapes of image, expanded_image, and squeezed_image to see the changes
💡 Why This Matters
🌍 Real World
In real-world data science, reshaping arrays is common when preparing data for machine learning models, especially in image processing and deep learning.
💼 Career
Understanding how to manipulate array dimensions is essential for data scientists and machine learning engineers to correctly format data inputs and outputs.
Progress0 / 4 steps
1
Create the initial 2D image array
Create a 2D NumPy array called image with these exact values: [[10, 20, 30], [40, 50, 60]]
NumPy
Hint
Use np.array() to create the array with the given nested list.
2
Set the axis to expand
Create a variable called axis_to_expand and set it to 0 to add a new dimension as the first axis
NumPy
Hint
Just assign the number 0 to the variable axis_to_expand.
3
Add a new dimension using np.expand_dims()
Use np.expand_dims() with image and axis_to_expand to create expanded_image that has a new dimension added
NumPy
Hint
Call np.expand_dims() with image and axis=axis_to_expand.
4
Remove single-dimensional entries using np.squeeze() and print shapes
Use np.squeeze() on expanded_image to create squeezed_image. Then print the shapes of image, expanded_image, and squeezed_image each on a new line.
NumPy
Hint
Use np.squeeze() on expanded_image. Then print the .shape attribute of each array.
Practice
(1/5)
1. What does np.expand_dims() do to a NumPy array?
easy
A. Adds a new dimension at the specified axis
B. Removes all dimensions of size one
C. Flattens the array into one dimension
D. Changes the data type of the array
Solution
Step 1: Understand the function purpose
np.expand_dims() is used to add a new axis to an array, increasing its dimensions.
Step 2: Compare with other options
Removing dimensions or flattening is done by other functions like np.squeeze() or reshape(). Changing data type is unrelated.
Final Answer:
Adds a new dimension at the specified axis -> Option A
2. Which of the following is the correct syntax to add a new axis at position 1 using np.expand_dims() on array a?
easy
A. np.expand_dims(a, axis=0)
B. np.expand_dims(a, 0)
C. np.expand_dims(a, axis=1)
D. np.expand_dims(a, axis=2)
Solution
Step 1: Check the function signature
np.expand_dims(array, axis) requires the axis parameter to specify where to add the new dimension.
Step 2: Identify correct axis value
Axis=1 adds the new dimension at position 1. np.expand_dims(a, 0) (positional) and np.expand_dims(a, axis=0) add the dimension at position 0. np.expand_dims(a, axis=2) causes an AxisError since valid axes are -2 to 1 for a 1D array.
Final Answer:
np.expand_dims(a, axis=1) -> Option C
Quick Check:
Use axis=1 to add dimension at position 1 [OK]
Hint: Use axis= to specify position clearly [OK]
Common Mistakes:
Omitting axis keyword and confusing position
Using wrong axis number
Mixing axis=0 and axis=1
3. What is the shape of the array after applying np.squeeze() to arr = np.array([[[1], [2], [3]]])?
medium
A. (3,)
B. (1, 3, 1)
C. (3, 1)
D. (3, 1, 1)
Solution
Step 1: Check original shape
arr has shape (1, 3, 1) because it is a 3D array with one element in first and last dimension.
Step 2: Apply np.squeeze()
np.squeeze() removes all dimensions of size 1, so the first and last dimensions are removed, leaving shape (3,).
Final Answer:
(3,) -> Option A
Quick Check:
squeeze removes size 1 dims = (3,) [OK]
Hint: Squeeze removes all size 1 dims from shape [OK]
Common Mistakes:
Assuming squeeze removes only one dimension
Confusing original shape with squeezed shape
Thinking squeeze flattens the array
4. Given arr = np.array([1, 2, 3]), which code will cause an error?
medium
A. np.expand_dims(arr, axis=-1)
B. np.expand_dims(arr, axis=0)
C. np.squeeze(arr)
D. np.expand_dims(arr, axis=3)
Solution
Step 1: Check original array shape
arr has shape (3,), so it has 1 dimension.
Step 2: Validate axis values for expand_dims
Valid axis values for adding a new dimension are between -2 and 1 (inclusive) because adding a dimension increases rank by 1. Axis=3 is out of bounds and causes an error.
Final Answer:
np.expand_dims(arr, axis=3) causes error -> Option D
Quick Check:
Axis out of range causes error [OK]
Hint: Axis must be in valid range for expand_dims [OK]
Common Mistakes:
Using axis larger than array ndim + 1
Confusing axis indexing with array shape
Assuming squeeze can cause error on 1D arrays
5. You have a 2D array data with shape (5, 1). You want to prepare it for a machine learning model that expects input shape (5,). Which code correctly reshapes data using np.squeeze() and np.expand_dims()?
hard
A. np.expand_dims(np.squeeze(data), axis=1)
B. np.squeeze(data)
C. np.squeeze(np.expand_dims(data, axis=-1))
D. np.expand_dims(data, axis=-1)
Solution
Step 1: Understand the original shape and target shape
data shape is (5, 1). The model expects (5,), so we need to remove the extra dimension.
Step 2: Use np.squeeze() to remove size 1 dimension
np.squeeze(data) removes the dimension of size 1, resulting in shape (5,). Adding dimensions again is unnecessary and would change shape incorrectly.
Final Answer:
np.squeeze(data) -> Option B
Quick Check:
Squeeze removes extra dims to match model input [OK]
Hint: Squeeze to remove size 1 dims for model input [OK]
Common Mistakes:
Adding dimensions instead of removing
Using expand_dims unnecessarily
Not matching model expected input shape
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.