慢csr矩阵构造

时间:2015-07-09 15:58:18

标签: python numpy scipy

我正在使用csr_matrix((data, indices, indptr), shape=[row, col])方法创建一个csr矩阵。我花了超过4倍的时间来执行构造方法csr_matrix()而不是自己构建data, indices, indptr。既然我已经有了(data, indices, indptr)元组,那么构造一个csr矩阵不应该是繁琐(和快速)吗?

我的代码和时间统计信息是这样的:

data = ...  # 2.207s
indices = ...  # 11.065s       
indptr = ...  # 0.047s          
matrix = csr_matrix((data, indices, indptr), shape=(row, col))  # 57.806s

1 个答案:

答案 0 :(得分:3)

您传递的数组似乎很大,因此可能会将它们复制到某处,导致内存问题导致速度减慢。

有几种方法可以复制您的阵列。如果这些条件中的任何一个是错误的,您将会产生副本:

  • indicesindptr需要具有适当的索引dtype。
  • 这三个都需要是numpy数组(numpy.ndarray
  • copy kwarg需要False。默认情况下这是假的,所以这不太可能成为问题。
相关问题