【发布时间】:2016-01-03 15:21:01
【问题描述】:
在我提出问题之前,我先向您提供代码。
代码
from scipy import *
x = randn(10)
cum_x = cumsum(x)
#The objective is to recover x using cum_x and the diff function.
y = append(cum_x[0],diff(cum_x))
#Now, y should be equal to x but this is not confirmed by the function in1d
test = in1d(x,y)
即使 y 和 x 明显相同,变量 test 也不会返回“True”布尔值数组。这里有什么问题?
提前谢谢你。
【问题讨论】:
-
我想有浮点问题,
allclose可能是你想要的 -
是的。但为什么 in1d 不返回 True 值数组?为什么要四舍五入?您认为累积和和差异会产生稍微不同的浮点数吗?
-
我添加了一个显示精度提高的答案,您可以看到有细微差别
-
除此之外:即使
x和y没有明显不同,in1d也不是检查它们是否相等的好方法: --in1d([1,2],[2,1])给出毕竟都是真的。