0
0
NumPydata~10 mins

Image as array concept in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a 3x3 grayscale image as a NumPy array.

NumPy
import numpy as np
image = np.array([1])
Drag options to blanks, or click blank then click option'
A[[0, 128], [64, 128], [0, 64]]
B[0, 128, 255, 64, 128, 192, 0, 64, 255]
C[[255, 255, 255]]
D[[0, 128, 255], [64, 128, 192], [0, 64, 255]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a flat list instead of nested lists.
Creating arrays with wrong dimensions.
2fill in blank
medium

Complete the code to access the pixel value at row 1, column 2 in the image array.

NumPy
pixel_value = image[[1]]
Drag options to blanks, or click blank then click option'
A1, 2
B[1, 2]
C(1, 2)
Dimage[1][2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple for indexing.
Trying to use double brackets like image[1][2] which works but is not the best practice here.
3fill in blank
hard

Fix the error in the code to convert a color image array to grayscale by averaging the color channels.

NumPy
grayscale = np.mean(image[1], axis=[2])
Drag options to blanks, or click blank then click option'
A(,)
B[,]
C[:, :, :]
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect slicing syntax.
Averaging over the wrong axis.
4fill in blank
hard

Fill both blanks to create a dictionary mapping pixel positions to their grayscale values for pixels with value greater than 100.

NumPy
pixel_dict = [1] for i in range(image.shape[0]) for j in range(image.shape[1]) if image[i, j] [2] 100
Drag options to blanks, or click blank then click option'
A{(i, j): image[i, j]}
B[i, j]
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of dictionary comprehension.
Using wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a new image array where pixels below 50 are set to 0, others remain unchanged.

NumPy
new_image = np.array([[image[i, j] if image[i, j] [1] 50 else [2] for j in range([3])] for i in range(image.shape[0])])
Drag options to blanks, or click blank then click option'
A>
B0
Cimage.shape[1]
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator.
Using wrong range for inner loop.