numpy 沿轴除法
是否有一个 numpy 函数可以沿轴将数组与另一个数组中的元素分开?例如,假设我有一个形状为 (l,m,n) 的数组 a 和一个形状为 (m,) 的数组 b;我正在寻找相当于:
def divide_along_axis(a,b,axis=None):
if axis is None:
return a/b
c = a.copy()
for i, x in enumerate(c.swapaxes(0,axis)):
x /= b[i]
return c
例如,这在标准化向量数组时很有用:
>>> a = np.random.randn(4,3)
array([[ 1.03116167, -0.60862215, -0.29191449],
[-1.27040355, 1.9943905 , 1.13515384],
[-0.47916874, 0.05495749, -0.58450632],
[ 2.08792161, -1.35591814, -0.9900364 ]])
>>> np.apply_along_axis(np.linalg.norm,1,a)
array([ 1.23244853, 2.62299312, 0.75780647, 2.67919815])
>>> c = divide_along_axis(a,np.apply_along_axis(np.linalg.norm,1,a),0)
>>> np.apply_along_axis(np.linalg.norm,1,c)
array([ 1., 1., 1., 1.])
Is there a numpy function to divide an array along an axis with elements from another array? For example, suppose I have an array a with shape (l,m,n) and an array b with shape (m,); I'm looking for something equivalent to:
def divide_along_axis(a,b,axis=None):
if axis is None:
return a/b
c = a.copy()
for i, x in enumerate(c.swapaxes(0,axis)):
x /= b[i]
return c
For example, this is useful when normalizing an array of vectors:
>>> a = np.random.randn(4,3)
array([[ 1.03116167, -0.60862215, -0.29191449],
[-1.27040355, 1.9943905 , 1.13515384],
[-0.47916874, 0.05495749, -0.58450632],
[ 2.08792161, -1.35591814, -0.9900364 ]])
>>> np.apply_along_axis(np.linalg.norm,1,a)
array([ 1.23244853, 2.62299312, 0.75780647, 2.67919815])
>>> c = divide_along_axis(a,np.apply_along_axis(np.linalg.norm,1,a),0)
>>> np.apply_along_axis(np.linalg.norm,1,c)
array([ 1., 1., 1., 1.])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您给出的具体示例:将 (l,m,n) 数组除以 (m,) 您可以使用 np.newaxis:
您可以阅读有关广播规则的所有信息 此处。如果需要,您还可以多次使用 newaxis。 (例如,将形状 (3,4,5,6) 数组除以形状 (3,5) 数组)。
根据我对文档的理解,使用 newaxis + 广播还可以避免任何不必要的数组复制。
现在此处更全面地描述了索引、newaxis 等。 (自该答案首次发布以来,文档已重新组织)。
For the specific example you've given: dividing an (l,m,n) array by (m,) you can use np.newaxis:
You can read all about the broadcasting rules here. You can also use newaxis more than once if required. (e.g. to divide a shape (3,4,5,6) array by a shape (3,5) array).
From my understanding of the docs, using newaxis + broadcasting avoids also any unecessary array copying.
Indexing, newaxis etc are described more fully here now. (Documentation reorganised since this answer first posted).
我认为你可以通过 numpy 的通常广播行为来获得这种行为:
如果我解释正确的话。
如果你想要另一个轴,你可以转置所有东西:
I think you can get this behavior with numpy's usual broadcasting behavior:
If i've interpreted correctly.
If you want the other axis you could transpose everything: