본문 바로가기
Data & AI Intelligence/▶Preprocessing & EDA

06.Numpy_matmul

by 류딩이2025. 10. 24.
a = np.array([-1,3, 2, 6])
b = np.array([3,6,1,2])

A = a.reshape([2,2])
print(A)
print(f"A.ndim:{A.ndim}") # 2차원
print(f"A.shape:{A.shape}") #2,2

B = np.reshape(b,[2,2])
print(B)

print(f"A+B\n:{A+B}")
print(f"A*B\n:{A*B}")
print(f"np.matmul:\n{np.matmul(A,B)}")
A		B
[[-1  3]	[[3 6]
 [ 2  6]]	 [1 2]]
A+B
:[[2 9]
 [3 8]]
 
A*B
:[[-3 18]
 [ 2 12]]
 
np.matmul:
[[ 0  0]
 [12 24]]
# matmul
결과 행렬 AB = A × B
각 원소의 계산 과정:
AB[0][0] = A[0][0]*B[0][0] + A[0][1]*B[1][0] = (-1)*3 + (3)*1 = 0
AB[0][1] = A[0][0]*B[0][1] + A[0][1]*B[1][1] = (-1)*6 + (3)*2 = 0
AB[1][0] = A[1][0]*B[0][0] + A[1][1]*B[1][0] = (2)*3 + (6)*1 = 12
AB[1][1] = A[1][0]*B[0][1] + A[1][1]*B[1][1] = (2)*6 + (6)*2 = 24

 

 

👉 왼쪽은 e, g (B의 왼쪽 세로줄),
👉 오른쪽은 f, h (B의 오른쪽 세로줄) 과 곱하면 됩니다.