如何在数组中找到第二个最常见的数字?

3

我尝试使用scipy.stats模块来查找最常见的值。然而,我的矩阵中包含很多零,因此这总是众数。

例如,如果我的矩阵看起来像下面这样:

array = np.array([[0, 0, 3, 2, 0, 0],
             [5, 2, 1, 2, 6, 7],
             [0, 0, 2, 4, 0, 0]])

我希望返回数值2

那你使用 scipy.stats 时发生了什么?而且这不是有效的列表或 Python 数组!! - Mazdak
如果您不想包括0,则应将其从数据中过滤,然后获取结果的统计信息。 - Barmar
{...} 是表示 set 的符号,而不是 listlist 使用 [...] 符号(Python 中没有 array)。set 中每个元素只会出现一次,因此它不可能有“第二常见的数字”。另外,你在 set 中的元素之间缺少逗号,所以你写的甚至不是有效的 Python 代码。 - ArtOfWarfare
可能OP在谈论一个numpy数组,因为他们正在使用scipy.stats - Lynn
3个回答

10

试试使用collections.Counter

import numpy as np
from collections import Counter

a = np.array(
  [[0, 0, 3, 2, 0, 0],
   [5, 2, 1, 2, 6, 7],
   [0, 0, 2, 4, 0, 0]]
)

ctr = Counter(a.ravel())
second_most_common_value, its_frequency = ctr.most_common(2)[1]

0

正如一些评论中提到的那样,您可能在谈论numpy数组。

在这种情况下,遮蔽您想要避免的值相当简单:

import numpy as np
from scipy.stats import mode

array = np.array([[0, 0, 3, 2, 0, 0],
                 [5, 2, 1, 2, 6, 7],
                 [0, 0, 2, 4, 0, 0]])
flat_non_zero = array[np.nonzero(array)]
mode(flat_non_zero)

返回的是(array([2]), array([ 4.])),意思是出现最多的值是2,它出现了4次(更多信息请参见doc)。因此,如果您只想得到2,您只需要获取mode返回值的第一个索引:mode(flat_non_zero)[0][0]

编辑:如果您想从数组中过滤出另一个特定值x而不是零,则可以使用array[array != x]


0
original_list = [1, 2, 3, 1, 2, 5, 6, 7, 8]  #original list
noDuplicates = list(set(t))                  #creates a list of all the unique numbers of the original list

most_common = [noDuplicates[0], original_list.count(noDuplicates[0])]  #initializes most_most common to 
                                                                       #the first value and count so we have something to start with


for number in noDuplicates:         #loops through the unique numbers
    if number != 0:                 #makes sure that we do not check 0
        count = original_list.count(number)     #checks how many times that unique number appears in the original list
        if count > most_common[1]   #if the count is greater than the most_common count
            most_common = [number, count] #resets most_common to the current number and count


print(str(most_common[0]) + " is listed " + str(most_common[1]) + "times!")

这个循环遍历您的列表并找到最常用的数字,并将其与原始列表中出现次数一起打印出来。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接