这是一个非常简单的问题。对于像 coo_matrix 这样的 SciPy 稀疏矩阵,如何访问单个元素?
类比 Eigen 线性代数库。可以使用 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)
这相当于在线文档 http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.sparse.coo_matrix.html).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)