计算邻接矩阵
现在,您将练习使用矩阵和稀疏矩阵乘法来计算投影!在本练习中,您将使用 Python 3.5 引入的矩阵乘法运算符 @。
您将继续使用美国独立战争的数据图。此处关注的两个分区是 'people' 和 'clubs'。
本练习是课程的一部分
Python 网络分析中级
练习说明
- 使用您在上一章定义的
get_nodes_from_partition()函数,从图G中获取人物列表和俱乐部列表。该函数接收两个参数:图和分区。 - 使用
nx.bipartite.biadjacency_matrix()计算双部邻接矩阵,将参数row_order设为people_nodes,column_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)