NumPy - Creating ArraysHow 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)Check Answer
Step-by-Step SolutionSolution:Step 1: Create 3D array with correct syntaxnp.random.rand(2, 3, 4) creates 3D array of shape (2, 3, 4).Step 2: Flatten array to 1DUsing arr.flatten() converts array to 1D with all elements.Final Answer:arr = np.random.rand(2, 3, 4) flat = arr.flatten() -> Option CQuick Check:flatten() makes 1D array [OK]Quick Trick: Use flatten() to convert any array to 1D [OK]Common Mistakes:Passing list as shapeUsing reshape with same shapeReshaping to 2D instead of 1D
Master "Creating Arrays" in NumPy9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More NumPy Quizzes Array Data Types - Complex number type - Quiz 5medium Array Manipulation - flatten() and ravel() for 1D conversion - Quiz 3easy Broadcasting - Broadcasting errors and debugging - Quiz 5medium Broadcasting - Common broadcasting patterns - Quiz 14medium Creating Arrays - np.arange() for range arrays - Quiz 11easy Creating Arrays - np.array() from Python lists - Quiz 10hard Creating Arrays - np.array() from Python lists - Quiz 15hard Indexing and Slicing - Why indexing matters - Quiz 9hard NumPy Fundamentals - Why NumPy over Python lists - Quiz 14medium NumPy Fundamentals - Array attributes (shape, dtype, ndim, size) - Quiz 12easy