Explain the difference between ravel() and flatten() in NumPy regarding views and copies.
Quick Overview
Asks how NumPy's ravel() and flatten() differ when turning an array into one dimension, and when each returns a view or a copy. Tests understanding of memory layout, contiguity, memory cost and the side effects of writing into a flattened result.
Explain the difference between ravel() and flatten() in NumPy regarding views and copies.
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Explain the difference between `ravel()` and `flatten()` in NumPy regarding views and copies.
Both turn a multi-dimensional array into a 1-D array. Explain when each one shares memory with the original array, when it allocates a new one, and what that means for speed, memory use and code that writes into the result.
```hint Look at the memory layout
Ask what has to be true about how the input is stored in memory for a 1-D result to point at the same buffer. Then think of a common array that breaks that condition.
```
### What a Strong Answer Covers
- What each function returns and whether the result shares memory with the input
- The condition under which `ravel()` cannot return a view, with a concrete example
- What happens to the original array when you write into each result
- The time and memory cost of each, and how to choose between them
- How to check in code whether two arrays share memory
### Follow-up Questions
- How does `reshape(-1)` compare with `ravel()` when the input is not contiguous?
- What does the `order` argument change for `ravel()` and `flatten()`?
- Is `a.flat` a view, a copy, or something else, and can you assign through it?
- When would a hidden copy from `ravel()` cause a bug in production code?
Overview: Asks how NumPy's ravel() and flatten() differ when turning an array into one dimension, and when each returns a view or a copy. Tests understanding of memory layout, contiguity, memory cost and the side effects of writing into a flattened result.
Explain the difference between ravel() and flatten() in NumPy regarding views and copies.
Both turn a multi-dimensional array into a 1-D array. Explain when each one shares memory with the original array, when it allocates a new one, and what that means for speed, memory use and code that writes into the result.
What a Strong Answer Covers Guidance
What each function returns and whether the result shares memory with the input
The condition under which
ravel()
cannot return a view, with a concrete example
What happens to the original array when you write into each result
The time and memory cost of each, and how to choose between them
How to check in code whether two arrays share memory
Follow-up Questions Guidance
How does
reshape(-1)
compare with
ravel()
when the input is not contiguous?
What does the
order
argument change for
ravel()
and
flatten()
?
Is
a.flat
a view, a copy, or something else, and can you assign through it?
When would a hidden copy from
ravel()
cause a bug in production code?