gpt4 book ai didi

python - bool 减法 DeprecationWarning

转载 作者:太空狗 更新时间:2023-10-30 00:40:36 27 4
gpt4 key购买 nike

我最近升级到 numpy 1.9dev。(为了改进 OpenBlas 支持)。

我有一些代码可以执行 x-y其中 xy 是来自概率分布的样本。如果分布是伯努利分布,则它们是 bool 值。如果分布是高斯分布,则它们是 float 。

根据路径的不同,xy可能是 bool 值或 float 。我不必在意,因为 python 有鸭子类型(duck typing)。如果它可以减去,则它是 xy

的有效值

我收到这个警告:

DeprecationWarning: numpy boolean subtract (the binary - operator) is deprecated, use the bitwise_xor (the ^ operator) or the logical_xor function instead.

我已将警告消失,方法是将其始终设为 float 。这可能是一件好事,因为它使代码在较低级别上更加一致。(不把它当作好东西卖)。

应该采取的正确行动是什么?我不能使用 bool 值或按位异或,因为当 xy 是 float 时,这会中断。在 xy 类型上创建代码分支会很丑陋。

最佳答案

也许你应该这样做:

x=x.astype(numpy.float32)
y=y.astype(numpy.float32)

然后

x - y

至少,它适用于我的情况。

关于python - bool 减法 DeprecationWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216210/

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