untitled
August 8, 2024
1 Importing the Numpy Library
[2]: import numpy as np
2 Vectors
2.0.1 Creating four vectors
[3]: vector1 = np.array([1,2,3,4,5])
vector2 = np.array([6,7,8,9,10])
vector3 = np.array([0,3,0.3,5])
vector4 = np.array([0.5,2,4,3])
2.0.2 Getting the dimensions for each vector
[82]: print(f"dimensions of vector1 = { vector1.ndim}")
print(f"dimensions of vector2 = {vector2.ndim}")
print(f"dimensions of vector3 = {vector3.ndim}")
print(f"dimensions of vector4 = {vector4.ndim}")
dimensions of vector1 = 1
dimensions of vector2 = 1
dimensions of vector3 = 1
dimensions of vector4 = 1
2.0.3 Getting the shape for each vector
[83]: print(f"shape of vector1 = {vector1.shape}")
print(f"shape of vector2 = {vector2.shape}")
print(f"shape of vector3 = {vector3.shape}")
print(f"shape of vector4 = {vector4.shape}")
shape of vector1 = (5,)
shape of vector2 = (5,)
shape of vector3 = (4,)
shape of vector4 = (4,)
1
2.0.4 Number of elements for each vector, the memory size it occupies, and other
operations
[84]: # the type
print(f"data type of vector1 = {vector1.dtype}")
print(f"data type of vector2 = {vector2.dtype}")
print(f"data type of vector3 = {vector3.dtype}")
print(f"data type of vector4 = {vector4.dtype}")
# the number of elements in each vector
print(f"the number of elements in vector1 = {vector1.size}")
print(f"the number of elements in vector2 = {vector2.size}")
print(f"the number of elements in vector3 = {vector3.size}")
print(f"the number of elements in vector4 = {vector4.size}")
# the size in bytes for each element in the vector/ array
print(f"the size in bytes for each element in vector1 = {vector1.itemsize}")
print(f"the size in bytes for each element in vector2 = {vector2.itemsize}")
print(f"the size in bytes for each element in vector3 = {vector3.itemsize}") #␣
↪floats are bigger than integers
print(f"the size in bytes for each element in vector4 = {vector4.itemsize}")
# the size it invades in the memory
print(f"the size vector1 invades in the memory = {vector1.nbytes}") #␣
↪print(vector1.size * vector1.itemsize )
print(f"the size vector2 invades in the memory = {vector2.nbytes}")
print(f"the size vector3 invades in the memory = {vector3.nbytes}")
print(f"the size vector4 invades in the memory = {vector4.nbytes}")
data type of vector1 = int32
data type of vector2 = int32
data type of vector3 = float64
data type of vector4 = float64
the number of elements in vector1 = 5
the number of elements in vector2 = 5
the number of elements in vector3 = 4
the number of elements in vector4 = 4
the size in bytes for each element in vector1 = 4
the size in bytes for each element in vector2 = 4
the size in bytes for each element in vector3 = 8
the size in bytes for each element in vector4 = 8
the size vector1 invades in the memory = 20
the size vector2 invades in the memory = 20
the size vector3 invades in the memory = 32
the size vector4 invades in the memory = 32
2
2.0.5 Addition, Subtraction, Multiplication & Division
[85]: # adding vectors
print(f"vector1 + vector2 = {vector1 + vector2}")
print(f"vector3 + vector4 = {vector3 + vector4}")
# print(vector3 + vector1) can not perform this operation because these two␣
↪vectors has different shapes
# Substracting vectors
print(f"vector1 - vector2 = {vector1 - vector2}")
print(f"vector3 - vector4 = {vector3 - vector4}")
# Multiplicating vectors
print(f"vector1 * vector2 = {vector1 * vector2}")
print(f"vector3 * vector4 = {vector3 * vector4}")
# print(vector3 * vector1) can not perform this operation because these two␣
↪vectors has different shapes
# the dot product of vectors
print(f"the dot product of vector1 and vector2 = {np.dot(vector1,vector2)}")
print(f"the dot product of vector4 and vector3 = {np.dot(vector4,vector3)}")
# dividing vectors
print(f"vector1 / vector2 = {vector1 / vector2}")
print(f"vector3 / vector4 = {vector3 / vector4}")
# print(vector3 / vector1) can not perform this operation because these two␣
↪vectors has different shapes
vector1 + vector2 = [ 7 9 11 13 15]
vector3 + vector4 = [0.5 5. 4.3 8. ]
vector1 - vector2 = [-5 -5 -5 -5 -5]
vector3 - vector4 = [-0.5 1. -3.7 2. ]
vector1 * vector2 = [ 6 14 24 36 50]
vector3 * vector4 = [ 0. 6. 1.2 15. ]
the dot product of vector1 and vector2 = 130
the dot product of vector4 and vector3 = 22.2
vector1 / vector2 = [0.16666667 0.28571429 0.375 0.44444444 0.5 ]
vector3 / vector4 = [0. 1.5 0.075 1.66666667]
3 Matrices
3.0.1 Creating three matrices
[46]: matrix1 = np.array([1,2,3]) # 1D matrix
matrix2 = np.array([[1,2],[4,5]]) # 2D matrix 2x2
matrix3 = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],[[10, 11, 12], [13, 14,␣
↪15], [16, 17, 18]],[[19, 20, 21], [22, 23, 24], [25, 26, 27]]]) # 3D matrix␣
↪3x3
3
3.0.2 Getting the shape for each matrix
[86]: print(f"shape of matrix1 = {matrix1.shape}")
print(f"shape of matrix2 = {matrix2.shape}")
print(f"shape of matrix3 = {matrix3.shape}")
shape of matrix1 = (3,)
shape of matrix2 = (2, 2)
shape of matrix3 = (3, 3, 3)
3.0.3 Getting the dimensions for each matrix
[87]: print(f"dimension of matrix1 = {matrix1.ndim}")
print(f"dimension of matrix2 = {matrix2.ndim}")
print(f"dimension of matrix3 = {matrix3.ndim}")
dimension of matrix1 = 1
dimension of matrix2 = 2
dimension of matrix3 = 3
3.0.4 Number of elements for each matrix, the memory size it occupies, and other
operations
[88]: # the type
print(f"data type of matrix1 = {matrix1.dtype}")
print(f"data type of matrix2 = {matrix2.dtype}")
print(f"data type of matrix3 = {matrix3.dtype}")
# the number of elements in each matrix
print(f"the number of elements in matrix1 = {matrix1.size}")
print(f"the number of elements in matrix2 = {matrix2.size}")
print(f"the number of elements in matrix3 = {matrix3.size}")
# the size in bytes for each element in the matrix/ array
print(f"the size in bytes for each element in matrix1 = {matrix1.itemsize}")
print(f"the size in bytes for each element in matrix2 = {matrix2.itemsize}")
print(f"the size in bytes for each element in matrix3 = {matrix3.itemsize}")
# the size it invades in the memory
print(f"the size matrix1 invades in the memory = {matrix1.nbytes}") #␣
↪print(matrix1.size * matrix1.itemsize )
print(f"the size matrix2 invades in the memory = {matrix2.nbytes}")
print(f"the size matrix3 invades in the memory = {matrix3.nbytes}")
data type of matrix1 = int32
data type of matrix2 = int32
data type of matrix3 = int32
the number of elements in matrix1 = 3
4
the number of elements in matrix2 = 4
the number of elements in matrix3 = 27
the size in bytes for each element in matrix1 = 4
the size in bytes for each element in matrix2 = 4
the size in bytes for each element in matrix3 = 4
the size matrix1 invades in the memory = 12
the size matrix2 invades in the memory = 16
the size matrix3 invades in the memory = 108
3.0.5 Addition, Subtraction, Multiplication & Division
[93]: # adding vectors
print(f"matrix1 + matrix1 = {matrix1 + matrix1}")
print(f"matrix1 + matrix3 = {matrix1 + matrix3}") # this is possible because␣
↪the number of rows in matrix1 = the number of columns in martix3
# print(matrix1 + matrix2) can not perform this operation because shapes (3,)␣
↪and (2,2) not aligned: 3 (dim 0) != 2 (dim 0)
# Substracting vectors
print(f"matrix1 - matrix1 = {matrix1 - matrix1}")
print(f"matrix1 - matrix3 = {matrix1 - matrix3}") # this is possible because␣
↪the number of rows in matrix1 = the number of columns in martix3
# Multiplicating vectors
print(f"matrix1 * matrix3 = {np.matmul(matrix1,matrix3)}")
print(f"matrix1 * matrix1 = {matrix1 * matrix1}")
print(f"matrix1 * matrix3 = {matrix1 * matrix3}")
# print(matrix1 * matrix2) can not perform this operation because these two␣
↪vectors has different shapes
# the dot product of vectors
# print(np.dot(matrix1,matrix2)) can not perform this operation because shapes␣
↪(3,) and (2,2) not aligned: 3 (dim 0) != 2 (dim 0)
print(f"the dot product of matrix3 and matrix1 = {np.dot(matrix3,matrix1)}")
# dividing vectors
print(f"matrix1 / matrix1 = {matrix1 / matrix1}")
print(f"matrix1 / matrix3 = {matrix1 / matrix3}")
# print(matrix1 / matrix2) can not perform this operation because shapes (3,)␣
↪and (2,2) not aligned: 3 (dim 0) != 2 (dim 0)
matrix1 + matrix1 = [2 4 6]
matrix1 + matrix3 = [[[ 2 4 6]
[ 5 7 9]
[ 8 10 12]]
[[11 13 15]
[14 16 18]
[17 19 21]]
5
[[20 22 24]
[23 25 27]
[26 28 30]]]
matrix1 - matrix1 = [0 0 0]
matrix1 - matrix3 = [[[ 0 0 0]
[ -3 -3 -3]
[ -6 -6 -6]]
[[ -9 -9 -9]
[-12 -12 -12]
[-15 -15 -15]]
[[-18 -18 -18]
[-21 -21 -21]
[-24 -24 -24]]]
matrix1 * matrix3 = [[ 30 36 42]
[ 84 90 96]
[138 144 150]]
matrix1 * matrix1 = [1 4 9]
matrix1 * matrix3 = [[[ 1 4 9]
[ 4 10 18]
[ 7 16 27]]
[[10 22 36]
[13 28 45]
[16 34 54]]
[[19 40 63]
[22 46 72]
[25 52 81]]]
the dot product of matrix3 and matrix1 = [[ 14 32 50]
[ 68 86 104]
[122 140 158]]
matrix1 / matrix1 = [1. 1. 1.]
matrix1 / matrix3 = [[[1. 1. 1. ]
[0.25 0.4 0.5 ]
[0.14285714 0.25 0.33333333]]
[[0.1 0.18181818 0.25 ]
[0.07692308 0.14285714 0.2 ]
[0.0625 0.11764706 0.16666667]]
[[0.05263158 0.1 0.14285714]
[0.04545455 0.08695652 0.125 ]
[0.04 0.07692308 0.11111111]]]
6
4 Other operations that can be performed with NumPy
[92]: # all zeros matrix
print(f"all zeros matrix = {np.zeros((2,2,3))}")
# all ones matrix
print(f"all ones matrix = {np.ones((2,3,3))}")
# any other number
print(f"any other number matrix = {np.full((2,2),99,dtype='int32')}")
# any other number (full_like)
print(f"any other number matrix (full_like) = {np.full_like(vector1,3)}") # np.
↪full(vector1.shape,3)
# random decimal nums
print(f"random decimal nums matrix = {np.random.rand(4,2)}")
print(f"random decimal nums matrix = {np.random.random_sample(vector1.shape)}")
# random integers
print(f"random integers matrix = {np.random.randint(-4,7, size = (3,3))}")
# identity matrix
print(f"identity matrix = {np.identity(3)}")
#find the determinant
print(f"the determinant of a matrix = {np.linalg.det(matrix2)}")
# statistics
print(f"min number in the second row of an array of numbers = {np.min(matrix3,␣
↪axis = 1)}")# axis =1 gives all the min values of the first row and the␣
↪second row
print(f"max number in an array of numbers = {np.max(matrix1)}")
print(f"the sum of an array = {np.sum(matrix1 , axis = 0)}")
print(f"the median of an array = {np.median(matrix2)}")
print(f"the mean of the second row of an array = {np.mean(matrix3, axis = 1)}")
# vertically stacking vectors
v1 = np.array([1,2,3,4])
v2 = np.array([5,6,7,8])
print(f"vertically stacking vectors : {np.vstack([v1,v2,v1,v2])}")
# horizontally stacking vectors
h1 = np.zeros((2,3))
h2 = np.ones((2,2))
print(f"horizontally stacking vectors : {np.hstack((h1,h2))}")
7
all zeros matrix = [[[0. 0. 0.]
[0. 0. 0.]]
[[0. 0. 0.]
[0. 0. 0.]]]
all ones matrix = [[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]]
any other number matrix = [[99 99]
[99 99]]
any other number matrix (full_like) = [3 3 3 3 3]
random decimal nums matrix = [[0.25901925 0.50742835]
[0.0767605 0.48565797]
[0.08118336 0.33748505]
[0.6238094 0.3765584 ]]
random decimal nums matrix = [0.04116704 0.01918068 0.42065864 0.14309036
0.19580194]
random integers matrix = [[-3 3 6]
[ 5 1 3]
[ 4 4 1]]
identity matrix = [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
the determinant of a matrix = -2.9999999999999996
min number in the second row of an array of numbers = [[ 1 2 3]
[10 11 12]
[19 20 21]]
max number in an array of numbers = 3
the sum of an array = 6
the median of an array = 3.0
the mean of the second row of an array = [[ 4. 5. 6.]
[13. 14. 15.]
[22. 23. 24.]]
vertically stacking vectors : [[1 2 3 4]
[5 6 7 8]
[1 2 3 4]
[5 6 7 8]]
horizontally stacking vectors : [[0. 0. 0. 1. 1.]
[0. 0. 0. 1. 1.]]
8
5 There is a lot more that can be done with NumPy, but these
are the most common operations
[ ]: