且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算每行在 numpy.array 中出现的次数

更新时间:2023-08-28 16:01:52

您可以使用 您的另一个问题的答案 以获取唯一项目的数量.

在 numpy 1.9 中有一个 return_counts 可选关键字参数,所以你可以简单地做:

>>>my_array数组([[1, 2, 0, 1, 1, 1],[1, 2, 0, 1, 1, 1],[9, 7, 5, 3, 2, 1],[1, 1, 1, 0, 0, 0],[1, 2, 0, 1, 1, 1],[1, 1, 1, 1, 1, 0]])>>>dt = np.dtype((np.void, my_array.dtype.itemsize * my_array.shape[1]))>>>b = np.ascontiguousarray(my_array).view(dt)>>>unq, cnt = np.unique(b, return_counts=True)>>>unq = unq.view(my_array.dtype).reshape(-1, my_array.shape[1])>>>不知道数组([[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 1, 0],[1, 2, 0, 1, 1, 1],[9, 7, 5, 3, 2, 1]])>>>cnt数组([1, 1, 3, 1])

在早期版本中,您可以这样做:

>>>unq, _ = np.unique(b, return_inverse=True)>>>cnt = np.bincount(_)>>>unq = unq.view(my_array.dtype).reshape(-1, my_array.shape[1])>>>不知道数组([[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 1, 0],[1, 2, 0, 1, 1, 1],[9, 7, 5, 3, 2, 1]])>>>cnt数组([1, 1, 3, 1])

I am trying to count a number each row shows in a np.array, for example:

import numpy as np
my_array = np.array([[1, 2, 0, 1, 1, 1],
                     [1, 2, 0, 1, 1, 1], # duplicate of row 0
                     [9, 7, 5, 3, 2, 1],
                     [1, 1, 1, 0, 0, 0], 
                     [1, 2, 0, 1, 1, 1], # duplicate of row 0
                     [1, 1, 1, 1, 1, 0]])

Row [1, 2, 0, 1, 1, 1] shows up 3 times.

A simple naive solution would involve converting all my rows to tuples, and applying collections.Counter, like this:

from collections import Counter
def row_counter(my_array):
    list_of_tups = [tuple(ele) for ele in my_array]
    return Counter(list_of_tups)

Which yields:

In [2]: row_counter(my_array)
Out[2]: Counter({(1, 2, 0, 1, 1, 1): 3, (1, 1, 1, 1, 1, 0): 1, (9, 7, 5, 3, 2, 1): 1, (1, 1, 1, 0, 0, 0): 1})

However, I am concerned about the efficiency of my approach. And maybe there is a library that provides a built-in way of doing this. I tagged the question as pandas because I think that pandas might have the tool I am looking for.

You can use the answer to this other question of yours to get the counts of the unique items.

In numpy 1.9 there is a return_counts optional keyword argument, so you can simply do:

>>> my_array
array([[1, 2, 0, 1, 1, 1],
       [1, 2, 0, 1, 1, 1],
       [9, 7, 5, 3, 2, 1],
       [1, 1, 1, 0, 0, 0],
       [1, 2, 0, 1, 1, 1],
       [1, 1, 1, 1, 1, 0]])
>>> dt = np.dtype((np.void, my_array.dtype.itemsize * my_array.shape[1]))
>>> b = np.ascontiguousarray(my_array).view(dt)
>>> unq, cnt = np.unique(b, return_counts=True)
>>> unq = unq.view(my_array.dtype).reshape(-1, my_array.shape[1])
>>> unq
array([[1, 1, 1, 0, 0, 0],
       [1, 1, 1, 1, 1, 0],
       [1, 2, 0, 1, 1, 1],
       [9, 7, 5, 3, 2, 1]])
>>> cnt
array([1, 1, 3, 1])

In earlier versions, you can do it as:

>>> unq, _ = np.unique(b, return_inverse=True)
>>> cnt = np.bincount(_)
>>> unq = unq.view(my_array.dtype).reshape(-1, my_array.shape[1])
>>> unq
array([[1, 1, 1, 0, 0, 0],
       [1, 1, 1, 1, 1, 0],
       [1, 2, 0, 1, 1, 1],
       [9, 7, 5, 3, 2, 1]])
>>> cnt
array([1, 1, 3, 1])