【发布时间】:2015-04-18 07:31:57
【问题描述】:
我有一个 3 维数组 (np.ndarray),其中大部分为 0。现在我想在第一个维度上对它们求和,但这相当慢。我研究了 csr_matrix,但 csr 不支持 3 维数组。有没有一种更快的方法来对几乎稀疏的 nd 数组求和?以下是我当前代码的摘录。
相关问题: sparse 3d matrix/array in Python?(创建一个自制的稀疏 ndarray 类,矫枉过正?)
r = np.array([ [[1, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0]],
[[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 2, 0]],
[[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]]], dtype=int)
np.sum(r,axis=0)
Out[35]:
array([[1, 2, 0, 1],
[1, 0, 0, 1],
[0, 0, 3, 0]])
编辑
在下面 hpaulj 的回答之后,我做了更多的计时测试,见下文。似乎重塑对总和没有多大好处,而将它们转换为 csr_matrix 并返回 numpy 会杀死性能。我仍在考虑直接使用索引(下面称为rand_persons、rand_articles 和rand_days,因为在我最初的问题中,我也使用这些索引制作了大 ndarray。
from timeit import timeit
from scipy.sparse import csr_matrix
import numpy as np
def create_test_data():
'''
dtype = int64
1% nonzero, 1000x1000x100: 1.3 s,
1% nonzero, 10000x1000x100: 13.3 s
0.1% nonzero, 10000x1000x100: 2.7 s
1ppm nonzero, 10000x1000x100: 0.007 s
'''
global purchases
N_persons = 10000
N_articles = 1000
N_days = 100
purchases = np.zeros(shape=(N_days, N_persons, N_articles), dtype=int)
N_elements = N_persons * N_articles * N_days
rand_persons = np.random.choice(a=range(N_persons), size=N_elements / 1e6, replace=True)
rand_articles = np.random.choice(a=range(N_articles), size=N_elements / 1e6, replace=True)
rand_days = np.random.choice(a=range(N_days), size=N_elements / 1e6, replace=True)
for (i, j, k) in zip(rand_persons, rand_articles, rand_days):
purchases[k, i, j] += 1
def sum_over_first_dim_A():
'''
0.1% nonzero, 10000x1000x99: 1.57s (average over 10)
1ppm nonzero, 10000x1000x99: 1.70s (average over 10)
'''
global purchases
d = purchases[:99, :, :]
np.sum(d, axis=0)
def sum_over_first_dim_B():
'''
0.1% nonzero, 10000x1000x99: 1.55s (average over 10)
1ppm nonzero, 10000x1000x99: 1.37s (average over 10)
'''
global purchases
d = purchases[:99, :, :]
(N_days, N_persons, N_articles) = d.shape
d.reshape(N_days, -1).sum(0).reshape(N_persons, N_articles)
def sum_over_first_dim_C():
'''
0.1% nonzero, 10000x1000x99: 7.54s (average over 10)
1ppm nonzero, 10000x1000x99: 7.44s (average over 10)
'''
global purchases
d = purchases[:99, :, :]
(N_days, N_persons, N_articles) = d.shape
r = csr_matrix(d.reshape(N_days, -1))
t = r.sum(axis=0)
np.reshape(t, newshape=(N_persons, N_articles))
if __name__ == '__main__':
print (timeit(create_test_data, number=10))
print (timeit(sum_over_first_dim_A, number=10))
print (timeit(sum_over_first_dim_B, number=10))
print (timeit(sum_over_first_dim_C, number=10))
编辑 2
我现在找到了一种更快的求和方法:我用稀疏矩阵创建了一个 numpy 数组。但是,这些矩阵的初始创建还有一段时间。我现在用一个循环来做这个。有没有办法加快速度?
def create_test_data():
[ ... ]
'''
0.1% nonzero, 10000x1000x100: 2.1 s
1ppm nonzero, 10000x1000x100: 0.45 s
'''
global sp_purchases
sp_purchases = np.empty(N_days, dtype=lil_matrix)
for i in range(N_days):
sp_purchases[i] = lil_matrix((N_persons, N_articles))
for (i, j, k) in zip(rand_persons, rand_articles, rand_days):
sp_purchases[k][i, j] += 1
def sum_over_first_dim_D():
'''
0.1% nonzero, 10000x1000x99: 0.47s (average over 10)
1ppm nonzero, 10000x1000x99: 0.41s (average over 10)
'''
global sp_purchases
d = sp_purchases[:99]
np.sum(d)
【问题讨论】:
-
Numpy masked arrays 可能会有所帮助,尽管它们不是存储稀疏矩阵的有效方法,因此取决于数组的大小和稀疏程度,它可能没有用。数据的起始格式是什么?如果它已经在 ndarray 中,那么我认为将其转换为稀疏格式并求和可能不会快得多。
-
原始的起始数据结构是三个 (i,j,k) 索引数组和一个等长的值数组。我从中制作了一个 numpy ndarray,速度很快,因为它是稀疏的(1,000,000 个元素中的约 1 个非零)。
标签: python arrays numpy multidimensional-array sparse-matrix