tags:

views:

188

answers:

4

I have a numpy array with positive and negative values in.

a = array([1,1,-1,-2,-3,4,5])

I want to create another array which contains a value at each index where a sign change occurs (For example, if the current element is positive and the previous element is negative and vice versa).

For the array above, I would expect to get the following result

array([0,0,1,0,0,1,0])

Alternatively, a list of the positions in the array where the sign changes occur or list of booleans instead of 0's and 1's is fine.

+4  A: 

Something like

a = array([1,1,-1,-2,-3,4,5])
asign = np.sign(a)
signchange = ((np.roll(asign, 1) - asign) != 0).astype(int)
print signchange
array([0, 0, 1, 0, 0, 1, 0])

Now, numpy.roll does a circular shift, so if the last element has different sign than the first, the first element in the signchange array will be 1. If this is not desired, one can of course do a simple

signchange[0] = 0

Also, np.sign considers 0 to have it's own sign, different from either positive or negative values. E.g. the "signchange" array for [-1,0,1] would be [0,1,1] even though the zero line was "crossed" only once. If this is undesired, one could insert the lines

sz = asign == 0
while sz.any():
    asign[sz] = np.roll(asign, 1)[sz]
    sz = asign == 0

between lines 2 and 3 in the first example.

janneb
Note that this will indicate a sign change in the first position if the last element is a different sign than the first. It also considers 0 to be a different sign than positive or negative. So `[-1, 0, 1]` will give `signchange = [1, 1, 1]`. This may be desired behavior, but I thought I'd point it out.
tgray
@tgray Yeah, I amended my answer to point out how to fix those issues, if desired.
janneb
+1  A: 

How about

[0 if x == 0 else 1 if numpy.sign(a[x-1]) != numpy.sign(y) else 0 for x, y in enumerate(a)]

numpy.sign assigns 0 its own sign, so 0s will be sign changes from anything except other 0s, which is probably what you want.

Personman
+1  A: 

The answers above use list comprehensions and some numpy magic to get the result you want. Here is a very straight forward, if a little convoluted, way of doing the same:

import numpy as np

arr = np.array([1,1,-1,-2,-3,4,5])

result = []
for i, v in enumerate(arr):
    if i == 0:
        change = False
    elif v < 0 and arr[i-1] > 0:
        change = True
    elif v > 0 and arr[i-1] < 0:
        change = True
    else:
        change = False

    result.append(change)

print result
rioch
it won't register a change in `np.array([1,0,-1])`
Justin Peel
A: 

For the direct interpretation of this question, where 0's aren't their own case, it's probably easier to use greater than sign. Here's an example:

a = array([1, 1, -1, -2, -3, 0, 4, 0, 5, 6])

x = greater_equal(a, 0)
sign_change = x[:-1]-x[1:]

Which gives, when printed with T or F to indicate the sign change between different numbers:

 1 F 1 T -1 F -2 F -3 T 0 F 4 F 0 F 5 F 6

when printed using:

print `a[0]`+"".join([(" T" if sign_change[i] else " F")+" "+`a[i+1]` for i in range(len(sign_change))])

Also note that this is one element shorter than the original array, which makes sense since you're asking for the change of sign. If you want to include the change between the last and first element, you can use roll, as others have suggested.

tom10