An overview of numpy#

Importing the numpy package#

Numpy is a third-party python module designed to make numerical computations in python easy and performant. In python, a module (a.k.a. package or library) is essentially a bundle of codes that extends the functionality of core python. And third-party here just means that you need to install the module on top of the standard python distribution—don’t worry, for your convenience, numpy (and all the other third-party packages you’ll encounter in this course) is already pre-installed on the JupyterHub.

The main object introduced by numpy is the N-dimensional arrays ndarray, which, similar to python list, is a sequential collection type. However, it differs from python list in a few important ways:

  • Numpy ndarray is intended for homogeneous data (i.e., data for which all values have the same data type), while python list can easily accommodate heterogeneous data.

  • Numpy ndarray is efficient in both memory and compute time when its data are numeric, while python list can be inefficient in both.

  • Numpy ndarray support vectorized operations, while python list needs to be operated on element-by-element.

In addition, numpy also provides a vast array (no pun intended!) of functions to create and operate on ndarray’s.

To use the functionality provided by the numpy module, we first need to import it. Typically, we’ll do:

import numpy as np

In the above line, the import numpy part is mandatory, while the as np part is optional. Without the as np part, we’ll have to refer to functions provided by numpy using the numpy prefix, e.g., numpy.sin() for its sine function. With the as np part, we create an alias named np for the same purpose, so we just need to write np.sin() instead of numpy.sin(). By the way, the np alias in this case is more-or-less standard, and you’ll see this import line everywhere.

Numpy provides a number of functions to create an ndarray, the simplest of which is to just wrap a python list around a np.array() call, like so:

x = np.array([1, 4, 9, 16, 25])
y = np.array([-2, -1, 0, 1, 2])

An ndarray (say x) has a few attributes associated with it. An attribute of a python object is accessed using a . followed by the attribute name. Unlike methods there is no call () at the end.

The first attribute of note is the size of the ndarray, which gives the number of elements the array has:

x.size
5

Another useful attribute is shape of the array. For 1-dimensional ndarray this is just the size placed inside a tuple (A tuple is like a python list, except it cannot be overwritten):

x.shape
(5,)

Finally, we have the ndim (number of dimensions) of the array:

x.ndim
1

Vectorized arithmetic and logical comparisons#

In contrast to python list’s, you can perform vectorized arithmetic on ndarray’s. For example,

z = x + y
print(z)
[-1  3  9 17 27]

You can also perform arithmetic between a scalar and an ndarray, like so:

w = x * 2
print(w)
[ 2  8 18 32 50]

The operators -, *, /, //, %, and ** works similarly. Moreover, one can also perform vectorized logical comparison. For example:

x < 10
array([ True,  True,  True, False, False])

(The reason why you may want to do so will become apparent soon). The operators ==, !=, >, <=, and >= work similarly.

To combine two logical ndarray, use & for “and” and | for “or”:

(x < 10) & (x > 3)
array([False,  True,  True, False, False])
(y > 1) | (y < -1)
array([ True, False, False, False,  True])

Finally, a logical ndarray can be negated by ~

u = np.array([True, False, False, True, True])
~u
array([False,  True,  True, False, False])