In a numpy array, we will easily perform arithmetic operations. Additionally, numpy provides a corresponding built-in function for every arithmetic operations. So in this post, we will learn about each of those functions and operations with detailed explanations and examples.

You can also check out our previous article 👉Shape and Reshape in NumPy Array

Now let’s start learning…

List of arithmetic operations in numpy that you can directly perform or use built-in functions related to it.

Arithmetic OperationsRespective Built-in Functions
a+bnumpy.add(a,b)
a-bnumpy.subtract(a,b)
a*bnumpy.multiply(a,b)
a/bnumpy.divide(a,b)
a%bnumpy.mod(a,b)
a**bnumpy.power(a,b)
1/anumpy.reciprocal(a)

let’s Understand each of those exam arithmetic operations. with the help of an example.

Addition Operation

The add() function is used to perform the addition operation in numpy or you can directly use the + operator for addition.

import numpy as np

arr1 = np.array([1,2,3,4,5])

arr1_add = arr1 + 3
print(arr1_add)

Output:

[4 5 6 7 8]

In above program, 3 is added to each element of arr1, and the results are stored in arr1_add.

Similarly, you can also add two arrays.

import numpy as np

arr1 = np.array([1,2,3,4,5])
arr2 = np.array([6,7,8,9,10])

arr1_add = arr1 + arr2
print(arr1_add)

Output:

[ 7  9 11 13 15]

As we already discussed, numpy provides a built-in function for each corresponding arithmetic operation. So we can also perform the above task using a add() function of numpy.

import numpy as np

arr1 = np.array([1,2,3,4,5])
arr2 = np.array([6,7,8,9,10])

arr1_add = np.add(arr1,arr2)
print(arr1_add)

Output:

[ 7  9 11 13 15]

Subtraction Operation

import numpy as np

var1 = np.array([2,12,11,6,4,89])
var2 = np.array([1,3,18,19,21,34])

var_sub = var1 - var2
print(var_sub)

Output:

[  1   9  -7 -13 -17  55]

You can also use the subtract() function to subtract two or more arrays.

import numpy as np

var1 = np.array([2,12,11,6,4,89])
var2 = np.array([1,3,18,19,21,34])

var_sub = np.subtract(var1,var2)
print(var_sub)

Output:

[  1   9  -7 -13 -17  55]

Similar to addition and subtraction, you can perform all other arithmetic operations, Let’s quickly explore each of them with the help of examples.

Multiplication Operation

The multiply() is used to multiply two or more arrays or you can directly use the * operator for multiplication.

import numpy as np

# Multiply array with single element
arr = np.array([7,8,9])

arr_mul1 = arr * 3
print(arr_mul1) # [21 24 27]




# Multiply two array
a1 = np.array([1,2,3])
a2 = np.array([4,5,6])

arr_mul2 = a1 * a2  # it multiply corresponding elements of the arrays 
print(arr_mul2) # [ 4 10 18]



# Multiply two array using built-in function
a3 = np.array([1,2,3,4,5])
a4 = np.array([1,2,4,3,1])

arr_mul3 = np.multiply(a3,a4) # it multiply corresponding elements of the arrays 
print(arr_mul3) # [ 1  4 12 12  5]

Output:

[21 24 27]
[ 4 10 18]
[ 1  4 12 12  5]

Division Operation

You can use the divide() function or / operator to perform division operations in a numpy array.

import numpy as np

# divide array with single element
a1 = np.array([12,45,21])
a1_div = a1/3

print(a1_div)  # [ 4. 15.  7.]



# divide two array using '/' operator
a2 = np.array([1,2,3])
a3 = np.array([5,6,7])

a23_div = a2 / a3 # it divide corresponding elements of the arrays 
print(a23_div) # [0.2        0.33333333 0.42857143]



# divide two arrays using built-in function
a4 = np.array([27,81,45,18,63])
a5 = np.array([3,9,5,2,7])

a45_div = np.divide(a4,a5) # it divide corresponding elements of the arrays 
print(a45_div) # [9. 9. 9. 9. 9.]

Output:

[ 4. 15.  7.]
[0.2        0.33333333 0.42857143]
[9. 9. 9. 9. 9.]

Similarly, you can also perform integer division operations using the // operator.

Moduleus Operation

“The mod() function divides two numbers or arrays and returns their remainders, or you can use the % operator to directly perform the modulus operation.

import numpy as np

a1 = np.array([12,14,16,18,20])
a1_mod = a1%5 

print(a1_mod) # [2 4 1 3 0]


# Using built-in function
a2 = np.array([12,15,17,19,21])
a3 = np.array([1,2,3,4,5])

a23_mod = np.mod(a2,a3) # it divide corresponding elements of the arrays and return new array with their remainders 

print(a23_mod) # [0 1 2 3 1]

# instead of mod() function, you can also use % operator 

Output:

[2 4 1 3 0]
[0 1 2 3 1]

numpy.power()

the power() function is used to calculate the element-wise exponentiation of an array. It raises each element in the input array to the power of the corresponding element in another array or a scalar. The result is a new array with the same shape as the input.

Instead of the power() function you can also use the ** operator.

import numpy as np

arr = np.array([1,2,3,4,5])
arr2 = np.array([1,2,3,1,2])


arr_pow = arr**arr2
print(arr_pow) # [ 1  4 27  4 25]


# using built-in function
arr_pow2 = np.power(arr,arr2)
print(arr_pow2) # [ 1  4 27  4 25]

Output:

[ 1  4 27  4 25]
[ 1  4 27  4 25]

numpy.reciprocal()

The reciprocal() function is used to compute the element-wise reciprocal (inverse) of an input array. It returns a new array where each element is the reciprocal of the corresponding element in the input array. The reciprocal of a number x is 1/x.

import numpy as np

a1 = np.array([1, 2, 3, 4, 5], dtype=float)
a1_reciprocal = np.reciprocal(a1)
print(a1_reciprocal)

Output:

[1.         0.5        0.33333333 0.25       0.2       ]

Note that the numpy.reciprocal() function performs integer division with integer values in an array, leading to rounded-down results. To obtain accurate reciprocals, it is recommended to use the / operator instead.

import numpy as np

a1 = np.array([1, 2, 3, 4, 5])
a1_reciprocal = 1/a1
print(a1_reciprocal)

Output:

[1.         0.5        0.33333333 0.25       0.2       ]

Arithmetic Operations with 2D arrays

import numpy as np

arr1 = np.array([[1,2,3,4,5],[6,7,8,9,10]])
arr2 = np.array([[1,2,1,2,3],[5,7,1,2,3]])

arr_add = arr1 + arr2
print(arr_add)

Output:

[[ 2  4  4  6  8]
 [11 14  9 11 13]]

When we perform addition with a two-dimension array, it will add corresponding elements and produce output.

Similarly, you can perform Subtraction & multiplication operations, it will subtract or multiply corresponding elements and give you output.

Arithmetic Functions

List of Basic arithmetic functions in numpy.

  1. numpy.min()
  2. numpy.max()
  3. numpy.argmin()
  4. numpy.argmax()
  5. numpy.sqrt()
  6. numpy.sin()
  7. numpy.cos()
  8. numpy.cumsum()

Now let’s see each of these functions in detail with the help of example.

numpy.min()

The numpy.min() function returns the minimum element or value from an array.

import numpy as np

my_arr = np.array([1,2,3,4,5,6,7,0,8,9])
print(np.min(my_arr))

Output:

0

numpy.max()

The numpy.max() function returns the maximum element or value from an array.

import numpy as np

my_arr2 = np.array([1,3,7,9,5,2,0])
print(np.max(my_arr2))

Output:

9

numpy.argmin()

The numpy.argmin() function returns the position (index) of the minimum value in the array.

import numpy as np

my_arr3 = np.array([1,3,7,9,5,2,0,4,17])
print(np.argmin(my_arr3))

Output:

6

numpy.argmax()

The numpy.argmax() function returns the position (index) of the maximum value in the array.

import numpy as np

my_arr4 = np.array([1,3,7,9,5,2,0,4])
print(np.argmax(my_arr4))

Output:

3

numpy.min() & numpy.max() with 2D array

In a 2D array, the min and max functions return the minimum and maximum values based on their axis. Here, axis = 0 represents columns, and axis = 1 represents rows.

For better understanding, let’s understand it with the help of an example.

import numpy as np

var1 = np.array([[2,3,6],[9,5,1]])
print(np.min(var1,axis=1))

Output:

[2 1]

Explanation:

Now let’s change the axis to zero and see the output.

import numpy as np

var1 = np.array([[2,3,6],[9,5,1]])
print(np.min(var1,axis=0))

Output:

[2 3 1]

Explanation:

Similarly, you can also use the max() function with an axis; it works as we discussed above. You can try the max() function yourself for practice.

numpy.sqrt()

The numpy.sqrt() function returns the square root of each element in the array.

import numpy as np

var2 = np.array([3,2,1,4,5])
print(np.sqrt(var2)) 

Output:

[1.73205081 1.41421356 1.         2.         2.23606798]

numpy.sin() & numpy.cos()

The numpy.sin() and numpy.cos() functions return the cosine and sine values in radians for each element of the array.

import numpy as np

var3 = np.array([1,2,3])
print(np.sin(var3)) 
print(np.cos(var3)) 

Output:

[0.84147098 0.90929743 0.14112001]
[ 0.54030231 -0.41614684 -0.9899925 ]

numpy.cumsum()

Let’s understand the cumsum() function with the help of an example. Suppose we have an array ([1, 2, 3, 4]). When we perform the cumsum() function on this array, it returns a new array that contains: [1, 3, 6, 10]. Now, let’s see how this output is obtained.

In general, the cumsum() function calculates the cumulative sum of elements in an array. It returns an array where each element is the sum of all the preceding elements in the input array.

It is often used in various statistical and data analysis operations to track cumulative totals or to analyze patterns in data.

import numpy as np

var4 = np.array([1,2,3,4])

cumsum_var = np.cumsum(var4)
print(cumsum_var)

Output:

[ 1  3  6 10] 

This is all about NumPy arithmetic operations. I hope this article adds some value to your life. Thank you for reading.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment