How can I perform the following computation in a more elegant (and Pythonic) way?

user706838

Given the object data, which is of type numpy.ndarray, how can I perform the following in one line?

VERY_LOW = np.where(data<=-2, 'VERY_LOW', 'LOL').astype('S12') 
LOW = np.where((data>-2) & (data<=-1), 'LOW', VERY_LOW)
AVERAGE = np.where((data>-1) & (data<+1), 'AVERAGE', LOW)
HIGH = np.where((data>=+1) & (data<+2), 'HIGH', AVERAGE)
VERY_HIGH = np.where(data>=+2, 'VERY_HIGH', HIGH)

Basically, what I am trying to achieve is to assign a tag to each cell depending on its value (one out of five available).

Hannes Ovrén

You could write a function that maps a value to a tag, and then use the np.vectorize function to apply it.

def map_func(x):
    if x <= -2:
        return 'VERY LOW'
    elif <= -1:
        return 'LOW'
    # Keep adding more conditions here
    else:
        return 'OTHER'

vec_map_func = np.vectorize(map_func)
tag_array = vec_map_func(data)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

More elegant/Pythonic way of printing elements of tuple?

From Dev

How do I perform this arithmatic in a more elegant manner?

From Dev

How can I make this code more elegant?

From Dev

Can I print list data in a more elegant way?

From Dev

Can I do multiple actions to one component in a more elegant way?

From Dev

Can I do multiple actions to one component in a more elegant way?

From Dev

How can I make my sort more elegant?

From Dev

How can I make unwrapping this optional in Swift, a little more elegant?

From Dev

How can I refactor / make this code more pythonic?

From Dev

Is there any more pythonic way to do the following repetitive code

From Dev

Pythonic way for the following code

From Dev

More elegant way of finding out if I can convert a value into a certain type

From Dev

Can I get a property from a collection using Linq in a more elegant way than this?

From Dev

How to write the function in a more pythonic way?

From Dev

How can I write this in a more dynamic way?

From Dev

More elegant way to do this?

From Dev

a more elegant way to do this?

From Dev

Is there a more elegant way to substring?

From Dev

More pythonic way for exceptions?

From Dev

Is there a more pythonic way to write

From Java

Elegant way to perform tuple arithmetic

From Dev

More elegant way of guarantying a method can´t use negative numbers?

From Dev

How can I make this code more Pythonic - specifically, merging something into a function?

From Dev

Is there an elegant and pythonic way to require inputs for a class constructor?

From Dev

Is there an elegant, Pythonic way to count processed data?

From Dev

Can be done more pythonic?

From Dev

How to improve the code with more elegant way and low memory consumed?

From Dev

How can I speed up NIntegrate computation?

From Dev

Is there a more elegant way to write this in Python?

Related Related

  1. 1

    More elegant/Pythonic way of printing elements of tuple?

  2. 2

    How do I perform this arithmatic in a more elegant manner?

  3. 3

    How can I make this code more elegant?

  4. 4

    Can I print list data in a more elegant way?

  5. 5

    Can I do multiple actions to one component in a more elegant way?

  6. 6

    Can I do multiple actions to one component in a more elegant way?

  7. 7

    How can I make my sort more elegant?

  8. 8

    How can I make unwrapping this optional in Swift, a little more elegant?

  9. 9

    How can I refactor / make this code more pythonic?

  10. 10

    Is there any more pythonic way to do the following repetitive code

  11. 11

    Pythonic way for the following code

  12. 12

    More elegant way of finding out if I can convert a value into a certain type

  13. 13

    Can I get a property from a collection using Linq in a more elegant way than this?

  14. 14

    How to write the function in a more pythonic way?

  15. 15

    How can I write this in a more dynamic way?

  16. 16

    More elegant way to do this?

  17. 17

    a more elegant way to do this?

  18. 18

    Is there a more elegant way to substring?

  19. 19

    More pythonic way for exceptions?

  20. 20

    Is there a more pythonic way to write

  21. 21

    Elegant way to perform tuple arithmetic

  22. 22

    More elegant way of guarantying a method can´t use negative numbers?

  23. 23

    How can I make this code more Pythonic - specifically, merging something into a function?

  24. 24

    Is there an elegant and pythonic way to require inputs for a class constructor?

  25. 25

    Is there an elegant, Pythonic way to count processed data?

  26. 26

    Can be done more pythonic?

  27. 27

    How to improve the code with more elegant way and low memory consumed?

  28. 28

    How can I speed up NIntegrate computation?

  29. 29

    Is there a more elegant way to write this in Python?

HotTag

Archive