Simple Arithmetic with NumPy Arrays
In NumPy, you can easily perform arithmetic operations like addition, subtraction, multiplication, and division directly on arrays using arithmetic operators (+, -, *, /). However, NumPy also provides dedicated functions that extend this functionality.
These functions can work not only with NumPy arrays but also with other array-like objects such as lists and tuples. A key feature of these functions is the where parameter, which allows you to perform operations conditionally—that is, only on elements that meet a certain condition.
1. Addition
The np.add() function adds corresponding elements from two arrays and returns the result as a new array.
Program:
Adding values from two arrays:
import numpy as np
arr1 = np.array([10, 11, 12, 13, 14, 15])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.add(arr1, arr2)
print(newarr)
Output:
[30 32 34 36 38 40]
2. Subtraction
The np.subtract() function subtracts the elements of one array from another.
Program:
Subtracting values in arr2 from arr1:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.subtract(arr1, arr2)
print(newarr)
Output:
[-10 -1 8 17 26 35]
3. Multiplication
The np.multiply() function multiplies corresponding elements from two arrays.
Program:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.multiply(arr1, arr2)
print(newarr)
Output:
[ 200 420 660 920 1200 1500]
4. Division
The np.divide() function divides elements of one array by another.
Program:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 10, 8, 2, 33])
newarr = np.divide(arr1, arr2)
print(newarr)
Output:
[ 3.33333333 4. 3. 5. 25. 1.81818182]
5. Power
The np.power() function raises elements of one array to the powers specified by another array.
Program:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 6, 8, 2, 33])
newarr = np.power(arr1, arr2)
print(newarr)
Output:
[ 1000 3200000 729000000 6553600000000 2500 0]
6. Remainder (Modulo)
The np.mod() and np.remainder() functions both return the remainder after division.
Program:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.mod(arr1, arr2)
print(newarr)
Output:
[1 6 3 0 0 27]
7. Quotient and Remainder Together
The np.divmod() function returns both the quotient and the remainder as a pair of arrays.
Program:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
quotient, remainder = np.divmod(arr1, arr2)
print((quotient, remainder))
Output:
(array([ 3, 2, 3, 5, 25, 1]), array([1, 6, 3, 0, 0, 27]))
8. Absolute Values
To get the absolute value of each element in an array, use np.absolute() or np.abs(). However, it’s recommended to use np.absolute() to avoid confusion with Python’s built-in abs().
Program:
import numpy as np
arr = np.array([-1, -2, 1, 2, 3, -4])
newarr = np.absolute(arr)
print(newarr)
Output:
[1 2 1 2 3 4]
Summary:
These NumPy arithmetic functions not only simplify operations but also provide greater flexibility with conditional execution using the where parameter, making them more versatile than using simple operators.