Let's import the numpy
module.
import numpy as np
n = 10 # CHANGE ME
a1 = list(range(n))
a2 = np.arange(n)
if n <= 10:
print(a1)
print(a2)
%timeit [i**2 for i in a1]
%timeit a2**2
Numpy Arrays: much less flexible, but:
Ways to create a numpy array:
np.array([1,2,3])
linspace
np.linspace(-1, 1, 10)
zeros
np.zeros((10,10), np.float64)
Operations on arrays propagate to all elements:
a = np.array([1.2, 3, 4])
b = np.array([0.5, 0, 1])
Addition, multiplication, power, .. are all elementwise:
a+b
a*b
a**b
Matrix multiplication is np.dot(A, B)
for two 2D arrays.
Numpy arrays have two (most) important attributes:
a = np.random.rand(5, 4, 3)
a.shape
a.dtype
Other dtype
s include np.complex64
, np.int32
, ...