如何解释networkx.adjacency_matrix中的indptr?
How to interpret the indptr in networkx.adjacency_matrix?
我在 python
中使用 networkx
。我测试了函数 adjacency_matrix
但我无法理解某些结果。例如:
import networkx as nx
import numpy as np
g = nx.Graph()
g.add_nodes_from([1,2,3])
g.add_edges_from([[1,2],[2,3],[1,3]])
adj = nx.adjacency_matrix(g)
print adj.todense()
# [[0 1 1]
# [1 0 1]
# [1 1 0]]
print adj.__dict__
# {'indices': array([1, 2, 0, 2, 0, 1]), 'indptr': array([0, 2, 4, 6]), 'maxprint'
# : 50, '_shape': (3, 3), 'data': array([1, 1, 1, 1, 1, 1])}
在print adj.__dict__
的结果中,indices
和indptr
代表什么?我认为它们是用于重建邻接矩阵的关键信息,连同 data
属性。但是我想不通它是如何实现的。
谢谢大家对我的帮助!
adj
由 nx.adjacency_matrix
给出,是邻接矩阵的 compressed sparse row 矩阵格式。
一种压缩格式,
represents a matrix M by three (one-dimensional) arrays, that respectively contain [the] nonzero values, the extents of rows, and column indices.
在您的例子中,adj.indices
是一个一维 (numpy) 数组,其索引是邻接矩阵的非零值。与 adj.indptr
一起,您可以知道矩阵中非零值的确切位置。根据定义 adj.indptr[0] == 0
和 adj.indptr[i] == adj.indptr[i-1] + number of nonzero values at row i
。 data
属性指的是非零值,在您的情况下它们都是 1。
我在 python
中使用 networkx
。我测试了函数 adjacency_matrix
但我无法理解某些结果。例如:
import networkx as nx
import numpy as np
g = nx.Graph()
g.add_nodes_from([1,2,3])
g.add_edges_from([[1,2],[2,3],[1,3]])
adj = nx.adjacency_matrix(g)
print adj.todense()
# [[0 1 1]
# [1 0 1]
# [1 1 0]]
print adj.__dict__
# {'indices': array([1, 2, 0, 2, 0, 1]), 'indptr': array([0, 2, 4, 6]), 'maxprint'
# : 50, '_shape': (3, 3), 'data': array([1, 1, 1, 1, 1, 1])}
在print adj.__dict__
的结果中,indices
和indptr
代表什么?我认为它们是用于重建邻接矩阵的关键信息,连同 data
属性。但是我想不通它是如何实现的。
谢谢大家对我的帮助!
adj
由 nx.adjacency_matrix
给出,是邻接矩阵的 compressed sparse row 矩阵格式。
一种压缩格式,
represents a matrix M by three (one-dimensional) arrays, that respectively contain [the] nonzero values, the extents of rows, and column indices.
在您的例子中,adj.indices
是一个一维 (numpy) 数组,其索引是邻接矩阵的非零值。与 adj.indptr
一起,您可以知道矩阵中非零值的确切位置。根据定义 adj.indptr[0] == 0
和 adj.indptr[i] == adj.indptr[i-1] + number of nonzero values at row i
。 data
属性指的是非零值,在您的情况下它们都是 1。