Python:检查值属于哪个 bin [英] Python: Checking to which bin a value belongs

查看:70
本文介绍了Python:检查值属于哪个 bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值列表和一个 bin 边缘列表.现在我需要检查它们所属的所有值.有没有比遍历值然后遍历 bin 并检查该值是否属于当前 bin 更 Pythonic 的方法,例如:

my_list = [3,2,56,4,32,4,7,88,4,3,4]箱 = [0,20,40,60,80,100]对于 my_list 中的 i:对于范围内的 j(len(bins)):如果 bins(j) <我<箱(j+1):做一点事

这对我来说看起来不是很漂亮.谢谢!

解决方案

可能为时已晚,但为了将来参考,numpy 有一个功能可以做到这一点:

<块引用>

http://docs.scipy.org/doc/numpy/reference/generated/numpy.digitize.html

<预><代码>>>>my_list = [3,2,56,4,32,4,7,88,4,3,4]>>>箱 = [0,20,40,60,80,100]>>>np.digitize(my_list,bins)数组([1, 1, 3, 1, 2, 1, 1, 5, 1, 1, 1])

结果是一个索引数组,对应于来自 my_list 的每个元素也属于的 bins 的 bin.请注意,该函数还会对超出第一个和最后一个 bin 边缘的值进行 bin:

<预><代码>>>>my_list = [-5,200]>>>np.digitize(my_list,bins)数组([0, 6])

熊猫也有类似的东西:

<块引用>

http://pandas.pydata.org/pandas-docs/dev/basics.html#discretization-and-quantiling

<预><代码>>>>pd.cut(my_list, bins)分类:数组(['(0, 20]', '(0, 20]', '(40, 60]', '(0, 20]', '(20, 40]', '(0, 20]'),'(0, 20]', '(80, 100]', '(0, 20]', '(0, 20]', '(0, 20]'], dtype=object)Levels (5): Index(['(0, 20]', '(20, 40]', '(40, 60]', '(60, 80]','(80, 100]'], dtype=object)

I have a list of values and a list of bin edges. Now I need to check for all values to what bin they belong to. Is there a more pythonic way than iterating over the values and then over the bins and checking if the value belongs to the current bin, like:

my_list = [3,2,56,4,32,4,7,88,4,3,4]
bins = [0,20,40,60,80,100]

for i in my_list:
    for j in range(len(bins)):
        if bins(j) < i < bins(j+1):
            DO SOMETHING

This doesn't look very pretty to me. Thanks!

解决方案

Probably too late, but for future reference, numpy has a function that does just that:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.digitize.html

>>> my_list = [3,2,56,4,32,4,7,88,4,3,4]
>>> bins = [0,20,40,60,80,100]
>>> np.digitize(my_list,bins)
array([1, 1, 3, 1, 2, 1, 1, 5, 1, 1, 1])

The result is an array of indexes corresponding to the bin from bins that each element from my_list belongs too. Note that the function will also bin values that fall outside of your first and last bin edges:

>>> my_list = [-5,200]
>>> np.digitize(my_list,bins)
array([0, 6])

And Pandas has something like it too:

http://pandas.pydata.org/pandas-docs/dev/basics.html#discretization-and-quantiling

>>> pd.cut(my_list, bins)
Categorical: 
array(['(0, 20]', '(0, 20]', '(40, 60]', '(0, 20]', '(20, 40]', '(0, 20]',
       '(0, 20]', '(80, 100]', '(0, 20]', '(0, 20]', '(0, 20]'], dtype=object)
Levels (5): Index(['(0, 20]', '(20, 40]', '(40, 60]', '(60, 80]',
                   '(80, 100]'], dtype=object)

这篇关于Python:检查值属于哪个 bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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