= np.array([1, 2, 3, 4, 5])
a
= np.array([9, 8, 7, 6, 5]) b
4 Working with Arrays
4.1 Arithmetic Functions
One major advantage of NumPy is working with arrays. Without NumPy, you would either have to use a loop
or a list comprehension
to perform operations on all values in a list. NumPy eliminates this inconvenience.
Basic mathematical operations like addition can be expressed in two ways: either using the np.add()
function or simply with the +
operator.
np.add(a,b)
array([10, 10, 10, 10, 10])
+ b a
array([10, 10, 10, 10, 10])
Without NumPy, the operation would look like this:
= np.ones(5)
result for i in range(len(a)):
= a[i] + b[i]
result[i]
print(result)
[10. 10. 10. 10. 10.]
For other types of arithmetic, there are functions like: np.subtract()
, np.multiply()
, and np.divide()
.
Higher-level mathematical operations also have functions:
np.exp(a)
np.sqrt(a)
np.power(a, 3)
np.sin(a)
np.cos(a)
np.tan(a)
np.log(a)
a.dot(b)
Just like with a calculator, a common error when using trigonometric functions (sin, cos, …) is to input degrees instead of radians. However, the trigonometric functions in NumPy expect values in radians.
To easily convert between degrees and radians, NumPy provides the functions np.deg2rad()
and np.rad2deg()
.
4.2 Comparisons
NumPy arrays can also be compared with one another. Let’s look at the following two arrays:
= np.array([1, 2, 3, 4, 5])
a
= np.array([9, 2, 7, 4, 5]) b
To check whether these arrays are identical, we can use the ==
comparator. This compares the arrays element-wise.
== b a
array([False, True, False, True, True])
You can also compare arrays using the >
and <
operators:
< b a
array([ True, False, True, False, False])
When comparing arrays with floating point numbers, it is often necessary to allow for some tolerance due to small rounding errors in computations.
= np.array(0.1 + 0.2)
a = np.array(0.3)
b == b a
np.False_
For this case, NumPy offers a comparison function np.isclose(a,b,atol)
, where atol
stands for absolute tolerance. In the following example, an absolute tolerance of 0.001 is used.
= np.array(0.1 + 0.2)
a = np.array(0.3)
b print(np.isclose(a, b, atol=0.001))
True
Numbers are internally represented in binary. Just like 1/3 cannot be represented precisely with a finite number of decimal digits, some numbers must be rounded in binary representation.
= 0.1
a = 0.2
b print(a + b)
0.30000000000000004
4.3 Aggregation Functions
For many types of analysis, we need functions such as sum or mean. Let’s start with an example array a
:
= np.array([1, 2, 3, 4, 8]) a
The sum is calculated using the np.sum()
function.
sum(a) np.
np.int64(18)
Of course, you can also determine the minimum and maximum of an array. The functions are np.min()
and np.max()
.
min(a) np.
np.int64(1)
If you want the position of the maximum value instead of the value itself, use np.argmax
instead of np.max
.
For statistical analysis, common functions are np.mean()
for the mean, np.median()
for the median, and np.std()
for the standard deviation.
np.mean(a)
np.float64(3.6)
np.median(a)
np.float64(3.0)
np.std(a)
np.float64(2.4166091947189146)
Given two one-dimensional arrays a
and b
:
a = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) and
b = np.array([5, 15, 25, 35, 45, 55, 65, 75, 85, 95])
- Create a new array that contains the sine values of the added arrays
a
andb
. - Calculate the sum, mean, and standard deviation of the elements in
a
. - Find the largest and smallest values in both
a
andb
.