Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean subtract DeprecationWarning

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.

like image 212
Lyndon White Avatar asked Jun 14 '14 02:06

Lyndon White


People also ask

How do you subtract Boolean?

There is no such thing as subtraction in the realm of Boolean mathematics.

How do you negate a Boolean array?

Use the numpy. logical_not() method to negate a numpy array of booleans, e.g. result = numpy. logical_not(arr) . The logical_not() method applies the logical not operator to the elements in the array and returns the result.

What does Numpy subtract do?

numpy. subtract() returns the difference of the two arrays element-wise. The return type is either ndarray or scalar depending on the input type.

How do you subtract a value from a Numpy array?

The most straightforward way to subtract two matrices in NumPy is by using the - operator, which is the simplification of the np. subtract() method - NumPy specific method designed for subtracting arrays and other array-like objects such as matrices.


2 Answers

maybe you should do:

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

then

x - y

at least, it works on my case.

like image 101
Andy Yuan Avatar answered Sep 25 '22 09:09

Andy Yuan


Here my solution:

z = (x.astype(np.float32) - y.astype(np.float32)).astype(np.bool)
like image 30
Alecs Avatar answered Sep 23 '22 09:09

Alecs