我有1-5个范围内的numpy浮点数,这些浮点数不是正态分布的。我想找到N-1
个截止值,将这些值分隔为N
个bin,其中每个bin具有相同数量的观察值。并非总是可以平等分配,但尽可能接近完美。它将被用于~1000次观察。
我已经使用名为discretize
的请求方法创建了一个示例。垃圾箱和临界值应按顺序递增。
import numpy as np
import random
dat = np.hstack(([random.uniform(1,5) for i in range(10)], [random.uniform(4,5) for i in range(5)]))
print dat # [4.0310121 3.53599004 1.7687312 4.94552008 2.00898982 4.5596209, ...
discrete_dat, cutoffs = discretize(dat, bins=3)
print cutoffs # 2.2, 3.8
print discrete_dat # 3, 2, 1, 3, 1, 3, ...
答案 0 :(得分:6)
好的,我只是快速攻击了这个,所以这使用np.array_split
,这样对于不等大小的bin它不会barf,这会先对数据进行排序,然后执行计算以拆分并返回cutoff:< / p>
import random
import numpy as np
dat = np.arange(1,13)/2.0
def discretize(data, bins):
split = np.array_split(np.sort(data), bins)
cutoffs = [x[-1] for x in split]
cutoffs = cutoffs[:-1]
discrete = np.digitize(data, cutoffs, right=True)
return discrete, cutoffs
discrete_dat, cutoff = discretize(dat, 3)
print "dat: {}".format(dat)
print "discrete_dat: {}".format(discrete_dat)
print "cutoff: {}".format(cutoff)
>> dat: [ 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. ]
>> discrete_dat: [0 0 0 0 1 1 1 1 2 2 2 2]
>> cutoff: [2.0, 4.0]
答案 1 :(得分:2)
>>>pd.qcut(range(5), 4, labels=False)
array([0, 0, 1, 2, 3]) 3])