介绍
NumPy 是一个用于 Python 编程语言的库,它增加了对大型多维数组和矩阵的支持,并提供了大量高级数学函数。在本实验中,我们将介绍 NumPy 库中的矩阵乘法概念。
虚拟机使用提示
虚拟机启动完成后,点击左上角切换到 Notebook 标签页,以访问 Jupyter Notebook 进行练习。
有时,你可能需要等待几秒钟,直到 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。
如果你在学习过程中遇到问题,随时可以询问 Labby。请在实验结束后提供反馈,我们将及时为你解决问题。
导入所需库
import numpy as np
定义矩阵
A = np.array([[1,2,3], [4,5,6],[1,2,1]])
B = np.array([[1,1,1], [0,1,0], [1,1,1]])
print("Matrix A:\n", A)
print("Matrix B:\n", B)
使用 multiply() 函数
print("Element-wise multiplication of matrix A and B:")
print(np.multiply(A, B))
使用 matmul() 函数
print("Matrix multiplication of matrix A and B:")
print(np.matmul(A, B))
使用 dot() 函数
print("Dot product of matrix A and B:")
print(np.dot(A, B))
使用三种方法
print("Matrix multiplication of matrix A and B using all three methods:")
print("Using 'multiply()':")
print(np.multiply(A, B))
print("Using 'matmul()':")
print(np.matmul(A, B))
print("Using 'dot()':")
print(np.dot(A, B))
总结
在本实验中,我们学习了 NumPy 库中矩阵乘法的概念。我们通过示例介绍了三种矩阵乘法的方式,例如 multiply()、matmul() 和 dot() 函数。这些函数能够执行不同的操作,例如逐元素乘法、矩阵乘法以及两个矩阵的标量或点积。