2014-11-03 28 views
0

我必须使用相当于sicpy的sparse.coo_matrix和sparse.csr_matrix在矩阵上操作。但是,我不能使用scipy(它与我想使用它的图像分析软件不兼容)。但是,我可以使用numpy。 有没有简单的方法来完成什么scipy.sparse.coo_matrix和scipy.sparse.csr_matrix做,只有numpy? 谢谢!是否有可能使用scipy建立coo和csr矩阵而不使用scipy?

回答

3

一个sparse.coo_matrix的属性是:

dtype : dtype 
    Data type of the matrix 
shape : 2-tuple 
    Shape of the matrix 
ndim : int 
    Number of dimensions (this is always 2) 
nnz 
    Number of nonzero elements 
data 
    COO format data array of the matrix 
row 
    COO format row index array of the matrix 
col 
    COO format column index array of the matrix 

datarowcol数组实质上是datai,当与coo_matrix((data, (i, j)), [shape=(M, N)])j定义参数。 shape也来自定义。 dtypedata阵列。 nzz,因为第一个近似值是data(不包含零和重复坐标)的长度。

因此很容易构建一个coo类似的对象。同样,一个lil矩阵有2个列表。并且dok矩阵是字典(参见其.__class__.__mro__)。

一个csr矩阵的数据结构是一个比较晦涩:

data 
    CSR format data array of the matrix 
indices 
    CSR format index array of the matrix 
indptr 
    CSR format index pointer array of the matrix 

它仍然有3个阵列。它们可以来自coo阵列。但是,使用纯Python代码这样做不会像编译的scipy函数那么快。

但是,这些类有很多功能,需要大量的工作才能复制。有些是纯Python,但关键部分是为了速度而编译的。特别重要的是csr_matrix实现的数学运算,如矩阵乘法。

复制临时存储的数据结构是一回事;复制功能是另一回事。