简体   繁体   English

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

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

I have a 2D numpy array as follows:我有一个二维 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)

I need to count how many values of 1 or 2 occur in column 2 for each value in column 1. For example when x=3 in column 1, there are two instances of the value 2 and one instance of the value 1 in column 2.我需要为第 1 列中的每个值计算第 2 列中出现多少个 1 或 2 的值。例如,当第 1 列中的 x=3 时,第 2 列中有两个值 2 的实例和一个值 1 的实例.

Any direction on how to complete this would be appreciated!任何有关如何完成此操作的指示将不胜感激! I think I could do some sort of for loop with np.unique but I am not sure...我想我可以用 np.unique 做某种 for 循环,但我不确定......

As in your comment, if you want list of lists format, try this:正如您的评论一样,如果您想要列表格式,请尝试以下操作:

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

For 2D-array对于二维阵列

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)

A simple way is using dict comprehension with collections.Counter and np.unique一个简单的方法是使用带有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})}

Assuming your values in the first column go from 1 to N and in the second column from 1 to M , this is one very simple and fast way to do that:假设您在第一列中的值从 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]}')

Output:输出:

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

This works simply by making an array c of zeros and then basically adding one to every row/column of c indicated by each row of a .这通过使阵列简单地工作c零,然后基本上增加一个到每行/列c指示由每行a Conceptually, it is equivalent to c[a[:, 0] - 1, a[:, 1] - 1] += 1 .从概念上讲,它等价于c[a[:, 0] - 1, a[:, 1] - 1] += 1 However, doing that will probably not work, because a contains repeated rows, so NumPy ends up counting only one of those.但是,这样做可能不起作用,因为a包含重复的行,因此 NumPy 最终只计算其中的一个。 To do that correctly, you need to use the at method of thenp.add ufunc (this method is available in other ufuncs too, see Universal functions (ufuncs) ).要正确执行此操作,您需要使用np.addat方法(此方法在其他 ufunc 中也可用,请参阅通用函数 (ufuncs) )。 This adds the given value at each position ( tuple(aT - 1) makes a tuple with the row indices and the column indices) counting repeated positions correctly.这会在每个位置添加给定值( tuple(aT - 1)一个带有行索引和列索引的元组),正确计算重复位置。

You can filter np array with the condition then use unique method to get count您可以使用条件过滤 np 数组,然后使用unique方法来获取计数

try below solution:尝试以下解决方案:

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}

Short solution:简短的解决方案:

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

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

output:输出:

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

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

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