在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]]

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

结论

如果你对教程有任何疑问,请在评论区提出。

更新日期:12-Feb-2020

755次浏览

开启您的职业生涯

通过完成课程获取证书

开始
广告