用numpy对array进行maximum或加法操作,当两个参数的维度不同时,首先以加法为例。
>>>a = np.arange(5).reshape(-1,1)
>>>a # a shape is (3,1)
array([[0],
[1],
[2],
[3],
[4]])
>>>b = np.array([10,20,30])
b # b shape is (3,)
>>>array([10, 20, 30])
>>>a+b
array([[10, 20, 30],
[11, 21, 31],
[12, 22, 32],
[13, 23, 33],
[14, 24, 34]]) # a+b shape is (3,3) 用低维度的array与高维度中维度不同的axis依次相加
同理,以maximum为例,维度再加一级
>>>a = np.arange(6).reshape(-1,1,2)
>>>a
array([[[0, 1]],
[[2, 3]],
[[4, 5]]]) # a shape is (3,1,2)
>>>b = np.array([[1.1,2.1],[2.2,3.2]]) # b shape is (2,2)
>>>np.maximum(a,b)
>>>array([[[1.1, 2.1], 原理与加法一致,首先a的维度高于b,其次两者中axis=1的维度不同,因此在该axis中依次作比较
[2.2, 3.2]],
[[2. , 3. ],
[2.2, 3.2]],
[[4. , 5. ],
[4. , 5. ]]])
>>>(a+b).shape
(3, 2, 2)