Calculating with arrays

Built-in python data types (lists, dictionaries, etc.) are fine for many applications. For mathematical operations, however, these types are not so flexible and fast. This is why the numpy module was created, which is now the base for most python scientific code.

The core of numpy is written in the low-level C programming language, so all computations are executed very fast. Moreover, computations with numpy arrays look very similar to the usual mathematical notations and this makes them very easy to read.

Creating a simple numpy array

To use numpy you first need to import it:

>>> import numpy as np

This means that in our code we will call the numpy module with the short name np.

We can now create a first array. One way to create the array is to define all its elements:

>>> a = np.array([1, 2, 3])

Several things happen in this one line:

  • We define a elements of our array in a list i.e. [1, 2, 3];
  • We convert this list to a numpy array using the array function of the numpy module i.e. np.array()
  • We store the resulting array in a variable called a.

The variable a is now a numpy array, suitable for mathematical computations:

>>> print(a)
[1 2 3]

>>> print(2 * a)  # Multiply by 2
[2 4 6]

>>> print(a**2)   # Square, element by element
[1 4 9]

We can, of course, store our results in a new array for further use:

>>> b = a**2
>>> print(b)
[1 4 9]

>>> print(a + b)
[2, 6, 12]

Convenient ways to create numpy arrays

The numpy module provides some functions that can create standard numpy arrays easily.

np.ones(<number of elements>)

Creates an array of ones:

>>> a = np.ones(5)
>>> print(a)
[1.  1.  1.  1.  1.]

Note the dots after the numpy 1. This stands for 1.0 i.e. the elements are of type float not integers.

np.zeros(<number of elements>)

Similarly, creates an array of zeros:

>>> a = np.zeros(5)
>>> print(a)
[0.  0.  0.  0.  0.]
np.arange(<stop number>)

Creates an array with a sequence of numbers:

>>> a = np.arange(5)
>>> print(a)
[0 1 2 3 4]

You can also call the arange function with two arguments, defining both the start and stop number e.g.:

>>> a = np.arange(5, 10)
>>> print(a)
[5 6 7 8 9]

As with any python function, you can read the documentation of arange in ipython using the question mark:

>>> np.arange?
Type:        builtin_function_or_method
String form: <built-in function arange>
Docstring:
arange([start,] stop[, step,], dtype=None)

Return evenly spaced values within a given interval.

Values are generated within the half-open interval ``[start, stop)``
(in other words, the interval including `start` but excluding `stop`).
For integer arguments the function is equivalent to the Python built-in
`range <http://docs.python.org/lib/built-in-funcs.html>`_ function,
but returns an ndarray rather than a list.

[...]

Remember you need to type q to exit the documentation view.

Accessing specific elements of an array

You can access specific elements of an array by using brackets notation. Note, that the first element of an array is called the element 0. For example:

>>> a = np.array([4, 5, 6, 7])
>>> print(a)
[4 5 6 7]

>>> print(a[0])
4

>>> print(a[2])
6

You can get a range of elements using the notation [<start index>:<stop index>]. The result is a new numpy array with the specified elements:

>>> print(a[0:3])
[4 5 6]

>>> print(a[1:3])
[5 6]

Skipping the start means to start from the first element:

>>> print(a[:2])  # Equivalent to a[0:2]
[4 5]

Similarly, skipping the end index means to get all the elements until the last:

>>> print(a[2:])
[6 7]

Process arrays using numpy functions

The numpy module provides many convenient arrays to perform usual mathematical calculations.

You can find the average value of an array using the mean function:

>>> a = np.array([1, 2, 3, 4])
>>> a_mean = np.mean(a)
>>> print(a_mean)
2.5

You can also calculate the minimum, maximum, and median value easily:

>>> a_min = np.min(a)
>>> print(a_min)
1

>>> a_max = np.max(a)
>>> print(a_max)
4

>>> a_median = np.max(a)
>>> print(a_median)
2.5

See also

A more advanced introduction in numpy can be found in the tentative numpy tutorial.

Check the numpy list of array creating routines included in numpy.

Check the numpy list of statistical functions included in numpy.

If you are familiar with Matlab programming this comparison can help you understand the similarities and differences of Matlab matrices and numpy arrays.