We use flatten() and ravel() to turn multi-dimensional arrays into a simple list of values. This helps us look at or work with all data in one line.
flatten() and ravel() for 1D conversion in NumPy
array.flatten() array.ravel()
flatten() always makes a new copy of the data.
ravel() tries to give a view (no copy) if possible, so it's faster and uses less memory.
arr with all elements in order.import numpy as np arr = np.array([[1, 2], [3, 4]]) flat_arr = arr.flatten()
arr if possible, sharing the same data.import numpy as np arr = np.array([[1, 2], [3, 4]]) raveled_arr = arr.ravel()
raveled_arr changes arr because ravel() returns a view.import numpy as np arr = np.array([[1, 2], [3, 4]]) raveled_arr = arr.ravel() raveled_arr[0] = 10 print(arr)
flat_arr does NOT change arr because flatten() returns a copy.import numpy as np arr = np.array([[1, 2], [3, 4]]) flat_arr = arr.flatten() flat_arr[0] = 10 print(arr)
This program shows how flatten() and ravel() convert a 2D array to 1D. It also shows that changing the raveled array changes the original, but changing the flattened array does not.
import numpy as np # Create a 2D array arr = np.array([[5, 6, 7], [8, 9, 10]]) # Use flatten() to get a 1D copy flat = arr.flatten() # Use ravel() to get a 1D view rav = arr.ravel() print('Original array:') print(arr) print('\nFlattened array (copy):') print(flat) print('\nRaveled array (view):') print(rav) # Change first element in raveled array rav[0] = 100 print('\nAfter changing raveled array first element to 100:') print('Original array:') print(arr) # Change first element in flattened array flat[0] = 200 print('\nAfter changing flattened array first element to 200:') print('Original array:') print(arr)
Use flatten() when you want a safe copy and won't change the original array.
Use ravel() for faster operations when you want to save memory and don't mind changes affecting the original.
Both methods return arrays in row-major order (left to right, top to bottom).
flatten() makes a new 1D copy of the array.
ravel() returns a 1D view if possible, sharing data with the original.
Use these to simplify multi-dimensional data into one line for easier work.
Practice
flatten() and ravel() in NumPy?Solution
Step 1: Understand
flatten()behaviorflatten()always returns a new 1D copy of the array, so changes to the result do not affect the original.Step 2: Understand
ravel()behaviorravel()returns a 1D view if possible, meaning it shares data with the original array and changes affect the original. If a view is not possible, it returns a copy.Final Answer:
flatten()returns a copy,ravel()returns a view if possible. -> Option DQuick Check:
Copy vs view difference =flatten()returns a copy,ravel()returns a view if possible. [OK]
- Thinking both always return copies
- Confusing which returns a view
- Assuming ravel always returns a copy
arr into a 1D array using ravel()?Solution
Step 1: Recall method usage
ravel()is a method of a NumPy array, so it is called asarr.ravel().Step 2: Check other options
ravel(arr)is incorrect becauseravelis not a standalone function in NumPy;arr.flatten()is valid but uses flatten, not ravel;arr.ravelmisses parentheses, so it returns a method object, not the result.Final Answer:
arr.ravel()-> Option CQuick Check:
Method call syntax =arr.ravel()[OK]
- Forgetting parentheses when calling methods
- Using ravel as a standalone function
- Confusing flatten() with ravel() syntax
import numpy as np arr = np.array([[1, 2], [3, 4]]) flat = arr.flatten() flat[0] = 100 print(arr[0, 0])
Solution
Step 1: Understand flatten() creates a copy
flat = arr.flatten()makes a new 1D copy ofarr. Changes toflatdo not affectarr.Step 2: Check the effect of modifying flat
Changingflat[0]to 100 does not changearr[0, 0], soarr[0, 0]remains 1.Final Answer:
1 -> Option BQuick Check:
flatten() copy means original unchanged = 1 [OK]
- Assuming flatten returns a view and changes original
- Confusing flatten() with ravel() behavior
- Expecting arr[0,0] to change after modifying flat
import numpy as np arr = np.array([[5, 6], [7, 8]]) flat = arr.flatten() flat[0] = 999 print(arr)
Solution
Step 1: Identify flatten() behavior
flatten()returns a new copy of the array, so changes toflatdo not affectarr.Step 2: Understand why original array is unchanged
Sinceflatis a copy, modifyingflat[0]does not changearr. To modify original, useravel()or modifyarrdirectly.Final Answer:
Using flatten() which returns a copy, not a view -> Option AQuick Check:
flatten() returns copy = Using flatten() which returns a copy, not a view [OK]
- Assuming flatten() returns a view
- Not using parentheses with flatten()
- Modifying the copy instead of original array
data and want to flatten it into 1D for fast processing without using extra memory. Which method should you use and why?Solution
Step 1: Understand memory use of flatten() vs ravel()
flatten()always creates a new copy, using extra memory.ravel()returns a view if possible, avoiding extra memory use.Step 2: Consider large data and performance
For large arrays, avoiding copies saves memory and speeds up processing.ravel()is preferred for this reason.Step 3: Check other options
reshape(-1)returns a view if possible, but not guaranteed;tolist()converts to Python list, which is slower and uses more memory.Final Answer:
Usedata.ravel()because it returns a view if possible, saving memory. -> Option AQuick Check:
ravel() view saves memory = Usedata.ravel()because it returns a view if possible, saving memory. [OK]
- Choosing flatten() and wasting memory
- Confusing reshape(-1) behavior
- Using tolist() which is slow and memory heavy
Ready to prove it?
Pass the test and this pattern moves unknown
