如何处理无唯一模式;在下面的函数中找到2个相同的通用值

时间:2018-10-19 04:33:26

标签: python mode

这是我用于查找模式的函数的代码段。

import statistics 
unique = numpy.unique(aggr_group)
x = numpy.zeros(shape=(unique.size))
for i in range(0, unique.size):
    x[i] = statistics.mode(val[aggr_group==unique[i]])

但是,有人可以让我知道如何处理“无唯一模式;找到2个相同的通用值”的情况吗?我尝试了多种选择,但是没有用。有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

以下代码按第1列对数据进行分组,并按照每组第0列查找 first 模式

import numpy as np
from collections import Counter


data = np.array([[115, 26925055],
        [115, 26925055],
        [115, 26925055],
        [115, 26925055],
        [114, 26925055],
        [115, 26925055],
        [115, 26925055],
        [114, 26925055],
        [115, 26925055],
        [84, 25471149],
        [111, 25471149],
        [84, 25471149],
        [84, 25471149],
        [84, 25471149],
        [84, 25471149],
        [111, 25471149],
        [111, 25471149],
        [111, 25471149],
        [84, 25471149]])
# ans = [[115, 26925055], [84, 25471149]]


def get_first_mode(a):
    c = Counter(a)  
    mode_count = max(c.values())
    mode = {key for key, count in c.items() if count == mode_count}
    first_mode = next(x for x in a if x in mode)
    return first_mode

col0 = data[:, 0]
col1 = data[:, 1]

ans = []
for v in np.unique(col1):
    mode = get_first_mode(col0[col1 == v])
    print((mode, v))
    ans.append((mode, v))

这是获取所有模式的另一个功能

def get_all_modes(a):
    c = Counter(a)  
    mode_count = max(c.values())
    mode = {key for key, count in c.items() if count == mode_count}
    return mode