Bird
0
0

How can you create a 3D array of shape (2, 3, 4) with random floats and then flatten it into 1D?

hard📝 Application Q9 of 15
NumPy - Creating Arrays
How can you create a 3D array of shape (2, 3, 4) with random floats and then flatten it into 1D?
Aarr = np.random.rand([2, 3, 4]) flat = arr.flatten()
Barr = np.random.rand(2, 3, 4) flat = arr.reshape(2, 3, 4)
Carr = np.random.rand(2, 3, 4) flat = arr.flatten()
Darr = np.random.rand(2, 3, 4) flat = arr.reshape(1, 24)
Step-by-Step Solution
Solution:
  1. Step 1: Create 3D array with correct syntax

    np.random.rand(2, 3, 4) creates 3D array of shape (2, 3, 4).
  2. Step 2: Flatten array to 1D

    Using arr.flatten() converts array to 1D with all elements.
  3. Final Answer:

    arr = np.random.rand(2, 3, 4) flat = arr.flatten() -> Option C
  4. Quick Check:

    flatten() makes 1D array [OK]
Quick Trick: Use flatten() to convert any array to 1D [OK]
Common Mistakes:
  • Passing list as shape
  • Using reshape with same shape
  • Reshaping to 2D instead of 1D

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes