0
0
NumPydata~10 mins

reshape() for changing dimensions 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 reshape the 1D array into a 2D array with 3 rows.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.[1]((3, 2))
print(reshaped)
Drag options to blanks, or click blank then click option'
Areshape
Bflatten
Cravel
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using resize instead of reshape.
Using flatten which converts to 1D instead of reshaping.
2fill in blank
medium

Complete the code to reshape the array into 3 rows and 2 columns.

NumPy
import numpy as np
arr = np.arange(6)
new_arr = arr.[1]((3, 2))
print(new_arr)
Drag options to blanks, or click blank then click option'
Aflatten
Bresize
Creshape
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Using resize which modifies the original array.
Using flatten which makes the array 1D.
3fill in blank
hard

Fix the error in reshaping the array to 2 rows and 4 columns.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
arr.[1]((2, 4))
print(arr)
Drag options to blanks, or click blank then click option'
Areshape
Bresize
Cflatten
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape with incompatible shape causes an error.
Using flatten or ravel does not change shape as needed.
4fill in blank
hard

Fill both blanks to create a 3D array with shape (2, 3, 1).

NumPy
import numpy as np
arr = np.arange(6)
reshaped = arr.[1]((2, [2], 1))
print(reshaped.shape)
Drag options to blanks, or click blank then click option'
Areshape
B3
C4
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using resize which changes array size.
Choosing wrong middle dimension that does not match total elements.
5fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 letters.

NumPy
words = ['data', 'science', 'is', 'fun']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
A:
Blen(word)
C>
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' in dictionary comprehension.
Using wrong comparison operator like '=' instead of '>'.