在Python中使用Numpy矩陣乘法


在本教程中,我們將學習如何使用Python中的NumPy庫對兩個矩陣進行乘法。藉助NumPy庫,很容易實現。

它有一個名為dot的方法,用於矩陣乘法。你可以使用以下命令安裝NumPy庫。

pip install numpy

讓我們瞭解程式中涉及的步驟。

  • 匯入NumPy庫。

  • 初始化矩陣。

  • 使用numpy.dot(matrix_1, matrix_2)方法對矩陣進行乘法,並將結果儲存在變數中。

  • 列印結果。

請看以下程式碼。

示例

# importing the module
import numpy
# initializing the matrices matrix_1 = [
      [1, 2, 3], [4, 5, 6], [7, 8, 9]
   ] matrix_2 = [
      [7, 8, 9], [4, 5, 6],[1, 2, 3]
   ]
# multiplying the two matrices
result = numpy.dot(matrix1, matrix2)
# printing the result
print(result)

輸出

如果你執行以上程式,你將獲得以下結果。

[[ 18 24 30]
[ 54 69 84]
[ 90 114 138]]

結論

如果你對教程有任何疑問,請在評論區提出。

更新日期:12-Feb-2020

755次瀏覽

開啟您的職業生涯

透過完成課程獲取證書

開始
廣告
© . All rights reserved.