The question is asking how to transform a NumPy array...
January 2, 2025 at 03:19 PM
The question is asking how to transform a NumPy array representing an image in the shape (height, width, channels) (320 × 320 × 3) into the shape (channels, height, width) (3 × 320 × 320). The transformation primarily involves rearranging the axes of the array using np.transpose
. Here's what the given options do:
Option 1
img = np.transpose(img, (2, 0, 1))
- The
np.transpose
function rearranges the axes of the array according to the specified order. (2, 0, 1)
means:- Axis 0 (height) becomes the second axis.
- Axis 1 (width) becomes the third axis.
- Axis 2 (channels) becomes the first axis.
- This effectively changes the shape (320, 320, 3) to (3, 320, 320).
- The result is stored back in the variable
img
.
Option 2
np.transpose(img, (2, 0, 1))
- Similar to Option 1, this also rearranges the axes using
(2, 0, 1)
, converting the shape (320, 320, 3) to (3, 320, 320). - HOWEVER, this result is not assigned to a variable, so the original array
img
remains unchanged.
Option 3
img = np.transpose(img, (1, 2, 0))
- The
np.transpose(img, (1, 2, 0))
rearranges the axes as follows:- Axis 0 (height) becomes the third axis.
- Axis 1 (width) becomes the first axis.
- Axis 2 (channels) becomes the second axis.
- This changes the shape (320, 320, 3) to (320, 3, 320), which is not the expected format.
Correct Answer
The code that transforms img
to the expected format (3, 320, 320) is:
- Option 1: This correctly rearranges the axes and assigns the result back to
img
.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node