[英]Python - binning
I'm generating a series of values and would like to bin them. 我正在生成一系列值,并希望对它们进行分类。 I'd rather not use numpy or the like. 我宁愿不使用numpy之类。 Is there something more pythonic than: 还有比pythonic更多的东西:
bins = [20,30,40]
results = [0,0,0,0]
for _ in range(iterations):
x = somefunction()
for n, bin in enumerate(bins):
if x < bin:
results[n] += 1
break
else:
results[-1] += 1
final = [100 * r / float(iterations) for r in results]
print(final)
You could use something like that: 您可以使用类似这样的方法:
r = [0] * (len(bins) + 1)
for _ in xrange(iterations):
r[next((i for i, bin in enumerate(bins) if somefunction() < bin), -1)] += 1
or alternatively a counter: 或者柜台:
n = len(bins)
from collections import Counter
c = Counter()
c.update(
next((i for i, bin in enumerate(bins) if somefunction() < bin), n)
for _ in xrange(iterations)
)
It would be better/faster (for larger arrays) to use a binary search algorithm instead of a linear search algorithm. (对于较大的数组)使用二进制搜索算法而不是线性搜索算法会更好/更快。
That is, 那是,
def binm(rr,ra):
ih=len(ra)-1
il=0
if rr<ra[il]: return il
while (ih-il>1):
ie=(ih+il)/2
if rr<ra[ie]:
ih=ie
else:
il=ie
return ih
bins = [20,30,40]
results = [0,0,0,0]
for _ in range(iterations):
x = somefunction()
ib=binm(x,bins)
results[ib]+=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.