-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiply_array_numpy.py
More file actions
53 lines (40 loc) · 3.37 KB
/
multiply_array_numpy.py
File metadata and controls
53 lines (40 loc) · 3.37 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(a)
print(b)
# element-wise multiplication of two arrays a and b
print(a * b) # element-wise multiplication of two arrays a and b, it will multiply each element of array a with the corresponding element of array b and return a new array with the
print(a.dot(b)) # matrix multiplication of two arrays a and b, it will perform the dot product of array a and array b and return a new array with the result of the matrix multiplication
# matrix multiplication of two arrays a and b
print(a @ b) # it is another way to perform matrix multiplication of two arrays a and b, it will give the same result as a.dot(b) because the @ operator is used for matrix multiplication in Python
print(np.dot(a, b)) # it is another way to perform matrix multiplication of two arrays a and b, it will give the same result as a.dot(b) because np.dot() function is used for matrix multiplication in NumPy
# multipltt two 3x3 random matrices
c = np.random.randint(1,40,(3, 3))
d = np.random.randint(1,40,(3, 3))
print(c)
print(d)
print(c @ d)
# transpose of a matrix
print(c.T) # it will print the transpose of the matrix c, which is obtained by swapping the rows and columns of the matrix c
print(d.T) # it will print the transpose of the matrix d, which is obtained by swapping the rows and columns of the matrix d
# transpose of 2x3 random matrix
e = np.random.randint(1,40,(2, 3))
print(e)
print(e.T) # it will print the transpose of the matrix e, which is obtained by swapping the rows and columns of the matrix e, it will give a 3x2 matrix
# add matrix with its transpose
print(c + c.T) # it will add the matrix c with its transpose c.T, it will give a new matrix where each element is the sum of the corresponding elements of matrix c
# diagonal of a matrix
print(np.diag(c)) # it will print the diagonal elements of the matrix c, which are the elements that are located on the diagonal of the matrix c, it will return a
#sum of diagonal elements of a matrix
print(np.trace(c)) # it will print the sum of the diagonal elements of the matrix c, which is the sum of the elements that are located on the diagonal of the matrix c,
# filtering elements of a matrix
print(c[c > 20]) # it will print the elements of the matrix c that are greater than 20, it will return a new array with the elements that satisfy the condition c
print(d[d < 10]) # it will print the elements of the matrix d that are less than 10, it will return a new array with the elements that satisfy the condition d < 10
print(e[e % 2 == 0]) # it will print the even elements of the matrix e, it will return a new array with the elements that satisfy the condition e % 2 == 0, which means the elements that are divisible by 2 without leaving a remainder
print(e[e % 2 != 0]) # it will print the odd elements of the matrix e, it will return a new array with the elements that satisfy the condition e % 2 != 0, which means the elements that are not divisible by 2 without leaving a remainder
# replacing negative elements of a matrix with 0
f = np.random.randint(-10, 10, (3, 3))
print(f)
f[f < 0] = 0 # it will replace the negative elements of the matrix f with 0, it will modify the original matrix f by setting the elements that satisfy the condition f < 0 to 0
print(f) # it will print the modified matrix f, where all the negative elements have been replaced with 0