Python:通过舍入将列表中的#个值分配给垃圾箱 [英] Python: Assigning # values in a list to bins, by rounding up

查看:118
本文介绍了Python:通过舍入将列表中的#个值分配给垃圾箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个函数,该函数可以容纳一系列和一组垃圾箱,并且基本上四舍五入到最近的垃圾箱.例如:

I want a function that can take a series and a set of bins, and basically round up to the nearest bin. For example:

my_series = [ 1, 1.5, 2, 2.3,  2.6,  3]
def my_function(my_series, bins):
    ...

my_function(my_series, bins=[1,2,3])
> [1,2,2,3,3,3]

这似乎与 Numpy's Digitize 打算这样做,但是会产生错误的值(错误值的星号):

This seems to be very close to what Numpy's Digitize is intended to do, but it produces the wrong values (asterisks for wrong values):

np.digitize(my_series, bins= [1,2,3], right=False)
> [1, 1*, 2, 2*, 2*, 3]

从文档中可以清楚看出错误的原因:

The reason why it's wrong is clear from the documentation:

我返回的每个索引都是 bins [i-1]< = x< bins [i] (如果bins是 单调递增,如果bin是,则为 bins [i-1]> x> = bins [i] 单调递减.如果x中的值超出了 bins,0或len(bins)会适当返回.如果正确为真, 然后关闭右垃圾箱,以使索引i等于 bins [i-1]< x< = bins [i]或bins [i-1]> = x> bins [i]``如果bins是 分别单调增加或减少.

Each index i returned is such that bins[i-1] <= x < bins[i] if bins is monotonically increasing, or bins[i-1] > x >= bins[i] if bins is monotonically decreasing. If values in x are beyond the bounds of bins, 0 or len(bins) is returned as appropriate. If right is True, then the right bin is closed so that the index i is such that bins[i-1] < x <= bins[i] or bins[i-1] >= x > bins[i]`` if bins is monotonically increasing or decreasing, respectively.

如果我输入减小的值并将"right"设置为True,则我可以更接近想要的值.

I can kind of get closer to what I want if I enter in the values decreasing and set "right" to True...

np.digitize(my_series, bins= [3,2,1], right=True)
> [3, 2, 2, 1, 1, 1]

,但是然后我必须考虑一种基本上有条理地反转最低编号分配(1)和最高编号分配(3)的方法.如果只有3个垃圾箱,这很简单,但是当垃圾箱的数目变多时,它将变得更加毛茸茸.必须有一种更优雅的方式来完成所有这些工作.

but then I'll have to think of a way of basically methodically reversing the lowest number assignment (1) with the highest number assignment (3). It's simple when there are just 3 bins, but will get hairier when the number of bins get longer..there must be a more elegant way of doing all this.

推荐答案

我们可以简单地使用np.digitize,将right选项设置为True,以获取索引,然后从bins中提取相应的元素. ,输入np.take,就像这样-

We can simply use np.digitize with its right option set as True to get the indices and then to extract the corresponding elements off bins, bring in np.take, like so -

np.take(bins,np.digitize(a,bins,right=True))

这篇关于Python:通过舍入将列表中的#个值分配给垃圾箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆