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() メソッドを使用して、2 つの配列間のドット積を計算します。
例 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 つの長方形行列の行列乗算。
Java文字列を配列に入れる
# 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]]>