boolean subtract DeprecationWarning

Lyndon White picture Lyndon White · Jun 14, 2014 · Viewed 14.6k times · Source

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.

Answer

Andy Yuan picture Andy Yuan · Jun 23, 2017

maybe you should do:

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

then

x - y

at least, it works on my case.