For example, I have
arr=np.linspace(0,1,11)
and I want to mark numbers n<0.25
label "a"
, n>0.75
label "c"
, numbers between label "b"
. the result would be
array(['a', 'a', 'a', 'b', ..., 'c'])
I tried things like arr[arr<0.25]='a'
, but it will only work once since there will be strings comparing with float on the next command. I can achieve this by checking its type before comparison using a for loop, but it is complicated. Is there a straight forward way to achieve this?
NumPy arrays are homogeneous. You have to set type for label array
import numpy as np
arr=np.linspace(0,1,11)
lbl=np.empty((arr.shape), dtype=object)
lbl[arr<.25]='a'
lbl[(arr>=.25) & (arr <=.75)] = 'b'
lbl[arr>.75]='c'
print arr
print lbl
Output:
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
['a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c']
For creating an array of three such groups, you could do something like this -
ID = (arr>0.75)*1 + (arr>=0.25)
select_arr = np.array(['a','b','c'])
out = select_arr[ID]
Sample run -
In [64]: arr # Modified from sample posted in question to include 0.75
Out[64]:
array([ 0. , 0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.75,
0.9 , 1. ])
In [65]: ID = (arr>0.75)*1 + (arr>=0.25)
...: select_arr = np.array(['a','b','c'])
...: out = select_arr[ID]
...:
In [66]: out
Out[66]:
array(['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'c', 'c'],
dtype='|S1')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.