100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python numpy 矩阵加减规则 ValueError: operands could not be broadcast together with shapes

python numpy 矩阵加减规则 ValueError: operands could not be broadcast together with shapes

时间:2019-06-12 21:39:27

相关推荐

python numpy 矩阵加减规则 ValueError: operands could not be broadcast together with shapes

矩阵的加减

矩阵大小不一的加减在numpy中只需要注意两个运算规则:

两个矩阵有一行或一列维度相等。

其中一个矩阵的必须为1。

满足这两个条件就可以进行numpy的广播规则。

Numpy的广播既是在2个不同的矩阵运算过程中,Numpy将较小的数组拉伸成较大数组的形状(shape),然后Numpy加减乘除不同矩阵的加减乘除运算

下面是代码实例:

import numpy as npb = np.random.randn(2, 3) # a.shape = (2, 3)a = np.random.randn(3, 3) # b.shape = (2, 1)print("a:")print(a)print("b:")print(b)c = a - bprint("c:")print(c)c_t=a.T-b.Tprint("c_t:")print(c_t)

输出为:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。