[Linear Algebra](python) Numpy(2)

berry Β·2021λ…„ 8μ›” 2일
0

Linear Algebra

λͺ©λ‘ 보기
2/5
post-thumbnail

🧩 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)) # you can call `dot` directly on the `ndarray`
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]]


profile
Engineer

0개의 λŒ“κΈ€