Numpy Array Math
In this session we are going to learn some basic math properties & we will write code & solve the problems.
(1) .sum :
code :
code :
import numpy as np
a=np.array([5,10])
print(np.sum(a))
output :
>> 15
a=np.array([5,10])
print(np.sum(a))
output :
>> 15
As you all can relate what this .sum will sum the numbers which are present in the array.
(2) .subtract :
code :
a=[10,20]
b=[6,8]
import numpy as np
print(np.subtract(a,b))
output : >> [4 12]
b=[6,8]
import numpy as np
print(np.subtract(a,b))
output : >> [4 12]
.subtraction help's us to subtract the number which are present in 2 list/array with each other i.e 10 - 6 = 4 & 20 - 8 = 12.
(3) axis :
code :
a=[10,20] # [x1 , x2]
b=[6,8] # [y1 , y2]
import numpy as np
print(np.sum([a,b],axis=0)) #[x1+y1 , x2+y2]
print(np.sum([a,b],axis=1)) #[x1+x2 , y1+y2]
output :
>> [16 28]
[ 30 14]
b=[6,8] # [y1 , y2]
import numpy as np
print(np.sum([a,b],axis=0)) #[x1+y1 , x2+y2]
print(np.sum([a,b],axis=1)) #[x1+x2 , y1+y2]
output :
>> [16 28]
[ 30 14]
In this axis=1 is row & axis=0 is column.The concept of axis will only work for .sum function.
(4) .divide :
code :
a=[10,7]
b=[5,2]
import numpy as np
print(np.divide(a,b))
output :
>> [2. 3.5]
b=[5,2]
import numpy as np
print(np.divide(a,b))
output :
>> [2. 3.5]
Remember that division will always give you float value as an output.
(5) .multiply :
code:
a=[10,7]
b=[5,2]
import numpy as np
print(np.multiply(a,b))
output :
>> [50 14]
b=[5,2]
import numpy as np
print(np.multiply(a,b))
output :
>> [50 14]
This will give you output as an multiplication number.
(6) .equal :
code :
a=[1,2,3]
b=[4,2,1]
import numpy as np
print(np.equal(a,b))
output :
>> [False True False]
b=[4,2,1]
import numpy as np
print(np.equal(a,b))
output :
>> [False True False]
.equal will compare both array's & if the value will match then it will give you True & if the value will not match then it will give you False.
(7) .array_equal :
code :
a=[1,2,3]
b=[4,2,1]
import numpy as np
print(np.array_equal(a,b))
output :
>> False
b=[4,2,1]
import numpy as np
print(np.array_equal(a,b))
output :
>> False
.array_equal will compare both the array & if all the elements of 1st array matches with the all the element of 2nd array then it will give you True as a Boolean value else it will give you False.
(8) Basic math functions :
a=[1,2,3]
print(np.sqrt(a)) # square root of the elements in the list
print(np.abs(a)) # calculate the absolute value element-wise
print(np.sum(a)) # sum of the elements in the list
print(np.min(a)) # minimum element in the list
print(np.max(a)) # maximum element in the list
print(np.mean(a)) # maximum element in the list
print(np.median(a)) # median of element in the list
print(np.std(a)) # standard deviation element in the list; its formula is SD=sqrt(mean(abs(x-x.mean())^2)
print(np.corrcoef(a)) # calculate the coefficient of the list
output :
>> [1. 1.41421356 1.73205081]
[1 2 3]
(8) Basic math functions :
a=[1,2,3]
print(np.sqrt(a)) # square root of the elements in the list
print(np.abs(a)) # calculate the absolute value element-wise
print(np.sum(a)) # sum of the elements in the list
print(np.min(a)) # minimum element in the list
print(np.max(a)) # maximum element in the list
print(np.mean(a)) # maximum element in the list
print(np.median(a)) # median of element in the list
print(np.std(a)) # standard deviation element in the list; its formula is SD=sqrt(mean(abs(x-x.mean())^2)
print(np.corrcoef(a)) # calculate the coefficient of the list
output :
>> [1. 1.41421356 1.73205081]
[1 2 3]
6
1
3
2.0
2.0
0.816496580927726
1.0
No comments:
No Spamming and No Offensive Language