Bird
Raised Fist0
NumPydata~20 mins

Image as array concept in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Image Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the shape of the image array?
Given a color image loaded as a NumPy array with shape (480, 640, 3), what does the shape represent?
NumPy
import numpy as np
image = np.zeros((480, 640, 3))
print(image.shape)
A(480, 640, 3) means 480 pixels width, 640 pixels height, and 3 color channels (RGB)
B(480, 640, 3) means 480 pixels height, 640 pixels width, and 3 color channels (RGB)
C(480, 640, 3) means 3 pixels height, 480 pixels width, and 640 color channels
D(480, 640, 3) means 3 pixels width, 640 pixels height, and 480 color channels
Attempts:
2 left
💡 Hint
Remember the shape order is (height, width, channels) for images.
❓ data_output
intermediate
1:30remaining
What is the pixel value at position (2, 3)?
Consider this grayscale image array:
import numpy as np
image = np.array([[10, 20, 30, 40],
                  [50, 60, 70, 80],
                  [90, 100, 110, 120]])
print(image[2, 3])
What is the output?
NumPy
import numpy as np
image = np.array([[10, 20, 30, 40],
                  [50, 60, 70, 80],
                  [90, 100, 110, 120]])
print(image[2, 3])
A120
B110
C80
D90
Attempts:
2 left
💡 Hint
Remember indexing is zero-based and rows come first.
❓ visualization
advanced
2:30remaining
Visualize a grayscale image from a 2D array
Which code snippet correctly displays a 5x5 grayscale image using matplotlib from a NumPy array?
NumPy
import numpy as np
import matplotlib.pyplot as plt
image = np.array([[0, 50, 100, 150, 200],
                  [10, 60, 110, 160, 210],
                  [20, 70, 120, 170, 220],
                  [30, 80, 130, 180, 230],
                  [40, 90, 140, 190, 240]])
plt.imshow(image, cmap='gray')
plt.show()
Aplt.imshow(image, cmap='jet') followed by plt.show()
Bplt.imshow(image) without cmap argument followed by plt.show()
Cplt.imshow(image, cmap='gray') followed by plt.show()
Dplt.plot(image) followed by plt.show()
Attempts:
2 left
💡 Hint
Use the grayscale colormap to show intensity properly.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
What error does this code raise and why?
import numpy as np
image = np.zeros((100, 100, 3))
print(image[50, 50])
print(image[50, 50, 3])
NumPy
import numpy as np
image = np.zeros((100, 100, 3))
print(image[50, 50])
print(image[50, 50, 3])
ANo error, prints pixel values
BTypeError: too many indices for array
CValueError: invalid index
DIndexError: index 3 is out of bounds for axis 2 with size 3
Attempts:
2 left
💡 Hint
Remember Python indexing starts at 0 and max index is size-1.
🚀 Application
expert
3:00remaining
Calculate average color of an RGB image
Given a NumPy array representing an RGB image of shape (100, 100, 3), which code correctly calculates the average color across all pixels?
NumPy
import numpy as np
image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
Aavg_color = image.mean(axis=(0,1))
Bavg_color = image.mean(axis=2)
Cavg_color = image.mean(axis=0)
Davg_color = image.mean()
Attempts:
2 left
💡 Hint
Average color means average over height and width, keep channels separate.

Practice

(1/5)
1. What does each element in a NumPy array representing an image usually stand for?
easy
A. A pixel's color value
B. The image file size
C. The image format type
D. The number of images

Solution

  1. Step 1: Understand image representation in arrays

    Images are stored as arrays where each element corresponds to a pixel's color information.
  2. Step 2: Identify what each element represents

    Each element holds the color value (like red, green, or blue intensity) for that pixel.
  3. Final Answer:

    A pixel's color value -> Option A
  4. Quick Check:

    Pixel color = element value [OK]
Hint: Pixels map to array elements holding color values [OK]
Common Mistakes:
  • Confusing pixel values with image size
  • Thinking array stores image format
  • Assuming array length equals number of images
2. Which NumPy code correctly creates a 3x3 image array with 3 color channels (RGB)?
easy
A. np.zeros((3, 3, 3))
B. np.zeros([3, 3])
C. np.zeros(3, 3, 3)
D. np.zeros((3, 3))

Solution

  1. Step 1: Understand array shape for RGB image

    An RGB image array shape is (height, width, 3) where 3 is for color channels.
  2. Step 2: Check correct NumPy syntax for shape

    NumPy expects shape as a tuple inside zeros(), so (3, 3, 3) is correct.
  3. Final Answer:

    np.zeros((3, 3, 3)) -> Option A
  4. Quick Check:

    Shape tuple with 3 channels = np.zeros((3,3,3)) [OK]
Hint: Use tuple shape with 3 for RGB channels [OK]
Common Mistakes:
  • Passing shape as separate arguments instead of tuple
  • Missing the third dimension for color channels
  • Using list instead of tuple for shape
3. What is the output shape of this code?
import numpy as np
img = np.ones((4, 5, 3))
print(img.shape)
medium
A. (5, 4, 3)
B. (4, 5, 3)
C. (4, 5)
D. (3, 4, 5)

Solution

  1. Step 1: Analyze the array creation

    np.ones((4, 5, 3)) creates an array with shape (4, 5, 3).
  2. Step 2: Understand shape attribute output

    Printing img.shape returns the tuple describing the array dimensions exactly as given.
  3. Final Answer:

    (4, 5, 3) -> Option B
  4. Quick Check:

    Shape matches input tuple (4,5,3) [OK]
Hint: Shape prints exactly as array was created [OK]
Common Mistakes:
  • Mixing up width and height order
  • Ignoring the color channel dimension
  • Assuming shape returns total size
4. Find the error in this code that tries to create a 2x2 RGB image array:
import numpy as np
img = np.zeros(2, 2, 3)
print(img.shape)
medium
A. np.zeros cannot create 3D arrays
B. Shape dimensions are too large
C. Missing tuple for shape in np.zeros
D. print statement syntax is wrong

Solution

  1. Step 1: Check np.zeros syntax

    np.zeros expects a single shape argument as a tuple, but here three separate arguments are given.
  2. Step 2: Identify correct shape input

    Shape should be passed as np.zeros((2, 2, 3)) with parentheses around the shape tuple.
  3. Final Answer:

    Missing tuple for shape in np.zeros -> Option C
  4. Quick Check:

    Shape must be tuple in np.zeros() [OK]
Hint: Always pass shape as one tuple to np.zeros [OK]
Common Mistakes:
  • Passing multiple arguments instead of one tuple
  • Assuming np.zeros can't create 3D arrays
  • Incorrect print syntax
5. You have a 2x2 image array with RGB values:
import numpy as np
img = np.array([[[255, 0, 0], [0, 255, 0]],
                [[0, 0, 255], [255, 255, 0]]])
How do you change the bottom-right pixel to white (255, 255, 255)?
hard
A. img[0, 0] = [255, 255, 255]
B. img[2, 2] = [255, 255, 255]
C. img[1][1][3] = 255
D. img[1, 1] = [255, 255, 255]

Solution

  1. Step 1: Identify pixel position in array

    Bottom-right pixel in a 2x2 array is at index [1, 1] (zero-based indexing).
  2. Step 2: Assign new RGB values correctly

    Assign the list [255, 255, 255] to img[1, 1] to set it to white.
  3. Final Answer:

    img[1, 1] = [255, 255, 255] -> Option D
  4. Quick Check:

    Index [1,1] changes bottom-right pixel [OK]
Hint: Use zero-based index for pixel, assign RGB list [OK]
Common Mistakes:
  • Using out-of-range indices like [2, 2]
  • Trying to assign to a non-existing channel index
  • Changing wrong pixel like top-left
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard