• <legend id='DVTL7'><style id='DVTL7'><dir id='DVTL7'><q id='DVTL7'></q></dir></style></legend>
  • <i id='DVTL7'><tr id='DVTL7'><dt id='DVTL7'><q id='DVTL7'><span id='DVTL7'><b id='DVTL7'><form id='DVTL7'><ins id='DVTL7'></ins><ul id='DVTL7'></ul><sub id='DVTL7'></sub></form><legend id='DVTL7'></legend><bdo id='DVTL7'><pre id='DVTL7'><center id='DVTL7'></center></pre></bdo></b><th id='DVTL7'></th></span></q></dt></tr></i><div id='DVTL7'><tfoot id='DVTL7'></tfoot><dl id='DVTL7'><fieldset id='DVTL7'></fieldset></dl></div>

      <small id='DVTL7'></small><noframes id='DVTL7'>

    1. <tfoot id='DVTL7'></tfoot>

          <bdo id='DVTL7'></bdo><ul id='DVTL7'></ul>

        Python:检查值属于哪个bin

        时间:2023-11-08
        • <tfoot id='LK4lj'></tfoot>

                <tbody id='LK4lj'></tbody>

              <small id='LK4lj'></small><noframes id='LK4lj'>

              <legend id='LK4lj'><style id='LK4lj'><dir id='LK4lj'><q id='LK4lj'></q></dir></style></legend>

                • <bdo id='LK4lj'></bdo><ul id='LK4lj'></ul>
                • <i id='LK4lj'><tr id='LK4lj'><dt id='LK4lj'><q id='LK4lj'><span id='LK4lj'><b id='LK4lj'><form id='LK4lj'><ins id='LK4lj'></ins><ul id='LK4lj'></ul><sub id='LK4lj'></sub></form><legend id='LK4lj'></legend><bdo id='LK4lj'><pre id='LK4lj'><center id='LK4lj'></center></pre></bdo></b><th id='LK4lj'></th></span></q></dt></tr></i><div id='LK4lj'><tfoot id='LK4lj'></tfoot><dl id='LK4lj'><fieldset id='LK4lj'></fieldset></dl></div>
                  本文介绍了Python:检查值属于哪个bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 range() 以相反的顺序打印列表? 下一篇:范围生成器`r_` - 具有复杂(但不是虚构)步骤的切片;使用幅度

                  相关文章

                  1. <tfoot id='aXZDR'></tfoot>
                  2. <legend id='aXZDR'><style id='aXZDR'><dir id='aXZDR'><q id='aXZDR'></q></dir></style></legend>
                    • <bdo id='aXZDR'></bdo><ul id='aXZDR'></ul>
                    <i id='aXZDR'><tr id='aXZDR'><dt id='aXZDR'><q id='aXZDR'><span id='aXZDR'><b id='aXZDR'><form id='aXZDR'><ins id='aXZDR'></ins><ul id='aXZDR'></ul><sub id='aXZDR'></sub></form><legend id='aXZDR'></legend><bdo id='aXZDR'><pre id='aXZDR'><center id='aXZDR'></center></pre></bdo></b><th id='aXZDR'></th></span></q></dt></tr></i><div id='aXZDR'><tfoot id='aXZDR'></tfoot><dl id='aXZDR'><fieldset id='aXZDR'></fieldset></dl></div>

                    <small id='aXZDR'></small><noframes id='aXZDR'>