南强小屋 Design By 杰米
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: 32
2. 对应元素相乘 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]]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,矩阵相乘,公式
南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米
暂无python的几种矩阵相乘的公式详解的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。