Arithmetic Operations in Numpy Arrays
Arithmetic Operations in Numpy
As we already know the arithmetic operations like addition, subtraction, division, modulo division, floor division, etc. in python. The same arithmetic operations can be performed in the Numpy arrays.
Create an Numpy arrays
import numpy as np
data1 = np.array([1,2,3])
data2 = np.array([4,5,6])
Here, we create an arrays named 'data1' and 'data2' to perform arithmetic operations on it.
Addition in Numpy arrays
We can add two numpy arrays by using the symbol '+' is placed in between the two arrays.
Example
print("Addition :", data1 + data2)
Output
Addition : [5 7 9]
Subtraction in Numpy arrays
We can subtract two numpy arrays by using the symbol '-' is placed in between the two arrays.
Example
print("Subtraction :", data1 - data2)
Output
Subtraction : [-3 -3 -3]
Multiplication in Numpy arrays
We can multiply two numpy arrays by using the symbol '*' is placed in between the two arrays.
Example
print("Multiplication :", data1 * data2)
Output
Multiplication : [ 4 10 18]
Division in Numpy arrays
We can divide two numpy arrays by using the symbol '/' is placed in between the two arrays. It will return quotient as a result.
Recommended by LinkedIn
Example
print("Division :", data1 / data2)
Output
Division : [0.25 0.4 0.5 ]
Modulo Division in Numpy arrays
We can divide two numpy arrays by using the symbol '%' is placed in between the two arrays. It will return remainder as a result.
Example
print("Modulo Division :", data1 % data2)
Output
Modulo Division : [1 2 3]
Floor Division in Numpy arrays
We can divide two numpy arrays by using the symbol '//' is placed in between the two arrays. It will neglect the values after a decimal.
Example
print("Floor Division :", data1 // data2)
Output
Floor Division : [0 0 0]
Exponent in Numpy arrays
We can take exponent of two numpy arrays by using the symbol '**' is placed in between the two arrays.
Example
print("Exponent :", data1 ** data2)
Output
Exponent : [ 1 32 729]
Challenge: Try out the all above operations in 2D array and see the output.
Follow on LinkedIn - Trillionaire TK
Follow on Instagram - Trillionaire TK
Visit our website - Information Technology
Learn, Code, Practice!!!