NumPy의 행렬 곱셈

NumPy를 사용하여 행렬 곱셈을 계산하는 방법을 살펴보겠습니다. 우리는 numpy.dot() 2개의 행렬의 곱을 구하는 방법.

 For example, for two matrices A and B. A = [[1, 2], [2, 3]] B = [[4, 5], [6, 7]] So, A.B = [[1*4 + 2*6, 2*4 + 3*6], [1*5 + 2*7, 2*5 + 3*7] So the computed answer will be: [[16, 26], [19, 31]] 

Python에서는 numpy.dot() 메서드를 사용하여 두 배열 간의 내적을 계산합니다.

예시 1 : 2개의 정사각형 행렬의 행렬 곱셈입니다.




# importing the module> import> numpy as np> > # creating two matrices> p> => [[> 1> ,> 2> ], [> 2> ,> 3> ]]> q> => [[> 4> ,> 5> ], [> 6> ,> 7> ]]> print> (> 'Matrix p :'> )> print> (p)> print> (> 'Matrix q :'> )> print> (q)> > # computing product> result> => np.dot(p, q)> > # printing the result> print> (> 'The matrix multiplication is :'> )> print> (result)>

출력 :

 Matrix p : [[1, 2], [2, 3]] Matrix q : [[4, 5], [6, 7]] The matrix multiplication is : [[16 19] [26 31]] 

예시 2: 2개의 직사각형 행렬의 행렬 곱셈입니다.




# importing the module> import> numpy as np> > # creating two matrices> p> => [[> 1> ,> 2> ], [> 2> ,> 3> ], [> 4> ,> 5> ]]> q> => [[> 4> ,> 5> ,> 1> ], [> 6> ,> 7> ,> 2> ]]> print> (> 'Matrix p :'> )> print> (p)> print> (> 'Matrix q :'> )> print> (q)> > # computing product> result> => np.dot(p, q)> > # printing the result> print> (> 'The matrix multiplication is :'> )> print> (result)>

출력 :

 Matrix p : [[1, 2], [2, 3], [4, 5]] Matrix q : [[4, 5, 1], [6, 7, 2]] The matrix multiplication is : [[16 19 5] [26 31 8] [46 55 14]]