Categories: R Programming

Arithmetic operations of Numpy arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

Arithmetic operations of Numpy ndarrays are element-wise. If an array has addition, subtraction, multiplication or division with a scalar, each element will have the same operation and the result is also an array.

#import numpy as np
#create an array of 0 to 9
b = np.arange(10)
b
#output
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#addition of an array with a scalar
b + 4
#Output
array([ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
#subtraction
b-2
#Output 
array([-2, -1,  0,  1,  2,  3,  4,  5,  6,  7])
#multiplication
b * 1.5
#Output
array([ 0. ,  1.5,  3. ,  4.5,  6. ,  7.5,  9. , 10.5, 12. , 13.5])
#division
b / 4
#Output
array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  , 2.25])

Two Numpy arrays of same dimensions can perform similar arithmetic operations as with a scalar. The resulting array has the same dimension as the input, and each element of the resulting array is the result of arithmetic operation between the inputitng arrays.

#create another array of same dimension as b
c = np.arange(4, 14)
c
#Output 
array([ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
#addition of two arrays
b + c 
#Output
array([ 4,  6,  8, 10, 12, 14, 16, 18, 20, 22])
#division between two arrays
b / c
#Output
array([0.        , 0.2       , 0.33333333, 0.42857143, 0.5       ,
       0.55555556, 0.6       , 0.63636364, 0.66666667, 0.69230769])

Numpy array can also be applied with particular mathematic functions, like trigonometric and exponential functions, which are also element-wise.

#sine of array
b * np.sin(c)
#Output
array([-0.        , -0.95892427, -0.558831  ,  1.9709598 ,  3.95743299,
        2.06059243, -3.26412667, -6.99993145, -4.29258334,  3.78150333])
#square root of array
b * np.sqrt(c)
#Output
array([ 0.        ,  2.23606798,  4.89897949,  7.93725393, 11.3137085 ,
       15.        , 18.97366596, 23.21637353, 27.71281292, 32.44996148])
#exponential of array
b * np.exp(c)
#Output 
array([0.00000000e+00, 1.48413159e+02, 8.06857587e+02, 3.28989948e+03,
       1.19238319e+04, 4.05154196e+04, 1.32158795e+05, 4.19118992e+05,
       1.30203833e+06, 3.98172053e+06])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago