0
0
NumPydata~10 mins

transpose() for swapping axes 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 transpose the 2D array using numpy.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
transposed = arr.[1]()
Drag options to blanks, or click blank then click option'
Atranspose
Bswapaxes
Creshape
Dflatten
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape instead of transpose changes the shape but not the axes.
Using flatten converts the array to 1D, not transposing it.
2fill in blank
medium

Complete the code to transpose a 3D numpy array by swapping the first and second axes.

NumPy
import numpy as np
arr = np.random.rand(2, 3, 4)
transposed = arr.transpose([1])
Drag options to blanks, or click blank then click option'
A(0, 1, 2)
B(1, 0, 2)
C(2, 1, 0)
D(0, 2, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using (0, 1, 2) does not change axes order.
Using (2, 1, 0) reverses all axes, not just the first two.
3fill in blank
hard

Fix the error in the code to correctly transpose the 2D numpy array.

NumPy
import numpy as np
arr = np.array([[5, 6], [7, 8]])
transposed = arr.[1]
Drag options to blanks, or click blank then click option'
Atranspose
Btranspose{}
Ctranspose[]
Dtranspose()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses causes no transposition.
Using brackets or braces instead of parentheses causes syntax errors.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 characters.

NumPy
words = ['data', 'is', 'fun', 'and', 'cool']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters out the wrong words.
Using word instead of len(word) stores the word, not its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 3 characters.

NumPy
words = ['data', 'is', 'fun', 'and', 'cool']
result = [1]: [2] for w in words if len(w) [3] 3}}
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using w instead of w.upper() stores lowercase keys.
Using < instead of > filters the wrong words.