0
0
NumPydata~10 mins

flatten() and ravel() for 1D conversion in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - flatten() and ravel() for 1D conversion
Start with 2D array
Choose method: flatten() or ravel()
flatten()
returns a copy1D array copy
ravel()
returns a view if possible1D array view or copy
Use 1D array for further processing
Start with a 2D array, then use flatten() to get a copy as 1D, or ravel() to get a view or copy as 1D, then use the 1D array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat = arr.flatten()
rav = arr.ravel()
flat[0] = 100
rav[1] = 200
print(arr)
print(flat)
print(rav)
This code converts a 2D array to 1D using flatten() and ravel(), modifies elements, and prints all arrays to show differences.
Execution Table
StepActionArray StateNotes
1Create arr = [[1, 2], [3, 4]][[1, 2], [3, 4]]Original 2D array
2flat = arr.flatten()[1, 2, 3, 4]flatten() returns a copy as 1D
3rav = arr.ravel()[1, 2, 3, 4]ravel() returns a view as 1D
4flat[0] = 100[100, 2, 3, 4]Change in flat only, arr unchanged
5rav[1] = 200[1, 200, 3, 4]Change in rav affects arr because rav is a view
6Print arr[[1, 200], [3, 4]]arr changed due to rav modification
7Print flat[100, 2, 3, 4]flat unchanged after modification
8Print rav[1, 200, 3, 4]rav shows updated values
9End-Execution complete
💡 All steps executed; flatten() copy unaffected by changes, ravel() view reflects changes in original array.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr[[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 200], [3, 4]][[1, 200], [3, 4]]
flatN/A[1, 2, 3, 4][1, 2, 3, 4][100, 2, 3, 4][100, 2, 3, 4][100, 2, 3, 4]
ravN/AN/A[1, 2, 3, 4][1, 2, 3, 4][1, 200, 3, 4][1, 200, 3, 4]
Key Moments - 3 Insights
Why does changing flat[0] not affect the original array arr?
Because flatten() returns a copy of the array, changes to flat do not affect arr. See execution_table step 4 and 6 where arr remains unchanged after flat modification.
Why does changing rav[1] affect the original array arr?
Because ravel() returns a view (not a copy) when possible, changes to rav reflect in arr. See execution_table step 5 and 6 where arr changes after modifying rav.
Are flat and rav always independent arrays?
No. flat is always a copy, independent from arr. rav is a view if possible, so it may share data with arr. See variable_tracker for their states after modifications.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the value of arr after rav[1] = 200?
A[[1, 200], [3, 4]]
B[[1, 2], [3, 4]]
C[[100, 2], [3, 4]]
D[[1, 2], [200, 4]]
💡 Hint
Check execution_table row 5 and 6 showing arr changes after modifying rav.
According to variable_tracker, what is the value of flat after step 4?
A[1, 2, 3, 4]
B[100, 2, 3, 4]
C[1, 200, 3, 4]
D[[100, 2], [3, 4]]
💡 Hint
Look at variable_tracker row for flat after step 4.
If we modify arr[0,0] = 500 after step 5, what happens to rav?
Arav remains unchanged
Bflat[0] becomes 500
Crav[0] becomes 500
Dflat and rav both become 500
💡 Hint
Recall rav is a view of arr, so changes in arr reflect in rav.
Concept Snapshot
flatten() and ravel() convert arrays to 1D.
flatten() returns a copy; changes do not affect original.
ravel() returns a view if possible; changes affect original.
Use flatten() to keep original safe.
Use ravel() for memory efficiency.
Full Transcript
We start with a 2D numpy array. Using flatten(), we get a new 1D array copy. Using ravel(), we get a 1D view if possible. Changing the flattened copy does not change the original array. Changing the raveled view changes the original array because they share data. This shows the difference between copy and view in numpy. Remember flatten() is safe for independent changes, ravel() is efficient but linked to original.