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

How can I find the second most common number in an array?

我尝试使用 scipy.stats 模式来查找最常见的值。不过,我的矩阵包含很多零,所以这始终是众数。

例如,如果我的矩阵如下所示:

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

我想要返回 2 的值。

尝试 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]

正如一些评论中提到的,您可能在谈论 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)

其中returns (array([2]), array([ 4.]))表示出现次数最多的是2,出现了4次(详见doc)。所以如果你只想得到2,你只需要得到模式的return值的第一个索引:mode(flat_non_zero)[0][0]

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

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!")

这会遍历您的列表并找到最常用的数字,并打印出它在原始列表中出现的次数。