NumPy Arrays
Beginner
Create and Reshape a NumPy Array
Beginner
~8 min
code completion
Create and Reshape a NumPy Array
NumPy arrays are the building block of all numerical computing in Python. Unlike Python lists, they are stored contiguously in memory and support vectorized operations.
Your task:
1. Create a 1D array of integers from 1 to 12 (inclusive) using np.arange.
2. Reshape it into a 3×4 matrix (3 rows, 4 columns).
3. Return the reshaped matrix.
Hint: np.arange(start, stop) excludes stop. A 3×4 matrix has 3 rows and 4 columns.
Expected output shape: (3, 4) Expected first row: [1, 2, 3, 4] Expected last row: [9, 10, 11, 12]
Example Tests
Output shape is (3, 4)
Expected: [3,4]
First row is [1, 2, 3, 4]
Expected: [1,2,3,4]
Last element is 12
Expected: 12