繁体   English   中英

计算numpy 2D数组中的出现次数

[英]Counting number of occurrences in numpy 2D array

我有一个二维 numpy 数组,如下所示:

import numpy as np

a=np.array([[1,2],[1,1], [2,1],[2,2],[3,2],[3,2], [3,1], [4,2],[4,1]])
print(a)

我需要为第 1 列中的每个值计算第 2 列中出现多少个 1 或 2 的值。例如,当第 1 列中的 x=3 时,第 2 列中有两个值 2 的实例和一个值 1 的实例.

任何有关如何完成此操作的指示将不胜感激! 我想我可以用 np.unique 做某种 for 循环,但我不确定......

正如您的评论一样,如果您想要列表格式,请尝试以下操作:

out = [[k, *np.unique(a[a[:,0] == k,1], return_counts=True)[1]] 
                                              for k in np.unique(a[:,0])]

Out[838]: [[1, 1, 1], [2, 1, 1], [3, 1, 2], [4, 1, 1]]

对于二维阵列

out = np.array([[k, *np.unique(a[a[:,0] == k,1], return_counts=True)[1]] 
                                                 for k in np.unique(a[:,0])])

Out[850]:
array([[1, 1, 1],
       [2, 1, 1],
       [3, 1, 2],
       [4, 1, 1]], dtype=int64)

一个简单的方法是使用带有collections.Counternp.unique字典理解

from collections import Counter

out = {k: Counter(a[a[:,0] == k,1]) for k in np.unique(a[:,0])}

Out[821]:
{1: Counter({2: 1, 1: 1}),
 2: Counter({1: 1, 2: 1}),
 3: Counter({2: 2, 1: 1}),
 4: Counter({2: 1, 1: 1})}

假设您在第一列中的值从 1 到N ,在第二列中从 1 到M ,这是一种非常简单且快速的方法:

import numpy as np

a = np.array([[1, 2], [1, 1], [2, 1], [2, 2], [3, 2], [3, 2], [3, 1], [4, 2], [4, 1]])
c = np.zeros(a.max(0), np.int32)
np.add.at(c, tuple(a.T - 1), 1)
# c[i, j] contains the number of times
# the second column value is j + 1 when
# the first column value is i + 1

# Print result
for i in range(c.shape[0]):
    print(f'Count result for {i + 1}')
    for j in range(c.shape[1]):
        print(f'    Number of {j + 1}s: {c[i, j]}')

输出:

Count result for 1
    Number of 1s: 1
    Number of 2s: 1
Count result for 2
    Number of 1s: 1
    Number of 2s: 1
Count result for 3
    Number of 1s: 1
    Number of 2s: 2
Count result for 4
    Number of 1s: 1
    Number of 2s: 1

这通过使阵列简单地工作c零,然后基本上增加一个到每行/列c指示由每行a 从概念上讲,它等价于c[a[:, 0] - 1, a[:, 1] - 1] += 1 但是,这样做可能不起作用,因为a包含重复的行,因此 NumPy 最终只计算其中的一个。 要正确执行此操作,您需要使用np.addat方法(此方法在其他 ufunc 中也可用,请参阅通用函数 (ufuncs) )。 这会在每个位置添加给定值( tuple(aT - 1)一个带有行索引和列索引的元组),正确计算重复位置。

您可以使用条件过滤 np 数组,然后使用unique方法来获取计数

尝试以下解决方案:

import numpy as np

a = np.array(
    [[1, 2], [1, 1], [2, 1], [2, 2], [3, 2], [3, 2], [3, 1], [4, 2], [4, 1]])

b = a[np.any(a == 3, axis=1)]

print(len(b[np.any(b == 2, axis=1)])) #output: 2
print(len(b[np.any(b == 1, axis=1)])) #output: 1

unique, counts = np.unique(b, return_counts=True)

print(dict(zip(unique, counts))) #output: {1: 1, 2: 2, 3: 3}

简短的解决方案:

unique, counts = np.unique(a[np.any(a == 3, axis=1)], return_counts=True) #replace 3 with x

print(dict(zip(unique, counts)))

输出:

{1: 1, 2: 2, 3: 3}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM