Reputation: 39
I have a nparray that contains 0 and 1 values
k = np.array([0, 1, 1, 0 ,1])
I want to transform the array into an array that contains 'blue'
if the value is 0 and 'red'
if the value is 1. I prefer to know the fastest way possible
Upvotes: 2
Views: 51
Reputation: 221574
You can use np.take
to index into a an array/list of 2
elements with those k
values as indices, like so -
np.take(['blue','red'],k)
Sample run -
In [19]: k = np.array([0, 1, 1, 0 ,1])
In [20]: np.take(['blue','red'],k)
Out[20]:
array(['blue', 'red', 'red', 'blue', 'red'],
dtype='|S4')
With the explicit indexing method -
In [23]: arr = np.array(['blue','red'])
In [24]: arr[k]
Out[24]:
array(['blue', 'red', 'red', 'blue', 'red'],
dtype='|S4')
Or with initialization with one string and then assigning the other one -
In [41]: out = np.full(k.size, 'blue')
In [42]: out[k==1] = 'red'
In [43]: out
Out[43]:
array(['blue', 'red', 'red', 'blue', 'red'],
dtype='|S4')
Runtime test
Approaches -
def app1(k):
return np.take(['blue','red'],k)
def app2(k):
arr = np.array(['blue','red'])
return arr[k]
def app3(k):
out = np.full(k.size, 'blue')
out[k==1] = 'red'
return out
Timings -
In [46]: k = np.random.randint(0,2,(100000))
In [47]: %timeit app1(k)
...: %timeit app2(k)
...: %timeit app3(k)
...:
1000 loops, best of 3: 413 µs per loop
10000 loops, best of 3: 103 µs per loop
1000 loops, best of 3: 908 µs per loop
Upvotes: 3