Bird
Raised Fist0
NumPydata~5 mins

flatten() and ravel() for 1D conversion in NumPy

Choose your learning style10 modes available
Introduction

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.

When you want to see all numbers in a matrix as a single list.
When you need to prepare data for a function that only accepts 1D input.
When you want to simplify complex data shapes for easier calculations.
When you want to quickly check or print all elements in an array.
When you want to convert image pixel data into a single row for analysis.
Syntax
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.

Examples
This makes a new 1D array from arr with all elements in order.
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat_arr = arr.flatten()
This creates a 1D view of arr if possible, sharing the same data.
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
raveled_arr = arr.ravel()
Changing raveled_arr changes arr because ravel() returns a view.
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
raveled_arr = arr.ravel()
raveled_arr[0] = 10
print(arr)
Changing flat_arr does NOT change arr because flatten() returns a copy.
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat_arr = arr.flatten()
flat_arr[0] = 10
print(arr)
Sample Program

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.

NumPy
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)
OutputSuccess
Important Notes

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).

Summary

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

(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