二分图的矩阵表示

Python 网络分析中级

Eric Ma

Data Carpentry instructor and author of nxviz package

矩阵表示

  • 行:一侧分区的节点
  • 列:另一侧分区的节点
  • 单元:有边为 1,否则为 0
Python 网络分析中级

矩阵表示

ch2-2.006.png

Python 网络分析中级

示例代码

cust_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'customers']
prod_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'products']

mat = nx.bipartite.biadjacency_matrix(G, row_order=cust_nodes, column_order=prod_nodes)
mat
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in Compressed Sparse Row format>
Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.014.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.015.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.016.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.017.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.018.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.019.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.020.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.021.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.022.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.023.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.024.png

Python 网络分析中级

矩阵投影

  • 可用矩阵乘法计算投影

ch2-2.025.png

Python 网络分析中级

Python 中的矩阵乘法

mat @ mat.T
<5x5 sparse matrix of type '<class 'numpy.int64'>'
 with 23 stored elements in Compressed Sparse Row format>
mat.T @ mat
<10x10 sparse matrix of type '<class 'numpy.int64'>'
 with 50 stored elements in Compressed Sparse Column format>
Python 网络分析中级

开始练习吧!

Python 网络分析中级

Preparing Video For Download...