NumPy ArraysIntro
Create and Reshape a NumPy Array
~8 mincode completion
- Create a 1D array of integers from 1 to 12 (inclusive) using .
- Reshape it into a 3×4 matrix (3 rows, 4 columns).
- Return the reshaped matrix.
Hint: excludes stop. A 3×4 matrix has 3 rows and 4 columns.
Expected output : (3, 4) Expected first row: [1, 2, 3, 4] Expected last row: [9, 10, 11, 12]
Examples
Output shape is (3, 4)
- Input
- create_matrix()
- shape of result
- [3, 4]
First row is [1, 2, 3, 4]
- Input
- create_matrix()
- first row of result
- [1, 2, 3, 4]
Last element is 12
- Input
- create_matrix()
- last element of result
- 12
Hints
Hint 1
Reshape so the two arrays broadcast against each other.
Hint 2
Watch for this: wrong range.
Requirements
Implement
create_matrixand return its result.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~8 min
••••••••••••••••
8 employers weight this skill
3 frontier labs, 2 quant funds, 2 big tech firms, 1 autonomy company. Top match scores 68.
Python
import numpy as np
def create_matrix() -> np.ndarray:
"""
Returns a 3x4 matrix containing integers 1 through 12.
"""
# YOUR CODE HERE
pass