I've read through most of How to convert a boolean array to an int array , but I was still at loss with, how to (most efficiently) convert a numpy
bool array to an int array, but with distinct values. For instance, I have:
>>> k=np.array([True, False, False, True, False])
>>> print k
[ True False False True False]
I'd like this to be converted to an array, where True is say, 2, and False is say 5.
Of course, I can always set up a linear equation:
>>> print 5-k*3
[2 5 5 2 5]
... which, while vectorized, needlessly employs both addition (subtraction) and multiplication; furthermore, if I'd want the opposite values (5 for True and 2 for False), I basically have to use (and recalculate) a different equation:
>>> print 2+k*3
[5 2 2 5 2]
... which is a bit of a readability issue for me.
In essence, this is merely a selection/mapping operation - but one which I'd like done in the numpy
domain. How could I do that?