31 Mar Arithmetic Operations on Numpy arrays
Perform arithmetic operations on Numpy arrays using the sum(), subtract(), multiply(), and divide() methods. Let us see the operations one by one:
- Add Numpy Arrays
- Subtract Numpy Arrays
- Multiply Numpy Arrays
- Divide Numpy Arrays
Add Numpy arrays
To add the elements of NumPy arrays, the sum() method is used. It adds each and every element of both arrays and displays the result in a new array.
Let us see the following examples to add arrays:
- Add the elements of Numpy arrays
- Add column values using the axis parameter
- Add individual array values using the axis parameter
Example1: Add the elements of Numpy arrays
import numpy as np
# Create arrays
n1 = np.array([15, 20, 25, 30])
n2 = np.array([65, 75, 85, 95])
print("Array1 =", n1)
print("Array2 =", n2)
# Add the arrays elements
resarr = np.sum([n1, n2])
print("Sum = ", resarr)
Output
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Sum = 410
Example2: Add column values using the axis parameter
import numpy as np
# Create two arrays
n1 = np.array([15, 20, 25, 30])
n2 = np.array([65, 75, 85, 95])
print("Array1 =", n1)
print("Array2 =", n2)
# Add column values using axis parameter
resarr = np.sum([n1, n2], axis=0)
print("Sum = ", resarr)
Output
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Sum = [ 80 95 110 125]
Example 3: Add individual array values using the axis parameter
import numpy as np
# Create two arrays
n1 = np.array([15, 20, 25, 30])
n2 = np.array([65, 75, 85, 95])
print("Array1 =", n1)
print("Array2 =", n2)
# Add individual array values using axis parameter
resarr = np.sum([n1, n2], axis=1)
print("Sum result= ", resarr)
Output
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Sum result= [ 90 320]
Subtract Numpy arrays
Use the subtract() method to subtract one array from another. It subtracts and displays the result in a new array. Let us see an example to subtract NumPy arrays:
import numpy as np
# Create two arrays
n1 = np.array([15, 20, 25, 30])
n2 = np.array([65, 75, 85, 95])
print("Array1 =", n1)
print("Array2 =", n2)
# Subtract Numpy arrays
resarr = np.subtract(n1, n2)
print("Subtraction result = ", resarr)
Output
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Subtraction result = [-50 -55 -60 -65]
Multiply Numpy arrays
Use the multiply() method to multiply elements of one array to another. It multiplies and displays the result in a new array. Let us see an example to multiply NumPy arrays:
import numpy as np
# Create Numpy arrays
n1 = np.array([15, 20, 25, 30])
n2 = np.array([65, 75, 85, 95])
print("Array1 =", n1)
print("Array2 =", n2)
# Multiply Numpy arrays
resarr = np.multiply(n1, n2)
print("Multiplication result = ", resarr)
Output
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Multiplication result = [ 975 1500 2125 2850]
Divide Numpy arrays
The divide() method divides elements of one array with elements of another. It divides and displays the result in a new array. Let us see an example to divide NumPy arrays:
import numpy as np
# Create two Numpy arrays
n1 = np.array([15, 20, 25, 30])
n2 = np.array([65, 75, 85, 95])
print("Array1 =", n1)
print("Array2 =", n2)
# Divide arrays
resarr = np.divide(n2, n1)
print("Division Result = ", resarr)
Output
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Division Result = [4.33333333 3.75 3.4 3.16666667]
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments