gpt4 book ai didi

python - 从矩阵 B 的每一行中减去矩阵 A 的每一行,无需循环

转载 作者:太空狗 更新时间:2023-10-30 02:38:03 25 4
gpt4 key购买 nike

给定两个数组,A (形状:M X C)和B (形状:N X C),有没有办法减去 A 的每一行?来自 B 的每一行不使用循环?最终输出的形状为 (M N X C)。


例子

A = np.array([[  1,   2,   3], 
[100, 200, 300]])

B = np.array([[ 10, 20, 30],
[1000, 2000, 3000],
[ -10, -20, -2]])

期望的结果(可以有一些其他形状)(已编辑):

array([[  -9,   -18,   -27],
[-999, -1998, -2997],
[ 11, 22, 5],
[ 90, 180, 270],
[-900, -1800, -2700],
[ 110, 220, 302]])

Shape: 6 X 3

(循环太慢,“outer”减去每个元素而不是每一行)

最佳答案

通过利用 broadcasting 可以高效地完成它(不使用任何循环)喜欢:

In [28]: (A[:, np.newaxis] - B).reshape(-1, A.shape[1])
Out[28]:
array([[ -9, -18, -27],
[ -999, -1998, -2997],
[ 11, 22, 5],
[ 90, 180, 270],
[ -900, -1800, -2700],
[ 110, 220, 302]])

或者,为了比 broadcasting 快一点的解决方案,我们将不得不使用 numexpr喜欢:

In [31]: A_3D = A[:, np.newaxis]
In [32]: import numexpr as ne

# pass the expression for subtraction as a string to `evaluate` function
In [33]: ne.evaluate('A_3D - B').reshape(-1, A.shape[1])
Out[33]:
array([[ -9, -18, -27],
[ -999, -1998, -2997],
[ 11, 22, 5],
[ 90, 180, 270],
[ -900, -1800, -2700],
[ 110, 220, 302]], dtype=int64)

另一种效率最低的方法是使用 np.repeatnp.tile匹配两个数组的形状。但是,请注意,这是效率最低的选项,因为它会在尝试匹配形状时生成副本

In [27]: np.repeat(A, B.shape[0], 0) - np.tile(B, (A.shape[0], 1))
Out[27]:
array([[ -9, -18, -27],
[ -999, -1998, -2997],
[ 11, 22, 5],
[ 90, 180, 270],
[ -900, -1800, -2700],
[ 110, 220, 302]])

关于python - 从矩阵 B 的每一行中减去矩阵 A 的每一行,无需循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com