5. NumPy#

NumPy is a central package in the Python ecosystem and is used by virtually every science-related module. The main goal of NumPy is to perform mathematical operations not only on floats, but also on vectors and matrices, and to perform these operations very quickly.

As an example, let’s say we need to average multiple measurements. We could do this easily using basic Python code, by summing every element of a list, and then by dividing the sum by the number of elements:

list_of_float = [1.0, 2.0, 3.0, 2.5, 2.75, 1.5]

# Calculate the sum of every elements
sum = 0
for one_float in list_of_float:
    sum += one_float
    
# Divide by the number of elements
mean = sum / len(list_of_float)

mean
2.125

NumPy makes it both easier and faster:

import numpy as np

np.mean(list_of_float)
2.125

This chapter covers the basics of NumPy for biomechanics.

Tip

If you already know Matlab, then this cheat sheet could be useful.

Chapter Contents