Bird
Raised Fist0
NumPydata~10 mins

flatten() and ravel() for 1D conversion in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 2D array to 1D using flatten().

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat_arr = arr.[1]()
Drag options to blanks, or click blank then click option'
Aravel
Bflatten
Creshape
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape instead of flatten()
Using resize which modifies the array in-place
Using ravel() which returns a view, not a copy
2fill in blank
medium

Complete the code to convert the 2D array to 1D using ravel().

NumPy
import numpy as np
arr = np.array([[5, 6], [7, 8]])
raveled_arr = arr.[1]()
Drag options to blanks, or click blank then click option'
Aravel
Bflatten
Creshape
Dresize
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten() which returns a copy instead of a view
Using reshape which requires specifying shape
Using resize which changes the array size
3fill in blank
hard

Fix the error in the code to flatten the array correctly.

NumPy
import numpy as np
arr = np.array([[9, 10], [11, 12]])
flat_arr = arr.[1]()
Drag options to blanks, or click blank then click option'
Aflatten()
Bflatten
Cravel()
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after method name
Using attribute name without calling it
Confusing flatten and ravel without parentheses
4fill in blank
hard

Fill both blanks to create a 1D array from 2D using flatten() and check its type.

NumPy
import numpy as np
arr = np.array([[13, 14], [15, 16]])
flat_arr = arr.[1]()
arr_type = type([2])
Drag options to blanks, or click blank then click option'
Aflatten
Bravel
Cflat_arr
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Checking type of original array instead of flattened array
Using ravel instead of flatten for this task
Not calling the method with parentheses
5fill in blank
hard

Fill all three blanks to flatten the array with ravel(), modify the first element, and print the original array.

NumPy
import numpy as np
arr = np.array([[17, 18], [19, 20]])
raveled_arr = arr.[1]()
raveled_arr[0] = [2]
print(arr[0, 0] == [3])
Drag options to blanks, or click blank then click option'
Aflatten
Bravel
C100
D17
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten() which returns a copy, so original array won't change
Not modifying the first element correctly
Checking original array value incorrectly

Practice

(1/5)
1. What is the main difference between flatten() and ravel() in NumPy?
easy
A. Both return copies but differ in speed.
B. flatten() returns a view, ravel() returns a copy.
C. Both return views but differ in shape.
D. flatten() returns a copy, ravel() returns a view if possible.

Solution

  1. Step 1: Understand flatten() behavior

    flatten() always returns a new 1D copy of the array, so changes to the result do not affect the original.
  2. Step 2: Understand ravel() behavior

    ravel() 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.
  3. Final Answer:

    flatten() returns a copy, ravel() returns a view if possible. -> Option D
  4. Quick Check:

    Copy vs view difference = flatten() returns a copy, ravel() returns a view if possible. [OK]
Hint: Remember: flatten copies, ravel tries to share data [OK]
Common Mistakes:
  • Thinking both always return copies
  • Confusing which returns a view
  • Assuming ravel always returns a copy
2. Which of the following is the correct syntax to convert a 2D NumPy array arr into a 1D array using ravel()?
easy
A. arr.flatten()
B. ravel(arr)
C. arr.ravel()
D. arr.ravel

Solution

  1. Step 1: Recall method usage

    ravel() is a method of a NumPy array, so it is called as arr.ravel().
  2. Step 2: Check other options

    ravel(arr) is incorrect because ravel is not a standalone function in NumPy; arr.flatten() is valid but uses flatten, not ravel; arr.ravel misses parentheses, so it returns a method object, not the result.
  3. Final Answer:

    arr.ravel() -> Option C
  4. Quick Check:

    Method call syntax = arr.ravel() [OK]
Hint: Use parentheses to call methods like ravel() [OK]
Common Mistakes:
  • Forgetting parentheses when calling methods
  • Using ravel as a standalone function
  • Confusing flatten() with ravel() syntax
3. What will be the output of the following code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat = arr.flatten()
flat[0] = 100
print(arr[0, 0])
medium
A. 100
B. 1
C. 0
D. Error

Solution

  1. Step 1: Understand flatten() creates a copy

    flat = arr.flatten() makes a new 1D copy of arr. Changes to flat do not affect arr.
  2. Step 2: Check the effect of modifying flat

    Changing flat[0] to 100 does not change arr[0, 0], so arr[0, 0] remains 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    flatten() copy means original unchanged = 1 [OK]
Hint: flatten() copies, so original array stays same after changes [OK]
Common Mistakes:
  • Assuming flatten returns a view and changes original
  • Confusing flatten() with ravel() behavior
  • Expecting arr[0,0] to change after modifying flat
4. The following code is intended to flatten a 2D array and modify the first element, but it does not change the original array. What is the mistake?
import numpy as np
arr = np.array([[5, 6], [7, 8]])
flat = arr.flatten()
flat[0] = 999
print(arr)
medium
A. Using flatten() which returns a copy, not a view
B. Forgetting to use parentheses with flatten
C. Modifying flat instead of arr directly
D. arr is not a NumPy array

Solution

  1. Step 1: Identify flatten() behavior

    flatten() returns a new copy of the array, so changes to flat do not affect arr.
  2. Step 2: Understand why original array is unchanged

    Since flat is a copy, modifying flat[0] does not change arr. To modify original, use ravel() or modify arr directly.
  3. Final Answer:

    Using flatten() which returns a copy, not a view -> Option A
  4. Quick Check:

    flatten() returns copy = Using flatten() which returns a copy, not a view [OK]
Hint: Use ravel() to get a view if you want to modify original [OK]
Common Mistakes:
  • Assuming flatten() returns a view
  • Not using parentheses with flatten()
  • Modifying the copy instead of original array
5. You have a large 3D NumPy array data and want to flatten it into 1D for fast processing without using extra memory. Which method should you use and why?
hard
A. Use data.ravel() because it returns a view if possible, saving memory.
B. Use data.tolist() to convert to list and flatten.
C. Use data.reshape(-1) because it always returns a copy.
D. Use data.flatten() because it returns a copy and is safer.

Solution

  1. 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.
  2. Step 2: Consider large data and performance

    For large arrays, avoiding copies saves memory and speeds up processing. ravel() is preferred for this reason.
  3. 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.
  4. Final Answer:

    Use data.ravel() because it returns a view if possible, saving memory. -> Option A
  5. Quick Check:

    ravel() view saves memory = Use data.ravel() because it returns a view if possible, saving memory. [OK]
Hint: Use ravel() for memory-efficient flattening [OK]
Common Mistakes:
  • Choosing flatten() and wasting memory
  • Confusing reshape(-1) behavior
  • Using tolist() which is slow and memory heavy
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard