问题描述
我偶尔会操纵csr_matrix
,但是我总是忘记参数indices
和indptr
是如何一起构建稀疏矩阵的.
Every once in a while, I get to manipulate a csr_matrix
but I always forget how the parameters indices
and indptr
work together to build a sparse matrix.
在寻找使用符号csr_matrix((data, indices, indptr), [shape=(M, N)])
的稀疏矩阵时indptr
与data
和indices
参数如何交互的清晰,直观的解释.
I am looking for a clear and intuitive explanation on how the indptr
interacts with both the data
and indices
parameters when defining a sparse matrix using the notation csr_matrix((data, indices, indptr), [shape=(M, N)])
.
我可以从 scipy中看到文档,其中data
参数包含所有非零数据,并且indices
参数包含与该数据关联的列(因此,在示例中给出的indices
等于col
文档).但是我们如何清楚地解释indptr
参数?
I can see from the scipy documentation that the data
parameter contains all the non-zero data, and the indices
parameter contains the columns associated to that data (as such, indices
is equal to col
in the example given in the documentation). But how can we explain in clear terms the indptr
parameter?
推荐答案
也许这个解释可以帮助理解这个概念:
Maybe this explanation can help understand the concept:
-
data
是一个包含稀疏矩阵的所有非零元素的数组. -
indices
是一个将data
中的每个元素映射到稀疏矩阵中其列的数组. 然后, -
indptr
将data
和indices
的元素映射到稀疏矩阵的行.这是通过以下推理完成的:
data
is an array containing all the non zero elements of the sparse matrix.indices
is an array mapping each element indata
to its column in the sparse matrix.indptr
then maps the elements ofdata
andindices
to the rows of the sparse matrix. This is done with the following reasoning:
- 如果稀疏矩阵具有 M 行,则
indptr
是包含 M + 1 个元素 的数组 - 对于 i 行,
[indptr[i]:indptr[i+1]]
返回与 i 行相对应的data
和indices
所取元素的索引.因此,假设indptr[i]=k
和indptr[i+1]=l
,与 i 行相对应的数据将在indices[k:l]
列中为data[k:l]
.这是棘手的部分,希望下面的示例有助于理解它.
- If the sparse matrix has M rows,
indptr
is an array containing M+1 elements - for row i,
[indptr[i]:indptr[i+1]]
returns the indices of elements to take fromdata
andindices
corresponding to row i. So supposeindptr[i]=k
andindptr[i+1]=l
, the data corresponding to row i would bedata[k:l]
at columnsindices[k:l]
. This is the tricky part, and I hope the following example helps understanding it.
编辑:在下面的示例中,为了避免混淆,我用字母替换了data
中的数字.
EDIT : I replaced the numbers in data
by letters to avoid confusion in the following example.
注意:indptr
中的值必然会增加,因为indptr
中的下一个单元格(下一行)是指data
和indices
中与该行相对应的下一个值.
Note: the values in indptr
are necessarily increasing, because the next cell in indptr
(the next row) is referring to the next values in data
and indices
corresponding to that row.
这篇关于scipy csr_matrix:了解indptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!