boolean subtract DeprecationWarning

若如初见. 提交于 2019-12-23 09:07:09

问题


I recently upgraded to numpy 1.9dev. (For improved OpenBlas support).

I have some code that does x-y Where x and y are samples from a probability distribution. If the distribution is Bernoulli, then they are boolean. If the distribution is Gaussian, then they are floats.

where depending on the path followed x and y might be bools or floats. I don't have to care as python has duck-typing. If it can subtract then it is a valid value for x and y

I get this warning:

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

I have made the warning go away, by casting it to always be a float. This may be a good thing since it makes the code more consistent at a lower level. (Not sold on that as a good thing).

What is the correct action to be taking? I can't use boolean or bitwise xor as when x and y are floats this will break. It would be ugly to make the code branch on the type of x and y.


回答1:


maybe you should do:

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

then

x - y

at least, it works on my case.




回答2:


Here my solution:

z = (x.astype(np.float32) - y.astype(np.float32)).astype(np.bool)


来源:https://stackoverflow.com/questions/24216210/boolean-subtract-deprecationwarning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!