python中多维数组的计算_计算Python中多维数组中数组的出现

I have the following type of arrays:

a = array([[1,1,1],

[1,1,1],

[1,1,1],

[2,2,2],

[2,2,2],

[2,2,2],

[3,3,0],

[3,3,0],

[3,3,0]])

I would like to count the number of occurrences of each type of array such as

[1,1,1]:3, [2,2,2]:3, and [3,3,0]: 3

How could I achieve this in python? Is it possible without using a for loop and counting into a dictionary? It has to be fast and should take less than 0.1 seconds or so. I looked into Counter, numpy bincount, etc. But, those are for individual element not for an array.

Thanks.

解决方案

You could convert those rows to a 1D array using the elements as two-dimensional indices with np.ravel_multi_index. Then, use np.unique to give us the positions of the start of each unique row and also has an optional argument return_counts to give us the counts. Thus, the implementation would look something like this -

def unique_rows_counts(a):

# Calculate linear indices using rows from a

lidx = np.ravel_multi_index(a.T,a.max(0)+1 )

# Get the unique indices and their counts

_, unq_idx, counts = np.unique(lidx, return_index = True, return_counts=True)

# return the unique groups from a and their respective counts

return a[unq_idx], counts

Sample run -

In [64]: a

Out[64]:

array([[1, 1, 1],

[1, 1, 1],

[1, 1, 1],

[2, 2, 2],

[2, 2, 2],

[2, 2, 2],

[3, 3, 0],

[3, 3, 0],

[3, 3, 0]])

In [65]: unqrows, counts = unique_rows_counts(a)

In [66]: unqrows

Out[66]:

array([[1, 1, 1],

[2, 2, 2],

[3, 3, 0]])

In [67]: counts

Out[67]: array([3, 3, 3])

Benchmarking

Assuming you are okay with either numpy arrays or collections as outputs, one can benchmark the solutions provided thus far, like so -

Function definitions:

import numpy as np

from collections import Counter

def unique_rows_counts(a):

lidx = np.ravel_multi_index(a.T,a.max(0)+1 )

_, unq_idx, counts = np.unique(lidx, return_index = True, return_counts=True)

return a[unq_idx], counts

def map_Counter(a):

return Counter(map(tuple, a))

def forloop_Counter(a):

c = Counter()

for x in a:

c[tuple(x)] += 1

return c

Timings:

In [53]: a = np.random.randint(0,4,(10000,5))

In [54]: %timeit map_Counter(a)

10 loops, best of 3: 31.7 ms per loop

In [55]: %timeit forloop_Counter(a)

10 loops, best of 3: 45.4 ms per loop

In [56]: %timeit unique_rows_counts(a)

1000 loops, best of 3: 1.72 ms per loop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值