0
0
NumPydata~5 mins

flatten() and ravel() for 1D conversion in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the flatten() method do in NumPy?
It converts a multi-dimensional array into a 1D array by copying the data into a new array.
Click to reveal answer
beginner
How is ravel() different from flatten() in NumPy?
ravel() returns a flattened 1D view of the array if possible, without copying data, making it faster and memory efficient.
Click to reveal answer
beginner
If you modify the output of flatten(), does it affect the original array?
No, because flatten() returns a copy, changes to it do not affect the original array.
Click to reveal answer
intermediate
If you modify the output of ravel(), does it affect the original array?
Yes, if ravel() returns a view (no copy), changes to it will affect the original array.
Click to reveal answer
intermediate
Which method is generally faster for flattening an array: flatten() or ravel()?
ravel() is generally faster because it tries to return a view instead of copying data.
Click to reveal answer
What does arr.flatten() return?
AA copy of the array flattened to 1D
BA view of the array flattened to 1D
CThe original array unchanged
DAn error if array is not 1D
Which method may return a view instead of a copy?
Aflatten()
Bravel()
CBoth flatten() and ravel()
DNeither flatten() nor ravel()
If you want to modify the flattened array without changing the original, which method should you use?
Aravel()
BBoth are safe
CNeither is safe
Dflatten()
Which method is generally faster for flattening large arrays?
Aflatten()
BBoth have same speed
Cravel()
DDepends on array shape
What is the shape of the output from arr.ravel() if arr is 2D?
A1D array with all elements
BSame as original 2D shape
C2D array with one column
DScalar value
Explain the difference between flatten() and ravel() in NumPy.
Think about copying vs viewing and speed.
You got /5 concepts.
    When would you prefer to use flatten() over ravel()?
    Consider safety of changes to the original array.
    You got /3 concepts.