python的几种矩阵相乘的公式详解
作者:cltdevelop 时间:2021-03-28 21:13:56
1. 同线性代数中矩阵乘法的定义: np.dot()
np.dot(A, B):对于二维矩阵,计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的定义。对于一维矩阵,计算两者的内积。见如下Python代码:
import numpy as np
# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
# 2-D array: 3 x 2
two_dim_matrix_two = np.array([[1, 2], [3, 4], [5, 6]])
two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two)
print('two_multi_res: %s' %(two_multi_res))
# 1-D array
one_dim_vec_one = np.array([1, 2, 3])
one_dim_vec_two = np.array([4, 5, 6])
one_result_res = np.dot(one_dim_vec_one, one_dim_vec_two)
print('one_result_res: %s' %(one_result_res))
结果如下:
two_multi_res: [[22 28]
[49 64]]
one_result_res: 322. 对应元素相乘 element-wise product: np.multiply(), 或 *
在Python中,实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*。见如下Python代码:
import numpy as np
# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
another_two_dim_matrix_one = np.array([[7, 8, 9], [4, 7, 1]])
# 对应元素相乘 element-wise product
element_wise = two_dim_matrix_one * another_two_dim_matrix_one
print('element wise product: %s' %(element_wise))
# 对应元素相乘 element-wise product
element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one)
print('element wise product: %s' % (element_wise_2))
结果如下:
element wise product: [[ 7 16 27]
[16 35 6]]
element wise product: [[ 7 16 27]
[16 35 6]]
来源:https://blog.csdn.net/u012609509/article/details/70230204
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用SqlBulkCopy时应注意Sqlserver表中使用缺省值的列
SQL提供的进行数据传输的实用程序—BCP
spyder快捷键与python符号化输出方式
ORACLE 10g 安装教程[图文]
![](https://img.aspxhome.com/file/UploadPic/20095/26/200952620153777.jpg)
数字人组件反写[asp组件开发实例1]
![](https://img.aspxhome.com/file/UploadPic/20096/9/vb-34s.gif)
装了 Access 2003 安全更新 (KB981716) 之后 Access 打不开
互联网一家之言(一):叫用户为你买单
![](https://img.aspxhome.com/file/UploadPic/20096/9/qqe688aae59bbee69caae591bde5908d1-49s.jpg)
Oracle数据库集复制方法浅议
详解bootstrap导航栏.nav与.navbar区别
![](https://img.aspxhome.com/file/2023/7/56077_0s.png)
防止别人iframe的js
Tag与Tagging
![](https://img.aspxhome.com/file/UploadPic/200910/15/2009-10-14_175620-19s.png)