I have a continuous variable with a min value of 0 and a max value of 1. Therefore this includes all the float numbers from 0 to 1 inclusive.
I want to be able to map 0-0.2 to 'very low', 0.2-0.4 to 'low', 0.4-0.6 to 'medium', 0.6-0.8 to 'high' and 0.8 to 1.0 to 'very high'.
I assume that there is a simple solution to this using dictionaries. However, I am having trouble generating a the continuous ranges as keys. It seems possible that if the ranges were discrete integers then I could just use the range function.
For example:
range_dict = {range(0,2):'very low',range(2,4):'low',range(4,6):'medium',
range(6,8):'high',range(8,10):'high'}
However, when attempting the following:
range_dict[1]
I expect to receive 'very low'. However, I just get a KeyError despite 1 being in the range(0,2).
Writing this as
1 in range(0,2)
does return True.
Anyway, back to the original problem where I am dealing with a continuous variable!
I am sure there is a simple solution to this, but it obviously requires a different approach! First, is there a range function to account for continuous rather than discrete variables? Second, perhaps another problem here is precisely that dictionaries cannot map multiple to one? Thanks...
Another way to approach it would be to write an if, elif, else style function covering all the bins and returning the string at the appropriate point in the function, but I am looking specifically for a solution involving using a dictionary (since the dictionary seems a clearer way to read this within the code). Thanks again...
The bisect module has a nice way to handle this. See under Other Examples in the link above:
from bisect import bisect
def category(fl, breakpoints=[.2,.4, .6, .8, 1.1], cat=['very low', 'low', 'medium', 'high', 'very high']):
return cat[bisect(breakpoints, fl)]
category(.15) # very low
category(.2) # low
category(.99) # very high
category(.5) # medium