import numpy as np #importing library
Elementwise Operations
Basic Operations
with scalars
a = np.array([1, 2, 3, 4]) #create an array
a + 1 # add 1 to array elements
o/p: array([2, 3, 4, 5])
a ** 2 # ** means making square of array list
o/p: array([ 1, 4, 9, 16])
All arithmetic operates elementwise
b = np.ones(4) + 1 #ones(4) means (1,1,1,1) will be created + 1=(2,2,2,2)
a–b
(1,2,3,4)-(2,2,2,2)
o/p: array([-1., 0., 1., 2.])
a*b
array([ 2., 4., 6., 8.])
# Matrix multiplication
c = np.diag([1, 2, 3, 4])
print(c * c)
print("*****************")
print(c.dot(c))
[[ 1 0 0 0]
[ 0 4 0 0]
[ 0 0 9 0]
[ 0 0 0 16]]
*****************
[[ 1 0 0 0]
[ 0 4 0 0]
[ 0 0 9 0]
[ 0 0 0 16]]
Comparisions
a = np.array([1, 2, 3, 4])
b = np.array([5, 2, 2, 4])
a == b
array([False, True, False, True], dtype=bool)
a > b
array([False, False, True, False], dtype=bool)
#array-wise comparisions
a = np.array([1, 2, 3, 4])
b = np.array([5, 2, 2, 4])
c = np.array([1, 2, 3, 4])
np.array_equal(a, b)
False
np.array_equal(a, c)
True
Logical Operations
a = np.array([1, 1, 0, 0], dtype=bool)
b = np.array([1, 0, 1, 0], dtype=bool)
np.logical_or(a, b)
array([ True, True, True, False], dtype=bool)
np.logical_and(a, b)
array([ True, False, False, False], dtype=bool)
Transcendental functions:
a = np.arange(5)
np.sin(a)
array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
Shape Mismatch
a = np.arange(4) # [0,1,2,3]
a + np.array([1, 2]) # adding 1x2 to 1x4 is not possible
error was occur as could not be broadcast together with shapes (4,) (2,)
Basic Reductions
computing sums
x = np.array([1, 2, 3, 4])
np.sum(x)
o/p: 10
sum by rows and by columns
x = np.array([[1, 1], [2, 2]])
x
o/p: array([[1, 1], Commented [va1]:
Axis=0
[2, 2]]) Commented [va2R1]:
x.sum(axis=0) #columns first dimension [1,1]
[2,2]
o/p: array([3, 3])
x.sum(axis=1) #rows (second dimension) [1,1]=2
Axis=1 [2,2] =4
o/p: array([2, 4])
Other reductions
> x = np.array([1, 3, 2])
> x.min()
o/p: 1
x.max()
o/p: 3
x.argmin() # index of minimum element
o/p: 0
x.argmax() # index of maximum element
o/p: 1
Logical Operations
np.all([True, True, False]) # np.all is like ‘and’ operation
o/p: False
np.any([True, False, False]) # np.any is like ‘or’ operation
o/p: True
#Note: any can be used for array comparisions
a = np.zeros((50, 50))
np.any(a != 0)
o/p: False
np.all(a == a)
o/p: True
a = np.array([1, 2, 3, 2])
b = np.array([2, 2, 3, 2])
c = np.array([6, 4, 4, 5])
((a <= b) & (b <= c)).all()
o/p: True
Broadcasting
a = np.tile(np.arange(0, 40, 10), (3,1))
print(a)
print("*************")
a=a.T
print(a)
o/p: [[ 0 10 20 30]
[ 0 10 20 30]
[ 0 10 20 30]]
*************
[[ 0 0 0]
[10 10 10]
[20 20 20]
[30 30 30]]
b = np.array([0, 1, 2])
b
o/p: array([0, 1, 2])
a=[[ 0 0 0]
[10 10 10]
[20 20 20]
[30 30 30]]
b= ([ 0, 1, 2])
a+b
o/p: array ([[ 0, 1, 2],
[10, 11, 12],
[20, 21, 22],
[30, 31, 32]])
a = np.arange(0, 40, 10)
a.shape
o/p: (4,) # it’s a 1D matrix
to convert into 2D matrix
a = a[:, np.newaxis] # adds a new axis -> 2D array
a.shape
o/p: (4, 1)
a
o/p: array([[ 0],
[10],
[20],
[30]])
> a + b
o/p: array([[ 0, 1, 2],
[10, 11, 12],
[20, 21, 22],
[30, 31, 32]])
a[0, 2, 1]
o/p: 5