Workshop Notes-2 Handling Array with NumPy
Workshop Notes-2 Handling Array with NumPy
1 Introduction
• Python comes with a data structure, such as List, for array operations
• A Python list on its own is not suitable to perform heavy mathematical operations.
• NumPy is a wonderful Python package produced by Travis Oliphant, which has been created
fundamentally for scientific computing.
• It helps handle large multidimensional arrays and matrices, along with a large library of
high-level mathematical functions to operate on these arrays.
• A NumPy array would require much less memory to store the same amount of data compared
to a Python list, which helps in reading and writing from the array in a faster manner.
2 Creating an array
[1]: import numpy as np
list1 = [1, 2, 3, 4, 5, 6]
a1 = np.array(list1)
[2]: print(a1)
[1 2 3 4 5 6]
[3]: print(type(a1))
<class 'numpy.ndarray'>
[4]: print(a1.ndim)
[5]: print(a1.dtype)
int32
[6]: print(a1.shape)
(6,)
[7]: print(sum(a1))
1
21
[8]: print(a1.sum())
21
[9]: a1.mean()
[9]: 3.5
[10]: a1.min()
[10]: 1
[11]: a1.max()
[11]: 6
[12]: np.std(a1)
[12]: 1.707825127659933
[13]: print(a1==2)
[14]: print(sum(a1==2))
[15]: print(a1**2)
[ 1 4 9 16 25 36]
[16]: print(a1.prod())
720
[17]: print(np.prod(a1))
720
[18]: print(np.cos(a1))
[11 11 11 11 11 11]
2
[20]: print(a1-a2)
[-9 -7 -5 -3 -1 1]
[21]: print(a1*a2)
[10 18 24 28 30 30]
[22]: print(sum(a1*a2))
140
[23]: print(np.sum(a1*a2))
140
[24]: a3=np.repeat(1,10)
print(a3)
[1 1 1 1 1 1 1 1 1 1]
[25]: a4=np.repeat([1,2,3],[3,2,1])
print(a4)
[1 1 1 2 2 3]
[23]: print(a2)
[['a' 'b']
['c' 'd']]
[24]: print(a2.ndim)
[26]: print(a2.shape)
(2, 2)
3
[43]: np.random.seed(4)
r1 = np.random.randint(10)
print(r1)
79
[11 97 82 60 19]
<class 'numpy.ndarray'>
[[68 65 65 67]
[46 60 54 48]
[62 13 10 65]
[31 31 83 48]
[66 76 56 40]]
[29]: np.random.seed(2)
r1 = np.random.randint(10,100,(2,3,2)) ## 5 random numbers b/w 10 to 99
print(r1)
[[[50 25]
[82 32]
[53 92]]
[[85 17]
[44 59]
[85 95]]]
[4 7 2 7 3 4 5 4 4 1]
[2 1 1 1 0 2 1 1 1 0]
4
[50]: np.random.geometric(0.5,[4,4])
[14 13 13 8 14 22 16 9 11 14]
[54]: np.random.exponential(0.5,[4,4])
5
print(s)
[57]: np.random.chisquare(1,[4,3])
[[10 5 16 1]
[18 17 18 9]
[10 1 11 9]
[ 5 17 5 16]
[12 12 2 9]]
[60]: print(len(r1))
[61]: print(r1[2])
[10 1 11 9]
[62]: print(r1[-2])
[ 5 17 5 16]
[63]: print(r1[0,3])
[64]: print(r1[0][3])
6
[65]: print(r1[:])
[[10 5 16 1]
[18 17 18 9]
[10 1 11 9]
[ 5 17 5 16]
[12 12 2 9]]
[66]: print(r1[1:4,0:3])
[[18 17 18]
[10 1 11]
[ 5 17 5]]
[[ 5 1]
[ 1 9]
[12 9]]
[68]: print(np.max(r1))
print(np.max(r1,axis=0)) ## column wise
print(np.max(r1,axis=1)) ## row wise
18
[18 17 18 16]
[16 18 11 17 12]
[69]: print(np.min(r1))
print(np.min(r1,axis=0)) ## column wise
print(np.min(r1,axis=1)) ## row wise
1
[5 1 2 1]
[1 9 1 5 2]
4
[1 1 1 3]
[2 0 2 1 0]
3
[3 2 4 0]
[3 3 1 0 2]
7
[72]: print(np.sum(r1))
print(np.sum(r1,axis=0)) ## column wise
print(np.sum(r1,axis=1)) ## row wise
203
[55 52 52 44]
[32 62 31 43 35]
[73]: print(np.mean(r1))
print(np.mean(r1,axis=0)) ## column wise
print(np.mean(r1,axis=1)) ## row wise
10.15
[11. 10.4 10.4 8.8]
[ 8. 15.5 7.75 10.75 8.75]
6 Shape Manupulation
[74]: print(r1)
r2=r1.reshape(20)
print(r2)
[[10 5 16 1]
[18 17 18 9]
[10 1 11 9]
[ 5 17 5 16]
[12 12 2 9]]
[10 5 16 1 18 17 18 9 10 1 11 9 5 17 5 16 12 12 2 9]
[[10 5 16 1 18]
[17 18 9 10 1]
[11 9 5 17 5]
[16 12 12 2 9]]
[76]: r3 = r1.reshape((2,2,5))
print(r3)
[[[10 5 16 1 18]
[17 18 9 10 1]]
[[11 9 5 17 5]
[16 12 12 2 9]]]
[77]: r4 = r1.astype('str')
print(r4)
8
[['10' '5' '16' '1' '18']
['17' '18' '9' '10' '1']
['11' '9' '5' '17' '5']
['16' '12' '12' '2' '9']]
[78]: r5 = r1.astype('float')
print(r5)
7 Sequence
[31]: s1 = np.arange(1, 9, 1)
print("\n A sequential array with a step 1: \n",s1)
[33]: s3=range(1,10,1)
print(type(s3))
s4=np.array(s3)
print(s4)
<class 'range'>
[1 2 3 4 5 6 7 8 9]
[83]: x=np.r_[1:50:3]
print(x)
print(type(x))
9
[ 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49]
<class 'numpy.ndarray'>
[84]: print(x==10)
print(sum(x==10))
[False False False True False False False False False False False False
False False False False False]
1
[85]: print(np.r_[2,4,1:30:3])
[ 2 4 1 4 7 10 13 16 19 22 25 28]
[86]: print(np.c_[1:20:3])
[[ 1]
[ 4]
[ 7]
[10]
[13]
[16]
[19]]
8 Matrix Operations
[88]: A1 = np.array([[1, 1],
[0, 1]])
print(A1)
[[1 1]
[0 1]]
[[2 0]
[3 4]]
Product of two matrices
[90]: A1 * A2
10
[90]: array([[2, 0],
[0, 4]])
The above command will perform the element-by-element multiplication. Matrix product can be
performed by the following code:
power of a matrix
[92]: np.linalg.matrix_power(A1, 2)
Transpose of a Matrix
[93]: print(A1.transpose())
[[1 0]
[1 1]]
[94]: np.transpose(A1)
Diagonal of matrix
[95]: print(np.diag(A1))
print(np.diagonal(A1)) # Alternate
[1 1]
[1 1]
[96]: x=[1,2,3]
A3=np.diag(x)
print(A3)
[[1 0 0]
[0 2 0]
[0 0 3]]
Trace of a matrix
[97]: print(np.trace(A1))
11
Determinant of a matrix
[98]: print(np.linalg.det(A1))
1.0
Inverse of a matrix
[99]: print(np.linalg.inv(A1))
[[ 1. -1.]
[ 0. 1.]]
Zero matrix
[100]: zeros=np.zeros((5,4))
print(zeros)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Matrix with 1
[101]: ones=np.ones((5,6))
print(ones)
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]
[102]: ones_int=np.ones((5,6),np.int64)
print(ones_int)
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
Identity matrix
[103]: identity=np.identity(5)
print(identity)
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
12
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
[104]: identity=np.identity(5,np.int32)
print(identity)
[[1 0 0 0 0]
[0 1 0 0 0]
[0 0 1 0 0]
[0 0 0 1 0]
[0 0 0 0 1]]
Eigenvalues and Eigenvectors
[105]: np.linalg.eig(A1)
[9]: A=np.array([[2,3,4],
[3,1,2],
[1,2,1]])
print(A)
b=np.array([13,8,5])
[[2 3 4]
[3 1 2]
[1 2 1]]
[6]: np.linalg.det(A)
[6]: 11.000000000000002
[8]: A1=np.linalg.inv(A)
print(A1)
[11]: sol=np.dot(A1,b)
print(sol)
[1. 1. 2.]
13