numpy.matmul()



numpy.matmul() 函式返回兩個陣列的矩陣乘積。雖然它對於二維陣列返回普通乘積,但如果任一引數的維度 >2,則將其視為位於最後兩個索引中的矩陣堆疊,並相應地進行廣播。

另一方面,如果任一引數是一維陣列,則透過在其維度上附加 1 來將其提升為矩陣,該維度在乘法後被移除。

示例

# For 2-D array, it is matrix multiplication 
import numpy.matlib 
import numpy as np 

a = [[1,0],[0,1]] 
b = [[4,1],[2,2]] 
print np.matmul(a,b)

它將產生以下輸出:

[[4  1] 
 [2  2]] 

示例

# 2-D mixed with 1-D 
import numpy.matlib 
import numpy as np 

a = [[1,0],[0,1]] 
b = [1,2] 
print np.matmul(a,b) 
print np.matmul(b,a)

它將產生以下輸出:

[1  2] 
[1  2] 

示例

# one array having dimensions > 2 
import numpy.matlib 
import numpy as np 

a = np.arange(8).reshape(2,2,2) 
b = np.arange(4).reshape(2,2) 
print np.matmul(a,b)

它將產生以下輸出:

[[[2   3] 
   [6   11]] 
  [[10  19] 
   [14  27]]]
numpy_linear_algebra.htm
廣告