Adding 5 to a numpy array adds 5 to every element (broadcasting).
Step 2: Calculate new array values
Each element increases by 5: 10+5=15, 20+5=25, 30+5=35, 40+5=45.
Final Answer:
[[15 25]
[35 45]] -> Option D
Quick Check:
Array + 5 adds 5 to all elements [OK]
Hint: Adding scalar adds to all pixels in array [OK]
Common Mistakes:
Thinking addition changes shape
Adding 5 only to first row or column
Confusing addition with multiplication
4. What is wrong with this code to crop the top-left 2x2 part of a 3x3 image array img?:
cropped = img[0:2, 0:3]
medium
A. It crops 2 rows but 3 columns, not 2 columns
B. It causes an index error
C. It flips the image instead of cropping
D. It returns the whole image
Solution
Step 1: Analyze slicing indices
The slice img[0:2, 0:3] selects rows 0 and 1 (2 rows) and columns 0,1,2 (3 columns).
Step 2: Compare to desired crop size
We want a 2x2 crop, but this selects 2x3, so it includes one extra column.
Final Answer:
It crops 2 rows but 3 columns, not 2 columns -> Option A
Quick Check:
Slicing columns 0:3 selects 3 columns [OK]
Hint: Check slice ranges match desired rows and columns [OK]
Common Mistakes:
Assuming slicing is exclusive of end index
Expecting error from out-of-range slice
Confusing rows and columns order
5. You have a color image as a 3D numpy array img with shape (100, 100, 3). How do you create a new image gray_img by averaging the color channels for each pixel?
hard
A. gray_img = img.sum(axis=0)
B. gray_img = img.mean(axis=2)
C. gray_img = img[:, :, 0]
D. gray_img = np.flip(img, axis=1)
Solution
Step 1: Understand image shape and channels
The shape (100, 100, 3) means 100x100 pixels with 3 color channels (RGB) along axis 2.
Step 2: Average color channels per pixel
Using img.mean(axis=2) calculates the average of the 3 channels for each pixel, producing a 2D grayscale image.
Final Answer:
gray_img = img.mean(axis=2) -> Option B
Quick Check:
Mean over axis 2 averages color channels [OK]
Hint: Use mean(axis=2) to average RGB channels [OK]
Common Mistakes:
Summing over wrong axis
Selecting only one color channel
Using flip instead of averaging
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.