- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定两个数组,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.repeat和 np.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/
我有两个列表,我想从 neg 中减去列表 pos 中元素的频率。所以: neg = [x for x in all[:800000]] pos = [x for x in all[800000:]]
我有两个列表,我想从 neg 中减去列表 pos 中元素的频率。所以: neg = [x for x in all[:800000]] pos = [x for x in all[800000:]]
我正在尝试减去 2 个标准逻辑向量并得到错误 p2 <= p1(11 downto 0)- idata(11 downto 0); Error (10327): VHDL error at sub.v
我将以下代码嵌入到类中。每当我运行 distToPoint 时,它都会给出错误“不支持的操作数类型 -: 'NoneType' 和 'float'” 我不知道为什么它会返回 NoneType 和如何让
这一直让我想知道, 假设我有这种情况: select (...long sub query..) - (...long sub query..) 我想把 - 放在条件中,这意味着有时它会是 - 有时是
我有两个 vector 。我需要从 vector1 中删除 vector2 中的内容。 [编辑:不清楚这是否意味着按照下面的链接或设置差异进行逐元素减法] 我使用 Visual Studio 2010
我有一张这样的 table : id product_property_id product_id amount type 1 1 145 10
我有两个 boolean 值列表 buy_machine 和 broken_machine。我想创建第三个列表 working_machines,它是购买的机器数量的总和并减去坏机器的数量。 我尝试了
我似乎可以解决这个问题。我有两个来自 sql 的访问者/国家/地区列表 us,us,uk,fr,uk,uk,uk 和 us,uk 我用 array_count_values() 将它们制成数组: Ar
我在 javascript 中减去时间时遇到了麻烦,尽管我已经谷歌搜索了 2 天但没有任何运气:( 我正在尝试为调查问卷计时。当用户开始调查问卷时,会记录时间戳。当用户完成/单击提交时,会记录新的时间
我正在尝试对 flex 搜索中索引的字段进行一些分析。 其中两个字段是“start_time”和“end_time”。我基本上希望将这两个字段的差异分组,即('end_time'-'start_tim
我有一个函数,它接收两个 BigDecimal 数字,即 bd1 和 bd2 作为参数。该函数应减去 bd1 - db2 并返回 bd1 和 bd2 的小数位数均为 2,结果的小数位数也应仅为 2但使
根据ldt_code中的ld源代码here。没有将dl_main传递给phdr的上下文,我对为什么通过减去虚拟地址来推断main_map的加载地址有些困惑。 我跟踪过的代码: 1124 static
我进行了多次重复测量的治疗,我想减去每次治疗的每个时间点的对照值。数据集的形状是这样的,有多年、物种和处理。 ID Year Species Treatment value 1 2010 x
我正在尝试查找一次旅行的矩形区域,可以在此处找到更多上下文 我在下面的代码中遇到的错误是: "Exception in thread "main" java.lang.ArrayIndexOutOfB
我一直在尝试使用 pandas dataframe 减去我读入 python 的列之间的日期和时间。我写的代码如下: Time = df['t'] - df['t'].shift(1) + df['t
I want to subtract all values in a[nn,...,0] by b[nn] while keeping the original structure of the ar
假设我有两个列表:List l1,和 Listl2 请帮助我如何在 2 个列表之间合并、减去和相交。谢谢。 注意:我使用的是 .NET 2.0,所以我不能使用 LINQ。谢谢。 最佳答案 以下是伪代码
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在尝试运行以下代码: extern crate unicase; use unicase::UniCase; use std::collections::HashSet; fn main() {
我是一名优秀的程序员,十分优秀!