NumPy Arrays and dtypes
Create ndarrays, understand dtypes, and explore array properties.
What Is NumPy?
NumPy provides the ndarray — a fixed-type, contiguous-memory array that supports vectorised operations orders of magnitude faster than Python lists.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
print(arr * 2) # [ 2 4 6 8 10] (no Python loop)Creating Arrays
Common creation functions: np.array(), np.zeros(), np.ones(), np.arange(), np.linspace().
import numpy as np
print(np.zeros((2, 3))) # 2x3 array of 0.0
print(np.ones(5)) # [1. 1. 1. 1. 1.]
print(np.arange(0, 10, 2)) # [0 2 4 6 8]
print(np.linspace(0, 1, 5)) # [0. 0.25 0.5 0.75 1. ]