繁体   English   中英

划分不同形状的numpy多维数组

[英]Dividing different shaped numpy multi-dimensional array

我已经尝试了下面的第二个示例,但它没有用,但是一个小规模的示例(将numpy数组除以另一个数组)效果很好。 有什么不同? 我怎样才能使实际的部门工作?

我正在尝试做的一个小规模示例:

>>> import numpy as np
>>> a = np.asarray([[1, 2, 3], [4, 5, 6]])
>>> b = np.asarray([2, 2, 2])
>>> a / b           # works as expected
array([[0, 1, 1],
       [2, 2, 3]])

我正在努力工作的实际部门

>>> a = np.random.random((9, 31, 2, 5))
>>> b = np.random.random((9, 31, 2))
>>> a / b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (9,31,2,5) (9,31,2) 

怎么样

a = np.random.random((9, 31, 2, 5))
b = np.random.random((9, 31, 2, 1))
print((a/b).shape)

产量

(9, 31, 2, 5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM