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?✗ Incorrect
flatten() always returns a copy of the array as 1D.Which method may return a view instead of a copy?
✗ Incorrect
ravel() tries to return a view to save memory.If you want to modify the flattened array without changing the original, which method should you use?
✗ Incorrect
flatten() returns a copy, so original array stays unchanged.Which method is generally faster for flattening large arrays?
✗ Incorrect
ravel() is faster because it avoids copying when possible.What is the shape of the output from
arr.ravel() if arr is 2D?✗ Incorrect
ravel() flattens the array to 1D.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.