没有独特的模式;找到2个相同的共同值

时间:2018-06-21 02:44:33

标签: python statistics usage-statistics

我想在python中使用statistics.mode([1,1,2,2,3])函数来计算模式,它返回no unique mode; found 2 equally common values。这样,我想输出12。有人有什么好主意吗?

8 个答案:

答案 0 :(得分:2)

from collections import Counter
c = Counter([1,1,2,2,3])
c.most_common(1)
# output
>>> [(1,2)] #the key 1, 2 occurrences.

从文档中:

  

“ most_common([n]):返回n个最常见元素的列表,以及   他们的人数从最常见到最少。相等的元素   计数是任意排序的”

答案 1 :(得分:2)

请注意,在Python 3.8中,statistics.mode的行为已更改:

在3.8版中进行了更改:现在通过返回遇到的第一个模式来处理多峰数据集。以前,当发现多个模式时会引发StatisticsError。

在您的示例中:

from statistics import mode

mode([1, 1, 2, 2, 3])
# 1

也可以从Python 3.8开始,也可以使用statistics.multimode以最先出现的值的顺序返回它们的列表:

from statistics import multimode

multimode([1, 1, 2, 2, 3])
# [1, 2]

答案 2 :(得分:1)

尝试以下功能,当没有唯一模式时,该最大值将作为模式找到:

import statistics
def find_max_mode(list1):
    list_table = statistics._counts(list1)
    len_table = len(list_table)

    if len_table == 1:
        max_mode = statistics.mode(list1)
    else:
        new_list = []
        for i in range(len_table):
            new_list.append(list_table[i][0])
        max_mode = max(new_list) # use the max value here
    return max_mode

if __name__ == '__main__':
    a = [1,1,2,2,3]
    print(find_max_mode(a)) # print 2

答案 3 :(得分:1)

from scipy import stats as s
a=[1,1,2,2,3]
print(int(s.mode(a)[0]))

答案 4 :(得分:1)

当没有唯一模式时,试试这个以找到最大值作为模式:

max([p[0] for p in statistics._counts([1, 1, 2, 2, 3])])

答案 5 :(得分:0)

例如:

lst = [1, 1, 2, 2, 3]

# test for count lst elements
lst_count = [[x, lst.count(x)] for x in set(lst)]
print lst_count
# [[1, 2], [2, 2], [3, 1]]

# remove count <= 1
lst_count = [x for x in set(lst) if lst.count(x) > 1]
print lst_count
# [1, 2]

# get 1 or 2 by index
print lst_count[0], lst_count[1]
# 1 2

另一种方式:

from collections import Counter

# change lst elements to str, for more readable
lst = ['a', 'a', 'b', 'b', 'c']

# get a dict, key is the elements in lst, value is count of the element
d_mem_count = Counter(lst)
print d_mem_count
# Counter({'a': 2, 'b': 2, 'c': 1})

for k in d_mem_count.keys():
    if d_mem_count[k] > 1:
        print k

# output as below
# a
# b

答案 6 :(得分:0)

我只是遇到了同样的问题。这是我很简单地解决它的方法:

def most_element(liste):
    numeral=[[liste.count(nb), nb] for nb in liste]
    numeral.sort(key=lambda x:x[0], reverse=True)
    return(numeral[0][1])

不确定这是最优雅的方法,但它确实可以完成:)。希望对您有帮助

答案 7 :(得分:0)

def MultiModeCalc(data):
    """
    set(data) -> discards duplicated values

    list(map(lambda x: data.count(x), set(data))))) -> counting how many of 
    each value there are

    dict(zip(set(data), list(map(lambda x: data.count(x), set(data))))) -> 
    making a dictionary by zipping all unique values(as keys) with their 
    frequency in the data(as values)

    return [i for i in multimode if multimode[i] == max(multimode.values())] 
    -> returning a list of the values that are most frequent in the data 
    """
    multimode = dict(zip(set(data), list(map(lambda x: data.count(x), set(data)))))
    return [i for i in multimode if multimode[i] == max(multimode.values())]
dataset = [1,2,5,6,3,7,2,3,9,1,10,3,1,2,9,9]
print(MultiModeCalc(dataset))
# [1, 2, 3, 9]

一个枯燥的答案。