我尝试使用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)
返回的是(array([2]), array([ 4.]))
,意思是出现最多的值是2,它出现了4次(更多信息请参见doc)。因此,如果您只想得到2,您只需要获取mode返回值的第一个索引: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!")
这个循环遍历您的列表并找到最常用的数字,并将其与原始列表中出现次数一起打印出来。
scipy.stats
时发生了什么?而且这不是有效的列表或 Python 数组!! - Mazdak{...}
是表示set
的符号,而不是list
,list
使用[...]
符号(Python 中没有array
)。set
中每个元素只会出现一次,因此它不可能有“第二常见的数字”。另外,你在 set 中的元素之间缺少逗号,所以你写的甚至不是有效的 Python 代码。 - ArtOfWarfarenumpy
数组,因为他们正在使用scipy.stats
。 - Lynn