计算每行在 numpy.array 中出现的次数 [英] Count how many times each row is present in numpy.array

查看:36
本文介绍了计算每行在 numpy.array 中出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算 np.array 中每行显示的数字,例如:

将 numpy 导入为 npmy_array = np.array([[1, 2, 0, 1, 1, 1],[1, 2, 0, 1, 1, 1], #第0行的重复[9, 7, 5, 3, 2, 1],[1, 1, 1, 0, 0, 0],[1, 2, 0, 1, 1, 1], #第0行的重复[1, 1, 1, 1, 1, 0]])

[1, 2, 0, 1, 1, 1] 出现了 3 次.

一个简单的天真的解决方案是将我的所有行转换为元组,并应用 collections.Counter,如下所示:

from collections import Counterdef row_counter(my_array):list_of_tups = [元组(ele) for ele in my_array]返回计数器(list_of_tups)

产生的结果:

在[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})

但是,我担心我的方法的效率.也许有一个库提供了一种内置的方式来做到这一点.我将问题标记为 pandas 因为我认为 pandas 可能有我正在寻找的工具.

解决方案

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

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

这篇关于计算每行在 numpy.array 中出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆