gpt4 book ai didi

python - 访问 coo_matrix 中的元素

转载 作者:太空狗 更新时间:2023-10-29 19:31:28 26 4
gpt4 key购买 nike

这是一个非常简单的问题。对于像 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 )。

关于python - 访问 coo_matrix 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com