Python 访问 coo_matrix 中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30163830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:02:27  来源:igfitidea点击:

Accessing elements in coo_matrix

pythonscipy

提问by haripkannan

This is a very simple question. For SciPy sparse matrices like coo_matrix, how does one access individual elements?

这是一个非常简单的问题。对于像 coo_matrix 这样的 SciPy 稀疏矩阵,如何访问单个元素?

To give an analogy to Eigen linear algebra library. One can access element (i,j) using coeffRef as follows:

以 Eigen 线性代数库为例。可以使用 coeffRef 访问元素 (i,j),如下所示:

myMatrix.coeffRef(i,j)

采纳答案by ev-br

From the docs for coo_matrix:

来自 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)

And indeed, csr_matrixsupports the indexing in an expected way:

事实上,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>

(The way I found the above quote from the docs was >>> help(m)which is equivalent to the online docs).

(我从文档中找到上述引用的方式>>> help(m)相当于在线文档)。

回答by hpaulj

To expand on converting a coomatrix to csrto index, here are some timings for a small sparse matrix

为了扩展将coo矩阵转换csr为索引,这里有一些小的稀疏矩阵的时间

Make the matrix

制作矩阵

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

Apparently for selecting a single element, dok, is fastests (counting the conversion time). This format is actually a dictionary, which of course has fast element access.

显然,选择单个元素 ,dok是最快的(计算转换时间)。这种格式实际上是一个字典,当然它具有快速的元素访问。

But if you are frequently accessing whole rows, or whole columns, or iterating over rows or columns, you need to read the docs more carefully, and may be do your own time tests of typical arrays.

但是,如果您经常访问整行或整列,或者遍历行或列,则需要更仔细地阅读文档,并且可能会对典型数组进行自己的时间测试。

If you are setting values, not just reading them, timings and even implementation can be different. You will get an efficiency warning if you try to change a 0element of a csror cscformat.

如果您正在设置值,而不仅仅是读取它们,时间甚至实现可能会有所不同。如果您尝试更改0acsrcsc格式的元素,您将收到效率警告。