Bird
0
0

You have a numpy array arr with a non-contiguous memory layout. Which method creates a contiguous copy without modifying the original data?

hard📝 Application Q9 of 15
NumPy - Fundamentals
You have a numpy array arr with a non-contiguous memory layout. Which method creates a contiguous copy without modifying the original data?
Anp.ascontiguousarray(arr)
Barr.flatten()
Carr.copy(order='F')
Dnp.reshape(arr, arr.shape)
Step-by-Step Solution
Solution:
  1. Step 1: Understand methods

    np.ascontiguousarray returns a contiguous array copy if needed.
  2. Step 2: Analyze other options

    flatten() returns a 1D copy, copy(order='F') makes Fortran order, reshape does not guarantee contiguity.
  3. Final Answer:

    np.ascontiguousarray(arr) -> Option A
  4. Quick Check:

    ascontiguousarray ensures C-contiguous copy [OK]
Quick Trick: Use np.ascontiguousarray for contiguous copy [OK]
Common Mistakes:
  • Using flatten which changes shape
  • Assuming reshape guarantees contiguity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes