0
0
NumPydata~20 mins

flatten() and ravel() for 1D conversion in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flatten and Ravel Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of flatten() vs ravel() on a 2D array
What is the output of the following code snippet?
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)
A
[[1 2]
 [3 4]]
[100   2   3   4]
[1 200 3 4]
B
[[100 2]
 [3 4]]
[100   2   3   4]
[1 200 3 4]
C
[[1 2]
 [3 4]]
[1 2 3 4]
[1 2 3 4]
D
[[1 200]
 [3 4]]
[100   2   3   4]
[1 200 3 4]
Attempts:
2 left
💡 Hint
Remember that flatten() returns a copy, while ravel() returns a view when possible.
data_output
intermediate
1:30remaining
Shape of arrays after flatten() and ravel()
Given the code below, what are the shapes of flat and rav?
NumPy
import numpy as np
arr = np.arange(12).reshape(3,4)
flat = arr.flatten()
rav = arr.ravel()
print(flat.shape)
print(rav.shape)
A
(3,4)
(12,)
B
(12,)
(12,)
C
(12,)
(3,4)
D
(3,4)
(3,4)
Attempts:
2 left
💡 Hint
Both flatten() and ravel() convert the array to 1D.
🔧 Debug
advanced
1:30remaining
Why does modifying ravel() output change the original array?
Consider this code: import numpy as np arr = np.array([[5, 6], [7, 8]]) r = arr.ravel() r[0] = 100 print(arr) Why does arr change after modifying r?
NumPy
import numpy as np
arr = np.array([[5, 6], [7, 8]])
r = arr.ravel()
r[0] = 100
print(arr)
Aravel() returns a copy, so changes to r do not affect arr.
Barr is immutable, so changes to r raise an error.
Cravel() returns a view of the original array, so changes to r affect arr.
Dravel() flattens the array but does not allow modification.
Attempts:
2 left
💡 Hint
Think about whether ravel() returns a copy or a view.
🧠 Conceptual
advanced
1:30remaining
Memory usage difference between flatten() and ravel()
Which statement about memory usage is true when comparing flatten() and ravel()?
Aflatten() always creates a copy, using more memory; ravel() returns a view when possible, using less memory.
Bflatten() returns a view, so it uses less memory; ravel() always copies the data.
CBoth flatten() and ravel() always create copies, so memory usage is the same.
DBoth flatten() and ravel() always return views, so memory usage is minimal.
Attempts:
2 left
💡 Hint
Check which method returns a copy and which returns a view.
🚀 Application
expert
2:30remaining
Choosing flatten() or ravel() for safe modification
You have a large 2D numpy array and want to create a 1D version to modify without changing the original array. Which method should you use and why?
AUse flatten() because it returns a copy, so the original array stays unchanged.
BUse ravel() because it returns a view, so changes reflect in the original array.
CUse flatten() because it returns a view, so changes reflect in the original array.
DUse ravel() because it returns a copy, so the original array stays unchanged.
Attempts:
2 left
💡 Hint
Think about whether you want changes to affect the original array or not.