[英]How to create an numpy array of labels from a numpy array of float?
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"
. 我想标记数字
n<0.25
标签"a"
, n>0.75
标签"c"
,标签"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. 我尝试了像
arr[arr<0.25]='a'
,但它只会工作一次,因为在下一个命令中会有与float相比较的字符串。 I can achieve this by checking its type before comparison using a for loop, but it is complicated. 我可以通过在使用for循环进行比较之前检查其类型来实现这一点,但它很复杂。 Is there a straight forward way to achieve this?
有没有直接的方法来实现这一目标?
NumPy arrays are homogeneous. NumPy数组是同类的。 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.