NumPy,  Python

NumPy

Python is one of the powerful language used for machine learning, Data Science, Hardware interaction problems like IoT, etc. The NumPy is one of the python libraries that provides the adding support of large multi-dimensional arrays and matrix along with the large collection of the high-level mathematical functions to operate the array and matrix. The NumPy stands for Numerical Python is created by Travis Oliphant in 2005. The following chart contains some of the array and matrix operations.

 NumPy commandsComments
1.Import numpy as pyNumPy library connection
2.x0 = np.array(12)Create a Scalar array for range(0-12)
3.x0.ndimPrint the Dimension (Rank) of the Scalar
4.X1 = np.array([12,3,6,14])Creat a Vector array
5.X1.shapeCheck the shape of array(n,m) (n=column, m=row)
6.X2.np.array([[5,78,2,34,0], [6,79,3,35,1], [7,80,4,36,2]])Creat a array Matrix
7.x3 = np.array([[[5, 78, 2, 34, 0],[6, 79, 3, 35, 1],[7, 80, 4, 36, 2]],
               [[5, 78, 2, 34, 0],[6, 79, 3, 35, 1],[7, 80, 4, 36, 2]],                [[5, 78, 2, 34, 0],[6, 79, 3, 35, 1],[7, 80, 4, 36, 2]]])
 Create a Three-dimension Tensor  This is a cube with numbers
8.I = np.eye(3)Identity Matrix
9.Z = np.zeros((4,3))Zero Matrix Array([[0,0,0], [0,0,0], [0,0,0], [0,0,0]])
10.L = np.ones((4,2))Ones Matrix Array([[1,1], [1,1], [1,1], [1,1]])
11.Y = np.array([1,4,-2]) D = np.diag(Y)Diagonal Matrix
12.R1 = np.array([1,2,3]*3) (OR) R2 = np.repeat([1,2,3],3)R1 = Creating the array using reapeating list R2 = Repeat elements of an array using repeat
13.X2 = np. array([[ 5, 78,  2, 34, 0],[ 6, 79,  3, 35,  1],              [ 7, 80,  4, 36,  2]])
X2[1][3]
Input of an element of NumPy Output of X2[1][3] is 34
    14.mylist = [1,2,3]
mylist1 = [4,5,6]
x = np.array(mylist)
y = np.array(mtlist1)
print(x,’\n’,y)  
    Matrix and Vector operation Create Vactors (mylist = [1,2,3]) Output of print statement is [1 2 3] [4 5 6]  
15.x = np.array([1,2,3])
y = np.array([4,5,6])
print(x+y)
#(OR)
Print(np.add(x,y))
Elementwise Addition [1 2 3] + [4 5 6] = [5 7 9] In NumPy instead of print(x+y) we can use print(np.add(x,y))
16.x = np.array([1,2,3])
y = np.array([4,5,6])
print(x-y)
#(OR)
Print(np.subtract(x,y))
Elementwise Subtraction [1 2 3] – [4 5 6] = [-3 -3 -3] In NumPy instead of print(x-y) we can use print(np.subtraction(x,y))
17.x = np.array([1,2,3])
y = np.array([4,5,6])
print(x*y)
#(OR)
Print(np.multiply(x,y))
Elementwise Multiplication [1 2 3] * [4 5 6] = [4 10 18] In NumPy instead of print(x*y) we can use print(np.multiply(x,y))
18.x = np.array([1,2,3])
y = np.array([4,5,6])
print(x/y)
#(OR)
Print(np.divide(x,y))
Elementwise Division [1 2 3] / [4 5 6] = [0.25 0.4 0.5] In NumPy instead of print(x/y) we can use print(np.divide(x,y))
19.x = np.array([1,2,3])
print(x**2)
#(OR)
print(np.power(x,2))
Elementwise Power [1 2 3] ^2 = [1 4 9] In NumPy instead of print(x**2) we can use print(np.power(x,2))
20.x = np.array([1,2,3])
a = 2 print(a*x)
Scaler * Vector Multiplication The output is [2 4 6]
21.g = np.array([[1,2],[3,4]])
print(np.sum(g))
Various Sums of elements Compute sum of all elements; print “10”
22.print(np.sum(g, axis=0))Compute sum of each column; print ”[4 6]”
23.print(np.sum(g, axis=1))Compute sum of each row: print ”[3 7]”
24.x = np.array([1,2,3])
y = np.array([4,5,6])
print(np.dot(x,y))
Dot Product (Not codrdinatewise multiplication) Vector Multiplication (scaler product) Output of print statement is ( 1*4 + 2*5 + 3*6 ) = 32  
25.x = np.array([1,2,3])
y = np.array([[4,1],[2,2],[1,3]])
z = np.dot(x,y)
print(z)
The output print(z) is [11 14]
26.y = np.array([[4,1],[2,2],[1,3]])
y[:,:]
Print entire array Array([[4,1], [2,2], [1,3]])
27.y[:2,:]Partial output Print first 2 row and all columns Array([[4,1], [2,2]])
28.y>2Intresting Decisions Print (True) If the elements of array(y) is greater the 2 else (False) array([[ True, False],
[False, False], [False,  True]])
29.y[y>=2]Just return elements which >=2 array([4, 2, 2, 3])
30.y.flatten()Flatten the array Array([4, 1, 2, 2, 1, 3])

Leave a Reply

Your email address will not be published. Required fields are marked *