【问题标题】:Displaying both modes in a bimodal distribution以双峰分布显示两种模式
【发布时间】:2015-05-25 18:01:51
【问题描述】:

我的程序遇到了一些问题,它应该找到数字列表的模式。它在大多数情况下都在工作,但是当我知道有 2 个时它只显示一种模式,并且想知道是否有任何方法可以显示它们。

    number_counts = {}
    mode = 0
    for i in order:
        if i in number_counts:
            number_counts[i] += 1
        else:
            number_counts[i] = 1
    for i in number_counts:
        most_values = max(number_counts.values())
    for key, value in number_counts.items():
        if  most_values == value:
            mode = key

【问题讨论】:

  • mode = key 您希望看到的模式之一吗?
  • 不确定你的意思... mode 是一个整数。那么为什么你期望多个值呢?
  • 另外,第二个 for 循环是没有用的,因为你独立于迭代器重复相同的操作
  • 是的,我稍后会在我的代码中打印该模式,所以我想在那里定义我的变量。并且在某些情况下,会有 2 个数字在整个列表中出现的次数相同且最常见
  • so mode 需要是一个列表

标签: python dictionary statistics mode


【解决方案1】:

这是一个简单的使用collections.Counter的两行代码:

from collections import Counter

order = [3, 5, 4, 7, 4, 3, 4, 7, 3, 6]
number_counts = Counter(order)
>>>> Counter({3: 3, 4: 3, 7: 2, 5: 1, 6: 1})

modes = [val for val,count in number_counter.items()
             if count == max(number_counter.values()) ]
>>> [3, 4]

我独立于 Shashank 写了这篇文章,后来才读到它。 itertools.takewhile 确实是从Counter.most_common() 获取所有模式的优雅方式

【讨论】:

    【解决方案2】:

    这是使用collections.Counter 及其most_common 方法的一种方法:

    from collections import Counter
    from itertools import takewhile
    counter = Counter([1,1,2,3,3,4,5,6,7])
    if counter: # Avoid IndexError on mostCommon below
        mostCommon = counter.most_common() # store once, avoid calling again
        maxCount = mostCommon[0][1] # maxCount is 2 for this example
        modes = [t[0] for t in takewhile(lambda x: x[1] == maxCount, mostCommon)]
    else:
        modes = [] #  There are no modes for an empty iterable.
    # Now modes references the list [1, 3]
    

    【讨论】:

    • 为什么不使用most_common方法获取最大值?
    • @PadraicCunningham 那是我的第一个直觉,但使用 most_common 的解决方案实际上更丑陋,更不符合 Pythonic IMO。
    • mx = c.most_common(1)[0][0] 在大型数据集上会更高效。
    • @PadraicCunningham 好吧,应该是c.most_common(1)[0][1],因为第二项是计数。尽管我认为它不那么 Pythonic,但我已经更改了答案以包含它。
    • @PadraicCunningham 我已将答案更改为使用itertools.takewhile,非常感谢您向我展示。我应该多学习itertools
    【解决方案3】:

    当然,您可以列出模式(复数):

    modes = []
    for key, value in number_counts.items():
            if  most_values == value:
                modes.append(key)
    

    然后根据需要处理列表,例如:

    number_of_modes = len(modes)
    if number_of_modes == 1:
        print('There is only one mode: {}'.format(modes[0]))
    else:
        print('There are {} modes:'.format(number_of_modes))
        for mode in modes:
            print(mode)
    

    【讨论】:

      【解决方案4】:
      number_counts = {}
      mode = []
      most_values = 0
      
      for i in order:
          if i in number_counts:
              number_counts[i] += 1
          else:
              number_counts[i] = 1
          if number_counts[i] > most_values:
              most_values = number_counts[i]
      for key, value in number_counts.iteritems():
          if  most_values == value:
              mode.append(key)
      

      【讨论】:

        猜你喜欢
        • 2021-08-16
        • 2012-07-16
        • 1970-01-01
        • 2014-01-15
        • 2017-04-11
        • 2011-07-23
        • 2015-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多