Creating arrays in Python
The array constructors and shape basics of NumPy.
import numpy as np
zeros = np.zeros(5)
ramp = np.arange(0, 10, 2)
grid = np.linspace(0, 1, 5)
ones = np.ones((2, 3))
data = np.array([[1, 2, 3], [4, 5, 6]])
shape = data.shape
kind = data.dtype
reshaped = data.reshape(3, 2)
flat = data.ravel()
How it works
zeros,arange, andlinspacebuild arrays.shapeanddtypedescribe an array.reshapeandravelchange its layout.
Keywords and builtins used here
as
The run, in numbers
- Lines
- 12
- Characters to type
- 245
- Tokens
- 99
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 59 seconds.
Step 1 of 2 in Arrays, step 1 of 22 in NumPy.