Xtralien Scientific Python: Numpy

 

Numpy is a Python Module that enables accelerated Numerical manipulation in Python. The method by which this is achieved is by using C and Fortran for the internals of the module.

This use of low-level code can increase the performance of numerical operations by multiple orders of magnitude. This speed increase can be useful for many applications, including data/signal analysis.

Arrays

Arrays are one of the key parts of numpy. These arrays are like those found in C, and are the reason for the large speed-boost from Numpy.

list_1 = range(10000)
list_2 = range(10000)

list_3 = []

print("1:", list_3)
for x, y in zip(list_1, list_2): list_3.append(x + y)

print("2:", list_3)

The example above shows a typical Python method of adding together the numbers in two lists. On a typical instance of Python 3 this can take \(1.7ms\) per run.

from numpy import array, arange

list_1 = arange(10000)
list_2 = arange(10000)

list_3 = list_1 + list_2

print(list_3)

Note: The examples above import from the numpy module, however this is done for you in Xtralien Scientific Python. It is explicitly listed here for completeness.

The above example shows how fast numpy arrays can be. When run in the same Python instance as the previous code, each invocation lasts for approximately \(17µs\) (\(0.017ms\)) per invocation. This makes numpy approximately \(100\times\) faster than native Python lists for processing datasets.

The difference is that the second example uses numpy’s native array methods instead of Pythons range and list interfaces.

One other key point to notice is that the numpy code uses a more intuitive list_1 + list_2 instead of a for loop. This hides complexity and allows for more clarity in the algorithms beging developed, rather than forcing the user to focus on low-level details.

Broadcasting

Numpy arrays also have an ability to ‘broadcast’ operations. This means that even using Python methods and operators, such as + will be made faster by applying to every element of the array at once.

number_array = arange(10000)
number_array += 10000

By the finish of the above example, number_array will contain the integers from 10000 to 19999 inclusive. This is useful to enable operations such as centering around the mean of data.