gpt4 book ai didi

python - Scipy 稀疏 Cumsum

转载 作者:太空狗 更新时间:2023-10-29 18:12:41 37 4
gpt4 key购买 nike

假设我有一个 scipy.sparse.csr_matrix 代表下面的值

[[0 0 1 2 0 3 0 4]
[1 0 0 2 0 3 4 0]]

我想就地计算非零值的累积和,这会将数组更改为:

[[0 0 1 3 0 6 0 10]
[1 0 0 3 0 6 10 0]]

实际值不是 1, 2, 3, ...

每行中非零值的数量不太可能相同。

如何快速做到这一点?

当前程序:

import scipy.sparse
import numpy as np

# sparse data
a = scipy.sparse.csr_matrix(
[[0,0,1,2,0,3,0,4],
[1,0,0,2,0,3,4,0]],
dtype=int)

# method
indptr = a.indptr
data = a.data
for i in range(a.shape[0]):
st = indptr[i]
en = indptr[i + 1]
np.cumsum(data[st:en], out=data[st:en])

# print result
print(a.todense())

结果:

[[ 0  0  1  3  0  6  0 10]
[ 1 0 0 3 0 6 10 0]]

最佳答案

改用这个怎么样

a = np.array([[0,0,1,2,0,3,0,4],
[1,0,0,2,0,3,4,0]], dtype=int)

b = a.copy()
b[b > 0] = 1
z = np.cumsum(a,axis=1)
print(z*b)

产量

array([[ 0,  0,  1,  3,  0,  6,  0, 10],
[ 1, 0, 0, 3, 0, 6, 10, 0]])

做稀疏

def sparse(a):
a = scipy.sparse.csr_matrix(a)

indptr = a.indptr
data = a.data
for i in range(a.shape[0]):
st = indptr[i]
en = indptr[i + 1]
np.cumsum(data[st:en], out=data[st:en])


In[1]: %timeit sparse(a)
10000 loops, best of 3: 167 µs per loop

使用乘法

def mult(a):
b = a.copy()
b[b > 0] = 1
z = np.cumsum(a, axis=1)
z * b

In[2]: %timeit mult(a)
100000 loops, best of 3: 5.93 µs per loop

关于python - Scipy 稀疏 Cumsum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45492626/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com