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
- 5.1. Importing NumPy
- 5.2. Arrays
- 5.3. Arithmetics
- 5.4. Matrix multiplication
- 5.5. Trigonometry
- 5.6. Infinity and Not-A-Number (nan)
- 5.7. Statistical functions
- 5.8. Comparisons
- 5.9. Logical operators
- 5.10. Indexing and slicing unidimensional arrays
- 5.11. Filtering unidimensional arrays
- 5.12. Multidimensional arrays
- 5.13. Exercises