0

The function values[(values<=0).argmin():] = 0 turns from the nth element to the last elements values into zeroes if the number is below 0 as seen in the Int array output. however when the array elements are floats the arrays are automatically converted into zeroes in the float array output. How can I fix this?

Float array

Numbers = np.array([123.6 , 123.6 ,  123.6, 110.3748 ,  111.6992976, 102.3165566,  97.81462811])
Numbers[(Numbers>=-50).argmin():] = 0

Output

[ 10   3 -20   0   0   0]

Int array

import numpy as np
values = np.array([123 , 123 , -123.6,  110,  111])
values[(values<=0).argmin():] = 0

Output:

[123 , 123 , 0,  0,  0]
2
  • what are you trying to achieve and what does the first output represent? Commented Feb 16, 2021 at 7:34
  • Look at values before doing the 0 insert. Commented Feb 16, 2021 at 7:52

1 Answer 1

3

EDIT:

Try these 2 methods, one is in place assignment to the NumPy view and the other creates a fresh array to be assigned to another variable -

#Method 1 (Inplace assignment)
Numbers[(Numbers<=0).cumsum(dtype=bool)] = 0

#OR

#Method 2 (Not inplace)
np.where(~(Numbers<=0).cumsum(dtype=bool), Numbers, 0)

Explanation -

  1. The bool array that returns [F, F, F, T, F, F, F] can be seen as an array of 1s and 0s. Doing a cumsum ends up propogating the first T to the subsequent elements.

  2. This, therefore, turns the array as [F, F, F, T, T, T, T] which can now be used with just boolean indexing and set the view to 0 OR np.where to fetch original elements or 0 based on reversing the boolean with ~

  3. Advantage here is that if your array is just composed of False, meaning no element meets the condition, it just returns the original Numbers itself, instead of setting them to 0.


Running tests -

  1. With a value that meets condition
Numbers = np.array([123.6 , 123.6 ,  -123.6, 110.3748 ,  111.6992976, 102.3165566,  97.81462811])

Numbers[(Numbers<=0).cumsum(dtype=bool)] = 0

#array([123.6, 123.6,   0. ,   0. ,   0. ,   0. ,   0. ])
  1. With no values meeting the condition
Numbers = np.array([123.6 , 123.6 ,  123.6, 110.3748 ,  111.6992976, 102.3165566,  97.81462811])

Numbers[(Numbers<=0).cumsum(dtype=bool)] = 0

#array([123.6 , 123.6 , 123.6 , 110.3748 ,111.6992976 , 102.3165566 ,  97.81462811])
Sign up to request clarification or add additional context in comments.

6 Comments

I found out that the problem with the function is if none of the elements meets the criteria it just turns all the numbers into zeroes.
I ma de another issue about this if you could take a look I would appreciate it: stackoverflow.com/questions/66233741/…
Updated my answer, the alternate thread as well
Does this work as you expect it? were you able to understand the logic behind it? do let me know.
Yes it works thank you, sorry for the late response
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.