计算数组中唯一数组的出现次数

Count occurrences of unique arrays in array

我有一个由各种热编码的 numpy 数组组成的 numpy 数组,例如;

x = np.array([[1, 0, 0], [0, 0, 1], [1, 0, 0]])

我想计算每个唯一的一个热向量的出现次数,

{[1, 0, 0]: 2, [0, 0, 1]: 1}

您可以将数组转换为元组并使用 Counter:

import numpy as np
from collections import Counter
x = np.array([[1, 0, 0], [0, 0, 1], [1, 0, 0]])
Counter([tuple(a) for a in x])
# Counter({(1, 0, 0): 2, (0, 0, 1): 1})

方法 #1

似乎是使用 numpy.unique(v1.13 和更新版本)的新功能的完美设置,它让我们可以沿着 NumPy 数组的轴工作 -

unq_rows, count = np.unique(x,axis=0, return_counts=1)
out = {tuple(i):j for i,j in zip(unq_rows,count)}

示例输出 -

In [289]: unq_rows
Out[289]: 
array([[0, 0, 1],
       [1, 0, 0]])

In [290]: count
Out[290]: array([1, 2])

In [291]: {tuple(i):j for i,j in zip(unq_rows,count)}
Out[291]: {(0, 0, 1): 1, (1, 0, 0): 2}

方法 #2

对于早于 v1.13 的 NumPy 版本,我们可以利用输入数组是单热编码数组的事实,就像这样 -

_, idx, count = np.unique(x.argmax(1), return_counts=1, return_index=1)
out = {tuple(i):j for i,j in zip(x[idx],count)} # x[idx] is unq_rows

列表(包括 numpy 数组)是不可散列的,即它们不能是字典的键。因此,在 Python 中永远不可能实现您想要的精确输出,即带有看起来像 [1, 0, 0] 的键的字典。为了解决这个问题,您需要将向量映射到元组。

from collections import Counter
import numpy as np

x = np.array([[1, 0, 0], [0, 0, 1], [1, 0, 0]])
counts = Counter(map(tuple, x))

这会让你:

In [12]: counts
Out[12]: Counter({(0, 0, 1): 1, (1, 0, 0): 2})

这是另一个有趣的解决方案,其中包含 sum

>> {tuple(v): n for v, n in zip(np.eye(x.shape[1], dtype=int), np.sum(x, axis=0)) 
                if n > 0}
{(0, 0, 1): 1, (1, 0, 0): 2}

给定数据格式的最快方法是:

x.sum(axis=0)

给出:

array([2, 0, 1])

第一个结果是数组的计数,其中第一个是热的:

[1, 0, 0] [2
[0, 1, 0]  0
[0, 0, 1]  1]

这利用了一次只能打开一个的事实,所以我们可以分解直和。

如果您确实需要将其扩展为相同的格式,可以通过以下方式进行转换:

sums = x.sum(axis=0)
{tuple(int(k == i) for k in range(len(sums))): e for i, e in enumerate(sums)}

或者,类似于 tarashypka:

{tuple(row): count for row, count in zip(np.eye(len(sums), dtype=np.int64), sums)}

产量:

{(1, 0, 0): 2, (0, 1, 0): 0, (0, 0, 1): 1}