np.diff(np.signbit())不再找到过零点,更好的方法?

时间:2018-01-17 05:12:41

标签: python-2.7 numpy

我正在寻找这个answer,但由于布尔减法已经折旧,它不再有效。还有更好的方法吗?

A   = np.array([[-2, -1,  1], 
                [-1,  0,  1], 
                [-1,  1,  2]], dtype=float)

zcs = np.diff(np.signbit(A), axis=1)     # find zero crossings in each row - now fails

产生

Traceback (most recent call last):
  File "/Users/david/Documents/wow.py", line 4, in <module>
    zcs = np.diff(np.signbit(A), axis=1)     # now fails
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1926, in diff
    return a[slice1]-a[slice2]

TypeError: numpy boolean subtract, the `-` operator, is deprecated, use the bitwise_xor, the `^` operator, or the logical_xor function instead.

1 个答案:

答案 0 :(得分:1)

这显然已在1.14.0中修复,但作为快速修复显式类型转换:

zcs = np.diff(np.signbit(A).astype(int), axis=1) 

应该有效。