众数

一个分布的众数就是它的最频繁值。


编写一个球众数的函数mode
编写一个AllMode函数,返回按频率降序排列的值-频对。


__author__
= 'dell' import Pmf import operator


hist = Pmf.MakeHistFromList([1, 2, 2, 3, 5])
print hist.Items()


def mode(hist):
    return AllModes(hist)[0][0]


def AllModes(hist):
    s = sorted(hist.Items(), key=operator.itemgetter(1), reverse=True)
    return s

print AllModes(hist)

print mode(hist)
运行结果:
[(1, 1), (2, 2), (3, 1), (5, 1)]
[(2, 2), (1, 1), (3, 1), (5, 1)]
2
原文地址:https://www.cnblogs.com/i80386/p/3229187.html