1

This is a follow-up post to a previous question of mine:

Check whether numpy array row is smaller than the next

Suppose i have the following numpy array:

a=np.reshape(np.array([[79,np.nan,87,77,92,133,99,121,103,118,126, 
133,131,67]]),(7,2))

In [1]: a
Out[1]: 
array([[  79.,   nan],
       [  87.,   77.],
       [  92.,  133.],
       [  99.,  121.],
       [ 103.,  118.],
       [ 126.,  133.],
       [ 131.,  67.]])

I would like to create a new column or array which will be a True/False indicator testing the following proposition:

a[-1, 0] < a[1:, 0] and a[-1, 1] > a[1:, 1]

The result that i expect is the following:

False (because the first value of column 1 is nan)
False
True
True
False
True
False

I have tried different variations of the solutions described in my previous post, but so far i have been unsuccessful.

EDIT:

The idea is to test whether 87<92 and at the same time 77>133 which is False. Then 92<99 and 133>121 which is True etc.

2
  • Your result will be a vector 6 times False because 131 (a[-1,0]) is > every other value in that column. Same for col 2. Can you please explain your proposition more exactly? Commented Feb 27, 2018 at 22:43
  • I just did, thank you. Hopefully this is more clear now. Commented Feb 27, 2018 at 22:49

1 Answer 1

0

You will use exactly the same strategy than Tai's answer in your previous post:

b = np.diff(a, axis=0)

[[   8.   nan]
 [   5.   56.]
 [   7.  -12.]
 [   4.   -3.]
 [  23.   15.]
 [   5.  -66.]]

And now you use a the logic function np.logical_and on array b. Since you always want a trailing False, we can just append it:

result = np.logical_and(b[:,0] > 0, b[:,1] < 0)
result = np.append(result, np.array([False]))

[False False  True  True False True False]

(edited my post according to your comments)

Note: Comparing values with nan will always return False. So if nan appears in some row in the middle, it will always yield two rows that evaluate to False.

Sign up to request clarification or add additional context in comments.

7 Comments

Just to break it down: The first False = 79 < 87 and nan > 76, second False = 87 < 92 and 77 > 133, third True = 92 < 99 and 133 > 121, fourth True = 99 < 103 and 121 > 118, fifth False = 103 < 126 and 118 > 133, sixth False = 126 < 131 and 133 > 672. That's what my code does and what your description suggests. If that's wrong, please tell for every row how the values should be calculated. Especially since you build differences from a 7-row-array between neighboring rows and still have 7 results instead of 6.
You are in fact correct. My apologies, i made a mistake in the last row. The value should be 67 not 672. I corrected that now. And the last (7nth row) is always false because there is not another row to compare it with.
One more thing. Can i test the same proposition using assert? It appears that with assert in Numpy you can only test certain things.
The assert functions in numpy.testing are intended for testing. So in a test suite, you could say numpy.testing.assert_equal(result, np.array([False,False, True, True, False, True, False])) which would raise an error if not all elements are equal.
Thank you, that's really helpful. I would like to have an if statement to take action is assert fails. Should i do this using a combination of try and exception? Or is there a better way?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.