Some NumPy Basic Functions
There are a substantial number of functions available through NumPy, much too numerous to go through here. However, this section will highlight the use of a few simple functions and give links to the NumPy documentation for commonly used mathematical functions.
The following example illustrates computing an average from a 2D array, read in using a NumPy function.
input.txt:
34 56 78 91
12 23 33 51
20 39 48 57
Code:
import numpy as np
filename = 'input.txt'
data = np.loadtxt(filename)
avg_data = np.average(data)
print(avg_data)
Output:
45.1666666667
Some Common Mathematical Operations:
- Average over all or parts of an array -
average()
- Sum of the elements of an array -
sum()
- Square Root of all elements -
sqrt()
- Maximum over an array -
max()
oramax
- Minimum over an array -
min()
oramin()
- Standard deviation over all the elements of an array -
std()
- Trigonometric Functions (expect angle in radians)
- Change from Degrees (angle) to radians -
deg2rad()
orradians()