简体   繁体   English

Python在数组中找到最常见的值

[英]Python find most common value in array

import numpy as np 
x = ([1,2,3,3])
y = ([1,2,3])
z = ([6,6,1,2,9,9])

(only positive values) In each array i need to return the most common value, or, if values come up the same amount of times - return the minimum. (仅正值)在每个数组中,我需要返回最常见的值,或者,如果值出现相同的次数-返回最小值。 This is home assignment and I can't use anything but numpy. 这是家庭作业,除numpy外我什么也不能使用。

outputs: 输出:

f(x) = 3,
f(y) = 1,
f(z) = 6

for a numpy exclusive solution something like this will work: 对于一个numpy的独家解决方案,这样的事情将工作:

occurances = np.bincount(x)
print (np.argmax(occurances))

The above mentioned method won't work if there is a negative number in the list. 如果列表中为负数,则上述方法将无效。 So in order to account for such an occurrence kindly use: 因此,为了解决这种情况,请使用:

not_required, counts = np.unique(x, return_counts=True)
x=np.array(x)
if (x >= 0).all():
    print(not_required[np.argmax(counts)])
else:    
    print(not_required[np.argmax(counts)])

Without numpy 没有numpy

n_dict = {}
for k in x:
    try:
        n_dict[k] += 1
    except KeyError:
        n_dict[k] = 1

rev_n_dict = {}
for k in n_dict:
    if n_dict[k] not in rev_n_dict:
        rev_n_dict[n_dict[k]] = [k]
    else:
        rev_n_dict[n_dict[k]].append(k)

local_max = 0
for k in rev_n_dict:
    if k > local_max:
        local_max = k

if len(rev_n_dict[local_max]) > 0:      
    print (min(rev_n_dict[local_max]))
else:
    print (rev_n_dict[local_max])

To add to the previous results, you could use a collections.Counter object: 要添加到先前的结果中,可以使用collections.Counter对象:

 my_array =  [3,24,543,3,1,6,7,8,....,223213,13213]
 from collections import Counter
 my_counter = Counter( my_array)
 most_common_value = my_counter.most_common(1)[0][0]

It is quite simple but certainly not pretty. 这很简单,但肯定不是很漂亮。 I have used variable names that will be self explanatory along with the comments. 我使用的变量名将随注释一起自我解释。 Feel free to ask if there is a doubt. 随时问是否有疑问。

import numpy as np
x=([6,6,1,2,9,9])

def tester(x): 
    not_required, counts = np.unique(x, return_counts=True)
    x=np.array(x)
    if (x >= 0).all():
        highest_occurance=[not_required[np.argmax(counts)]]
        number_of_counts=np.max(counts)

    else:    
        highest_occurance=not_required[np.argmax(counts)]
        number_of_counts=np.max(counts)

    return highest_occurance,number_of_counts

most_abundant,first_test_counts=(tester(x))

new_x=[vals for vals in x if vals not in most_abundant]

second_most_abundant,second_test_counts=(tester(new_x))

if second_test_counts==first_test_counts:
    print("Atleast two elements have the same number of counts",most_abundant," and", second_most_abundant, "have %s"%first_test_counts,"occurances")
else:
    print("%s occurrs for the max of %s times"%(most_abundant,first_test_counts))

we can also loop it to check if there are more than two elements with the same occurrence, instead of using an if else for a specific case of only looking at two elements 我们也可以循环检查是否有两个以上的元素同时出现,而不是在只查看两个元素的特定情况下使用if来代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从圈子数组中找到最常见的圈子python opencv - Find the most common circle from an array of circles python opencv 使用python在CSV文件的列中查找最常用的值 - Using python to find the most common value(s) in the column of CSV file 在 numpy 3d 数组中找到最常见的值(不包括 -1),其中另一个数组中的对应值为 1 - Find most common value(exclude -1) in numpy 3d array where the corrsponding value in another array is 1 在python词典列表中找到最常用的单词 - Find the most common words in list of dictionaries in python 在python中找到第n个最常见的单词并计数 - Find the nth most common word and count in python Python - 查找图像中的主要/最常见颜色 - Python - Find dominant/most common color in an image 如何根据 python 中的另一个列值从列中找到最常见的值? - How can I find the most common value from a column based on another Columns value in python? 返回矩阵/数组的最常见值(模式) - Return most common value (mode) of a matrix / array 在 numpy 二维数组行中查找最常见的值,否则返回最大值 - Find most common value in numpy 2d array rows, otherwise return maximum 在 Python 中的 N 维元组数组中查找最常见的元组第一个和第二个值的最快方法 - Fastest approach to finding the most common first and second value of tuples in an N-dimensional array of tuples in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM