Objectives:
Upon completion of this lesson, you should be able to:
Create N-dimensional arrays
Index values in those arrays
Perform operations on those arrays
Know what "broadcasting" is and how it works
Python has strings, integers, floating point types for numerics, and containers (which ones?) for storing (heterogeneous) collections of those (convenience) --- but more is needed (efficiency)
Numpy is a Python package for multidimensional arrays, designed for scientific computation (efficiency and convenience)
An array containing...
discretized time of an experiment/simulation
signal recorded by a measurement device
pixels of an image
voxels of a volume
...
axes
. The number of axes is a ''rank'' of the arrayFor example
Coordinates of a point in 3D space: [1, 2, 1]
Is an array of rank 1, because it has one axis
That axis has a length of 3
import numpy as np
np.lookfor("holy")
Numpy's array class is called ndarray
. (It is also known by the alias array
)
the Python world, the number of dimensions is referred to as rank
integers indicating the size of the array in each dimension
equal to the product of the elements of shape
array
a = np.array([0, 1, 2, 3])
print(repr(a), a)
print(a.ndim)
print(a.shape)
print(len(a))
b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array
print(repr(b))
print(b)
print(b.ndim)
print(b.shape)
print(len(b)) # returns the size of the first dimension
c = np.array([[[1], [2]], [[3], [4]]])
print(c)
print(c.shape)
We rarely enter items one by one...
import numpy as np
np.arange(10) # 0 .. n-1 (!)
np.arange(1, 9, 2) # start, end (exlusive), step
or by number of points using np.linspace()
:
np.linspace(0, 1, 6) # start, end, num-points
np.linspace(0, 1, 5, endpoint=False)
np.ones((3, 3)) # reminder: (3, 3) is a tuple
np.zeros((2, 2))
np.eye(3)
np.diag(np.array([1, 2, 3, 4, 5]))
numpy.random provides many routines to generate various pseudo-random numbers using PRNG (pseudo-random numbers generator)
np.random.rand(4) # uniform in [0, 1]
np.random.rand(3, 3)
np.random.randn(4) # gaussian
If you would like "reproducible" random numbers you can seed the PRNG globally:
np.random.seed(1)
or create an instance of the PRNG to be used in your particular code (preferred, so there is no side-effects):
prng = np.random.RandomState(1)
prng.rand(3)
You probably noted the 1
and 1.
above. These are different
data types:
a = np.array([1, 2, 3])
a.dtype
b = np.array([1., 2., 3.])
b.dtype
Warning Much of the time you don't necessarily need to care, but remember about the same gotcha as with regular Python2 "int":
a / 3
so you would need to cast one of the arguments to float:
a/3.
a.astype(float)/3
You can control your data type destiny:
c = np.array([1, 2, 3], dtype=float)
c.dtype
The default data type is floating point:
a = np.ones((3, 3))
a.dtype
There are also other types:
np.array([1+2j, 3+4j, 5+6*1j]).dtype
np.array([True, False, False, True]).dtype
np.array(['Bonjour', 'Hello', 'Hallo', 'Terve', 'Hej']).dtype
from IPython.display import YouTubeVideo
YouTubeVideo('EEUXKG97YRw')