给定奇数、横、竖、斜、总和相等python_列中值相等的行的总和

这篇博客探讨了两种使用NumPy库进行数据聚合的方法。第一种利用`bincount`函数和`unique`组合实现,第二种通过`argsort`、`cumsum`和`diff`来完成。在基准测试中,第二种方法(cumsum+diff)在处理大型数组时表现出较好的性能,特别是在高维度数据上。
摘要由CSDN通过智能技术生成

接近#1

以下是基于^{}的核矢量化方法# Initial setup

N = A.shape[1]-1

unqA1, id = np.unique(A[:, 0], return_inverse=True)

# Create subscripts and accumulate with bincount for tagged summations

subs = np.arange(N)*(id.max()+1) + id[:,None]

sums = np.bincount( subs.ravel(), weights=A[:,1:].ravel() )

# Append the unique elements from first column to get final output

out = np.append(unqA1[:,None],sums.reshape(N,-1).T,1)

样本输入,输出-In [66]: A

Out[66]:

array([[1, 2, 3],

[1, 4, 6],

[2, 3, 5],

[2, 6, 2],

[7, 2, 1],

[2, 0, 3]])

In [67]: out

Out[67]:

array([[ 1., 6., 9.],

[ 2., 9., 10.],

[ 7., 2., 1.]])

接近#2

这是另一个基于^{}和^{}-# Sort A based on first column

sA = A[np.argsort(A[:,0]),:]

# Row mask of where each group ends

row_mask = np.append(np.diff(sA[:,0],axis=0)!=0,[True])

# Get cummulative summations and then DIFF to get summations for each group

cumsum_grps = sA.cumsum(0)[row_mask,1:]

sum_grps = np.diff(cumsum_grps,axis=0)

# Concatenate the first unique row with its counts

counts = np.concatenate((cumsum_grps[0,:][None],sum_grps),axis=0)

# Concatenate the first column of the input array for final output

out = np.concatenate((sA[row_mask,0][:,None],counts),axis=1)

基准测试

以下是目前为止针对这个问题提出的基于numpy的方法的一些运行时测试-In [319]: A = np.random.randint(0,1000,(100000,10))

In [320]: %timeit cumsum_diff(A)

100 loops, best of 3: 12.1 ms per loop

In [321]: %timeit bincount(A)

10 loops, best of 3: 21.4 ms per loop

In [322]: %timeit add_at(A)

10 loops, best of 3: 60.4 ms per loop

In [323]: A = np.random.randint(0,1000,(100000,20))

In [324]: %timeit cumsum_diff(A)

10 loops, best of 3: 32.1 ms per loop

In [325]: %timeit bincount(A)

10 loops, best of 3: 32.3 ms per loop

In [326]: %timeit add_at(A)

10 loops, best of 3: 113 ms per loop

似乎Approach #2: cumsum + diff表现相当不错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值