NumPy Arithmetic Operations (original) (raw)

Last Updated : 10 Oct, 2023

**NumPy is an open-source Python library for performing array computing (matrix operations). It is a wrapper around the library implemented in C and used for performing several trigonometric, algebraic, and statistical operations. NumPy objects can be easily converted to other types of objects like the Pandas data frame and the tensorflow tensor. Python list can be used for array computing, but it is much slower than NumPy. NumPy achieves its fast implementation using vectorization. One of the important features of NumPy arrays is that a developer can perform the same mathematical operation on every element with a single command.

Let us understand arithmetic operations using NumPy.

**Addition

Python3

import numpy as np

a = np.array([ 5 , 72 , 13 , 100 ])

b = np.array([ 2 , 5 , 10 , 30 ])

add_ans = a + b

print (add_ans)

add_ans = np.add(a, b)

print (add_ans)

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

add_ans = a + b + c

print (add_ans)

add_ans = np.add(a, b, c)

print (add_ans)

**Output

[ 7 77 23 130]
[ 7 77 23 130]
[ 8 79 26 134]
[ 7 77 23 130]

As we can see that the matrixes are of the same shape, if they are different than, Numpy will try broadcasting if it is possible. The reader can see that the same operation (addition) can be done using arithmetic operation (+) as well as numpy function (np.add).

**Subtraction

Python3

import numpy as np

a = np.array([ 5 , 72 , 13 , 100 ])

b = np.array([ 2 , 5 , 10 , 30 ])

sub_ans = a - b

print (sub_ans)

sub_ans = np.subtract(a, b)

print (sub_ans)

**Output

[ 3 67 3 70]
[ 3 67 3 70]

The user can also perform broadcasting with a matrix and a constant

Python3

import numpy as np

a = np.array([ 5 , 72 , 13 , 100 ])

b = np.array([ 2 , 5 , 10 , 30 ])

sub_ans = a - b - 1

print (sub_ans)

sub_ans = np.subtract(a, b, 1 )

print (sub_ans)

**Output

[ 2 66 2 69]
[ 2 66 2 69]

**Multiplication

Python3

import numpy as np

a = np.array([ 5 , 72 , 13 , 100 ])

b = np.array([ 2 , 5 , 10 , 30 ])

mul_ans = a * b

print (mul_ans)

mul_ans = np.multiply(a, b)

print (mul_ans)

**Output

[ 10 360 130 3000]
[ 10 360 130 3000]

**Division

Python3

import numpy as np

a = np.array([ 5 , 72 , 13 , 100 ])

b = np.array([ 2 , 5 , 10 , 30 ])

div_ans = a / b

print (div_ans)

div_ans = np.divide(a, b)

print (div_ans)

**Output

[ 2.5 14.4 1.3 3.33333333]
[ 2.5 14.4 1.3 3.33333333]

There is a myriad number of other functions which in NumPy let us see some of them one by one.

mod() and power() function

**Example

Python3

mod_ans = np.mod(a, b)

print (mod_ans)

rem_ans = np.remainder(a,b)

print (rem_ans)

pow_ans = np.power(a, b)

print (pow_ans)

**Output

[ 1 2 3 10]
[ 1 2 3 10]
[ 25 1934917632 137858491849
1152921504606846976]

Some aggregation and statistical functions

**Example

Python3

mean_a = np.mean(a)

print (mean_a)

mean_b = np.average(b)

print (mean_b)

sum_a = np. sum (a)

print (sum_a)

var_b = np.var(b)

print (var_b)

**Output

47.5
11.75
190
119.1875