𧩠Numpy(2)
π Element-wise multiplication
m = np.array([[1,2,3],[4,5,6]])
n = m * 0.25
print(n)
print(m * n)
print(np.multiply(m,n))
> [[0.25 0.5 0.75]
[1. 1.25 1.5 ]]
[[0.25 1. 2.25]
[4. 6.25 9. ]]
[[0.25 1. 2.25]
[4. 6.25 9. ]]
π Matrix product
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a.shape)
b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(b.shape)
c = np.matmul(a,b)
print(c.shape)
print(c)
> (2, 4)
(4, 3)
(2, 3)
[[ 70 80 90]
[158 184 210]]
π Dot product
a = np.array([[1,2],[3,4]])
print(np.dot(a,a))
print(a.dot(a))
print(np.matmul(a,a))
> [[ 7 10]
[15 22]]
[[ 7 10]
[15 22]]
[[ 7 10]
[15 22]]
π Transpose
m = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
m.T
print(m)
print(m.T)
> [[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]