0
0
NumPydata~10 mins

Matrix transpose operations 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 matrix using NumPy.

NumPy
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
transposed = matrix[1]
Drag options to blanks, or click blank then click option'
A.transpose()
B.T
C.reshape(2,2)
D.flatten()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .reshape() changes the shape but does not transpose.
Using .flatten() converts the matrix to a 1D array.
2fill in blank
medium

Complete the code to transpose the matrix using the transpose() function.

NumPy
import numpy as np
matrix = np.array([[5, 6, 7], [8, 9, 10]])
transposed = np.[1](matrix)
Drag options to blanks, or click blank then click option'
Aflatten
Breshape
Ctranspose
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten() or ravel() which flatten the array instead of transposing.
Using reshape() which changes shape but does not transpose.
3fill in blank
hard

Fix the error in the code to correctly transpose the matrix.

NumPy
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
transposed = matrix.transpose[1]
Drag options to blanks, or click blank then click option'
A{}
B[]
C.T
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses causes the method object to be returned, not the transposed matrix.
Using square brackets or curly braces causes syntax errors.
4fill in blank
hard

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

NumPy
words = ['data', 'is', 'fun', 'and', 'cool']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dword.length > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word > 3 compares string to number, which is invalid.
Using word.length is not valid in Python.
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 = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if [3] }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 3
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of uppercase keys.
Not filtering words by length > 3.