• <small id='SxvPm'></small><noframes id='SxvPm'>

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

    1. <tfoot id='SxvPm'></tfoot>
        <legend id='SxvPm'><style id='SxvPm'><dir id='SxvPm'><q id='SxvPm'></q></dir></style></legend>

        Python中的分组/聚类数字

        时间:2023-07-04
        1. <tfoot id='UuMrS'></tfoot>

          1. <legend id='UuMrS'><style id='UuMrS'><dir id='UuMrS'><q id='UuMrS'></q></dir></style></legend>
              • <bdo id='UuMrS'></bdo><ul id='UuMrS'></ul>

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

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

                • 本文介绍了Python中的分组/聚类数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我用谷歌搜索过,我已经测试过,这让我束手无策.我有一个需要按相似度分组的数字列表.例如,在 [1, 6, 9, 100, 102, 105, 109, 134, 139] 的列表中,将 1 6 9 放入列表中,将 100、102、105 和 109 放入列表中列表,以及 134 和 139.我的数学很糟糕,我已经尝试过这个,但我无法让它工作.为了尽可能明确,我希望将彼此相距 10 个值以内的数字分组.任何人都可以帮忙吗?谢谢.

                  I've googled, I've tested, and this has me at my wits end. I have a list of numbers I need to group by similarity. For instance, in a list of [1, 6, 9, 100, 102, 105, 109, 134, 139], 1 6 9 would be put into a list, 100, 102, 105, and 109 would be put into a list, and 134 and 139. I'm terrible at math, and I've tried and tried this, but I can't get it to work. To be explicit as possible, I wish to group numbers that are within 10 values away from one another. Can anyone help? Thanks.

                  推荐答案

                  集群分析有很多方法.一种简单的方法是查看连续数据元素之间的间隙大小:

                  There are many ways to do cluster analysis. One simple approach is to look at the gap size between successive data elements:

                  def cluster(data, maxgap):
                      '''Arrange data into groups where successive elements
                         differ by no more than *maxgap*
                  
                          >>> cluster([1, 6, 9, 100, 102, 105, 109, 134, 139], maxgap=10)
                          [[1, 6, 9], [100, 102, 105, 109], [134, 139]]
                  
                          >>> cluster([1, 6, 9, 99, 100, 102, 105, 134, 139, 141], maxgap=10)
                          [[1, 6, 9], [99, 100, 102, 105], [134, 139, 141]]
                  
                      '''
                      data.sort()
                      groups = [[data[0]]]
                      for x in data[1:]:
                          if abs(x - groups[-1][-1]) <= maxgap:
                              groups[-1].append(x)
                          else:
                              groups.append([x])
                      return groups
                  
                  if __name__ == '__main__':
                      import doctest
                      print(doctest.testmod())
                  

                  这篇关于Python中的分组/聚类数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python re.sub 组: umber 之后的数字 下一篇:如何限制 Django 模型中数值字段的最大值?

                  相关文章

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

                    <small id='2yRwZ'></small><noframes id='2yRwZ'>