scipy csr_matrix:了解indptr [英] scipy csr_matrix: understand indptr

查看:315
本文介绍了scipy csr_matrix:了解indptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶尔会操纵csr_matrix,但是我总是忘记参数indicesindptr是如何一起构建稀疏矩阵的.

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)])的稀疏矩阵时indptrdataindices参数如何交互的清晰,直观的解释.

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中的每个元素映射到稀疏矩阵中其列的数组.
  • 然后,
  • indptrdataindices的元素映射到稀疏矩阵的行.这是通过以下推理完成的:

  • data is an array containing all the non zero elements of the sparse matrix.
  • indices is an array mapping each element in data to its column in the sparse matrix.
  • indptr then maps the elements of data and indices to the rows of the sparse matrix. This is done with the following reasoning:

  1. 如果稀疏矩阵具有 M 行,则indptr是包含 M + 1 个元素
  2. 的数组
  3. 对于 i 行,[indptr[i]:indptr[i+1]]返回与 i 行相对应的dataindices所取元素的索引.因此,假设indptr[i]=kindptr[i+1]=l,与 i 行相对应的数据将在indices[k:l]列中为data[k:l].这是棘手的部分,希望下面的示例有助于理解它.
  1. If the sparse matrix has M rows, indptr is an array containing M+1 elements
  2. for row i, [indptr[i]:indptr[i+1]] returns the indices of elements to take from data and indices corresponding to row i. So suppose indptr[i]=k and indptr[i+1]=l, the data corresponding to row i would be data[k:l] at columns indices[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中的下一个单元格(下一行)是指dataindices中与该行相对应的下一个值.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆