NumPy Quiz
NumPy Quiz
August 8, 2024
2 Vectors
2.0.1 Creating four vectors
dimensions of vector1 = 1
dimensions of vector2 = 1
dimensions of vector3 = 1
dimensions of vector4 = 1
1
2.0.4 Number of elements for each vector, the memory size it occupies, and other
operations
2
2.0.5 Addition, Subtraction, Multiplication & Division
# 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
# 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
3 Matrices
3.0.1 Creating three matrices
↪3x3
3
3.0.2 Getting the shape for each matrix
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
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
# 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
# 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]]
[[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]]
6
4 Other operations that can be performed with NumPy
[92]: # all zeros matrix
print(f"all zeros matrix = {np.zeros((2,2,3))}")
# random integers
print(f"random integers matrix = {np.random.randint(-4,7, size = (3,3))}")
# identity matrix
print(f"identity matrix = {np.identity(3)}")
# 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
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
[ ]: