You can directly use arithmetic operators (+, -, *, /) between NumPy arrays, but this section explores an extension that allows functions to accept any array-like objects, such as lists and tuples, and perform arithmetic conditionally.
Arithmetic conditionally refers to the ability to define specific conditions under which arithmetic operations will be executed. |
All the arithmetic functions discussed include a where parameter that allows us to specify the condition for the operation.
The add() function computes the sum of the contents of two arrays and returns the results in a new array.
Add the values from arr1 to the values in arr2:
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) |
The example above will yield [30, 32, 34, 36, 38, 40] , which represents the sums of 10+2010 + 20, 11+2111 + 21, 12+2212 + 22, and so on. |