开始使用免费开始使用

计算邻接矩阵

现在,您将练习使用矩阵和稀疏矩阵乘法来计算投影!在本练习中,您将使用 Python 3.5 引入的矩阵乘法运算符 @

您将继续使用美国独立战争的数据图。此处关注的两个分区是 'people''clubs'

本练习是课程的一部分

Python 网络分析中级

查看课程

练习说明

  • 使用您在上一章定义的 get_nodes_from_partition() 函数,从图 G 中获取人物列表和俱乐部列表。该函数接收两个参数:图和分区。
  • 使用 nx.bipartite.biadjacency_matrix() 计算双部邻接矩阵,将参数 row_order 设为 people_nodescolumn_order 设为 clubs_nodes。别忘了同时传入图 G
  • 通过将双部邻接矩阵 bi_matrix 与其转置 bi_matrix.T 使用 @ 运算相乘,计算用户-用户投影。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Get the list of people and list of clubs from the graph: people_nodes, clubs_nodes
people_nodes = ____
clubs_nodes = ____

# Compute the biadjacency matrix: bi_matrix
bi_matrix = ____(____, row_order=____, column_order=____)

# Compute the user-user projection: user_matrix
user_matrix = ____

print(user_matrix)
编辑并运行代码