0
0
NumPydata~10 mins

flatten() and ravel() for 1D conversion 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 convert the 2D array to 1D using flatten().

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat_arr = arr.[1]()
Drag options to blanks, or click blank then click option'
Aravel
Bflatten
Creshape
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape instead of flatten()
Using resize which modifies the array in-place
Using ravel() which returns a view, not a copy
2fill in blank
medium

Complete the code to convert the 2D array to 1D using ravel().

NumPy
import numpy as np
arr = np.array([[5, 6], [7, 8]])
raveled_arr = arr.[1]()
Drag options to blanks, or click blank then click option'
Aravel
Bflatten
Creshape
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten() which returns a copy instead of a view
Using reshape which requires specifying shape
Using resize which changes the array size
3fill in blank
hard

Fix the error in the code to flatten the array correctly.

NumPy
import numpy as np
arr = np.array([[9, 10], [11, 12]])
flat_arr = arr.[1]()
Drag options to blanks, or click blank then click option'
Aflatten()
Bflatten
Cravel()
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after method name
Using attribute name without calling it
Confusing flatten and ravel without parentheses
4fill in blank
hard

Fill both blanks to create a 1D array from 2D using flatten() and check its type.

NumPy
import numpy as np
arr = np.array([[13, 14], [15, 16]])
flat_arr = arr.[1]()
arr_type = type([2])
Drag options to blanks, or click blank then click option'
Aflatten
Bravel
Cflat_arr
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Checking type of original array instead of flattened array
Using ravel instead of flatten for this task
Not calling the method with parentheses
5fill in blank
hard

Fill all three blanks to flatten the array with ravel(), modify the first element, and print the original array.

NumPy
import numpy as np
arr = np.array([[17, 18], [19, 20]])
raveled_arr = arr.[1]()
raveled_arr[0] = [2]
print(arr[0, 0] == [3])
Drag options to blanks, or click blank then click option'
Aflatten
Bravel
C100
D17
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten() which returns a copy, so original array won't change
Not modifying the first element correctly
Checking original array value incorrectly