Arrays#

While lists and tuples go a long way, they are inherently 1D-structures - whereas images, for instances are 2D structures. Numpy provides such data structures and represents one of the cornerstones of the Python ecosystem. To use it, we need to import it. Whenever we want to use a function from this package, we need to use it like this:

package.function()

Hence, it is often desirable to use an alias for often-used packages:

import numpy as np

Lists can be converted in numpy arrays like this:

measurements = [5.5, 6.3, 7.2, 8.0, 8.8]
measurements2 = np.asarray(measurements)
measurements2
array([5.5, 6.3, 7.2, 8. , 8.8])

Accessing elements of an array works exactly as it does with lists:

measurements2[:3]
array([5.5, 6.3, 7.2])

Numpy arrays allow to perform mathematical operations in a quite simple fashion:

measurements2 * 2
array([11. , 12.6, 14.4, 16. , 17.6])
measurements2 + 1
array([6.5, 7.3, 8.2, 9. , 9.8])

Masking numpy arrays#

Masking or logical indexing are the term used for selecting entries in arrays, e.g. depending on its content. In order to do this, we need to use numpy arrays.

array = np.asarray([1, 17, 25, 3, 5, 26, 12])
array
array([ 1, 17, 25,  3,  5, 26, 12])

Next, we create a mask, e.g. a mask that defines all measurements which are above a given threshold:

mask = array > 10
mask
array([False,  True,  True, False, False,  True,  True])

We can apply that mask to our data to retrieve a new array that only contains masked values.

measurements[mask]
array([17, 25, 26, 12])

Sidenote: If we inspect the datatype of the array, we see something interesting - the array “knows” what sort of values (numbers, strings, etc.) are stored inside it!

print('Datatype of variable `array`: ', array.dtype)
print('Datatype of variable `mask`: ', mask.dtype)
Datatype of variable `array`:  int32
Datatype of variable `mask`:  bool

Exercises#

Create a new mask for all measurements below 20.

Apply the mask to retrieve a new array with numbers below 20.

Compute the average of all numbers below 20. Hint: There is probably a numpy function for all basic operations/calculations.