我想要将两个数值numpy对象t
和speed
相乘,而不需要事先知道每个对象是标量还是数组。问题是0是t
(或t
的元素)的合法值,而inf
是speed
(或speed
的元素)的合法值。对于这种情况,我使用的模型有一条规则:当速度为无穷大且t
为零时,乘积定义为0。
我的问题是,在处理两个操作数的可能标量-可能不是的同时应用规则。到目前为止,我能想到的最好的是这个丑陋的级联:
if t.size == 1:
if t.flat[0]:
t = t * speed
else:
nonzero = t != 0
if speed.size == 1:
t[nonzero] *= speed
else:
t[nonzero] *= speed[nonzero]
我不禁想,一定有一种更有效、更具Numpythonic风格的方式,而我却错过了。在那里吗?
发布于 2019-05-12 20:40:09
已经过了几天了,没有人在hpaulj和SubhaneilLahiri的评论中写出答案,所以我最好自己来做。我之所以将此作为可接受的答案,是因为它足够通用,可以解决问题的标题,即使在超出问题中给出的特定模型的情况下也是如此。
# prepare zeros of the correct shape:
out = np.zeros(np.broadcast(t, speed).shape), dtype=float)
# write the product values into the prepared array, but ONLY
# according to the mask t!=0
np.multiply(t, speed, where=t!=0, out=out)
在问题中的特定模型中,预先准备的默认输出值0对于测试t!=0
失败的所有地方都是正确的:无论在哪里,0 * speed
都是0
,当speed
是有限的时,它也是0
(根据模型的规则)。但这种方法适用于更一般的情况:原则上,可以使用额外的numpy ufunc调用,用任意不同规则的结果填充输出数组的屏蔽部分。
发布于 2019-05-10 03:54:44
0 * np.inf
抛出一个RuntimeWarning
并计算为np.nan
,如果只做乘法,然后替换np.nan
呢?
import numpy as np
def multiply(a, b):
temp = np.array(a*b)
temp[np.isnan(temp)] = 0
return temp
测试:
np.random.seed(123)
t = np.random.rand(10)
t[0] = 0
speed = np.random.rand(10)
speed[0] = np.inf
speed[6] = np.inf
输入:
multiply(t, speed)
输出:
array([ 0. , 0.09201602, 0.02440781, 0.49796297, 0.06170694,
0.57372377, inf, 0.03658583, 0.53141369, 0.1746193 ])
https://stackoverflow.com/questions/56066019
复制相似问题