In this tutorial, we will see how to perform basic arithmetic operations, apply trigonometric and logarithmic functions on the array elements of a NumPy array. We will also see how to find sum, mean, maximum and minimum of elements of a NumPy array and then we will also see how to perform matrix multiplication using NumPy arrays.
Arithmetic Operations on NumPy Arrays:
In NumPy, Arithmetic operations are element-wise operations. i.e. we can perform arithmetic operations on the entire array and every element of the array gets updated by the same operation.
1. Basic Math Operations:
For example, suppose we have an array ‘A’ with elements from 1 to 10 and we want to add 4 to each element. We can do this by simply doing ‘A+4’ and we don’t have to iterate the whole array and then add 4 to each element.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
C=A+4
print("Array C is:")
print(C)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Array C is:
[ 5 6 7 8 9 10 11 12 13 14]
Similarly, we can perform subtraction, division, multiplication etc. on NumPy arrays.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
D=A-2
print("Array D is:")
print(D)
E=A/2
print("Array E is:")
print(E)
F=A*2
print("Array F is:")
print(F)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Array D is:
[-1 0 1 2 3 4 5 6 7 8]
Array E is:
[0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. ]
Array F is:
[ 2 4 6 8 10 12 14 16 18 20]
There are many functions which are called universal functions and which operate on a NumPy array in an element by element manner. Functions to calculate trigonometric ratios, logarithm and square root are some of the universal functions.
2. Square Root:
We can calculate the square root of each element of an array by simply using np.sqrt() function and passing the array as a parameter. The output is also an array with the same shape that of the input array and elements having square roots of the input array.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
B=np.sqrt(A)
print("Array B is :")
print(B)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Array B is :
[1. 1.41421356 1.73205081 2. 2.23606798 2.44948974
2.64575131 2.82842712 3. 3.16227766]
3. Logarithm:
To calculate the logarithm of each element of the NumPy array, we can use np.log() function and pass the input array as a parameter to it. The output is a new array of the same shape of the input array and having elements as logarithms of elements of the input array.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
C=np.log(A)
print("Array C is :")
print(C)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Array C is :
[0. 0.69314718 1.09861229 1.38629436 1.60943791 1.79175947
1.94591015 2.07944154 2.19722458 2.30258509]
4. Trigonometric functions
Trigonometric functions can also be applied on NumPy array in a similar manner to sqrt and log functions.
4.1 Sin:
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
D=np.sin(A)
print("Array D is :")
print(D)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Array D is :
[ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427 -0.2794155
0.6569866 0.98935825 0.41211849 -0.54402111]
4.2 Cosine:
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
E=np.cos(A)
print("Array E is :")
print(E)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Array E is :
[ 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219 0.96017029
0.75390225 -0.14550003 -0.91113026 -0.83907153]
4.3 tan:
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
F=np.tan(A)
print("Array F is :")
print(F)
5. Aggregations:
There are also aggregate functions which perform an operation on the whole array and produce a single result. These functions include calculation of sum, minimum, maximum, mean and standard deviation of all the elements of a NumPy array.
5.1 Sum of elements of an array:
We can calculate the sum of elements of a given NumPy array using sum() method. Suppose we have to calculate the sum of elements of a NumPy array A, then we can simply call A.sum() and it returns the sum of all the elements in A.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
b= A.sum()
print("sum of elements of array A is:")
print(b)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
sum of elements of array A is:
55
5.2 Min number in an array:
To find the minimum element in a NumPy array A, we can simply call A.min() method and it returns the minimum number among the elements of A.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
min=A.min()
print("Minimum number among the elements of A is:")
print(min)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Minimum number among the elements of A is:
1
5.3 Max number in an array:
We can find maximum element in a NumPy array A by using method A.max() and it returns maximum number among all the elements of the array.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
max=A.max()
print("Maximum number among the elements of A is:")
print(max)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Maximum number among the elements of A is:
10
5.4 Average of elements of an array:
We can find the average value/mean of elements of a NumPy array by using the mean() method.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)
e= A.mean()
print("Average of elements of array A is:")
print(e)
Output:
Array A is:
[ 1 2 3 4 5 6 7 8 9 10]
Average of elements of array A is:
5.5
6. References:
Happy Learning 🙂