访问 coo_matrix 中的元素
Accessing elements in coo_matrix
这是一个很简单的问题。对于像 coo_matrix 这样的 SciPy 稀疏矩阵,如何访问单个元素?
类比本征线性代数库。可以使用 coeffRef 访问元素 (i,j),如下所示:
myMatrix.coeffRef(i,j)
来自 coo_matrix 的文档:
| Intended Usage
| - COO is a fast format for constructing sparse matrices
| - Once a matrix has been constructed, convert to CSR or
| CSC format for fast arithmetic and matrix vector operations
| - By default when converting to CSR or CSC format, duplicate (i,j)
| entries will be summed together. This facilitates efficient
| construction of finite element matrices and the like. (see example)
事实上,csr_matrix
以预期的方式支持索引:
>>> from scipy.sparse import coo_matrix
>>> m = coo_matrix([[1, 2, 3], [4, 5, 6]])
>>> m1 = m.tocsr()
>>> m1[1, 2]
6
>>> m1
<2x3 sparse matrix of type '<type 'numpy.int64'>'
with 6 stored elements in Compressed Sparse Row format>
(我从文档中找到上述引用的方式是 >>> help(m)
,相当于 the online docs)。
为了扩展将 coo
矩阵转换为 csr
以进行索引,这里是小型稀疏矩阵的一些时间安排
制作矩阵
In [158]: M=sparse.coo_matrix([[0,1,2,0,0],[0,0,0,1,0],[0,1,0,0,0]])
In [159]: timeit M[1,2]
TypeError: 'coo_matrix' object is not subscriptable
In [160]: timeit M.tocsc()[1,2]
1000 loops, best of 3: 375 µs per loop
In [161]: timeit M.tocsr()[1,2]
1000 loops, best of 3: 241 µs per loop
In [162]: timeit M.todok()[1,2]
10000 loops, best of 3: 65.8 µs per loop
In [163]: timeit M.tolil()[1,2]
1000 loops, best of 3: 270 µs per loop
显然对于选择单个元素,dok
是最快的(计算转换时间)。这种格式其实就是一个字典,当然元素访问速度很快。
但是如果您经常访问整行或整列,或者遍历行或列,您需要更仔细地阅读文档,并且可能会对典型数组进行自己的时间测试。
如果您要设置值,而不仅仅是读取它们,那么时间安排甚至实施可能会有所不同。如果您尝试更改 csr
或 csc
格式的 0
元素,您将收到效率警告。
这是一个很简单的问题。对于像 coo_matrix 这样的 SciPy 稀疏矩阵,如何访问单个元素?
类比本征线性代数库。可以使用 coeffRef 访问元素 (i,j),如下所示:
myMatrix.coeffRef(i,j)
来自 coo_matrix 的文档:
| Intended Usage
| - COO is a fast format for constructing sparse matrices
| - Once a matrix has been constructed, convert to CSR or
| CSC format for fast arithmetic and matrix vector operations
| - By default when converting to CSR or CSC format, duplicate (i,j)
| entries will be summed together. This facilitates efficient
| construction of finite element matrices and the like. (see example)
事实上,csr_matrix
以预期的方式支持索引:
>>> from scipy.sparse import coo_matrix
>>> m = coo_matrix([[1, 2, 3], [4, 5, 6]])
>>> m1 = m.tocsr()
>>> m1[1, 2]
6
>>> m1
<2x3 sparse matrix of type '<type 'numpy.int64'>'
with 6 stored elements in Compressed Sparse Row format>
(我从文档中找到上述引用的方式是 >>> help(m)
,相当于 the online docs)。
为了扩展将 coo
矩阵转换为 csr
以进行索引,这里是小型稀疏矩阵的一些时间安排
制作矩阵
In [158]: M=sparse.coo_matrix([[0,1,2,0,0],[0,0,0,1,0],[0,1,0,0,0]])
In [159]: timeit M[1,2]
TypeError: 'coo_matrix' object is not subscriptable
In [160]: timeit M.tocsc()[1,2]
1000 loops, best of 3: 375 µs per loop
In [161]: timeit M.tocsr()[1,2]
1000 loops, best of 3: 241 µs per loop
In [162]: timeit M.todok()[1,2]
10000 loops, best of 3: 65.8 µs per loop
In [163]: timeit M.tolil()[1,2]
1000 loops, best of 3: 270 µs per loop
显然对于选择单个元素,dok
是最快的(计算转换时间)。这种格式其实就是一个字典,当然元素访问速度很快。
但是如果您经常访问整行或整列,或者遍历行或列,您需要更仔细地阅读文档,并且可能会对典型数组进行自己的时间测试。
如果您要设置值,而不仅仅是读取它们,那么时间安排甚至实施可能会有所不同。如果您尝试更改 csr
或 csc
格式的 0
元素,您将收到效率警告。