Get startedGet started for free

Compute adjacency matrix

Now, you'll get some practice using matrices and sparse matrix multiplication to compute projections! In this exercise, you'll use the matrix multiplication operator @ that was introduced in Python 3.5.

You'll continue working with the American Revolution graph. The two partitions of interest here are 'people' and 'clubs'.

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • Get the list of people and list of clubs from the graph G using the get_nodes_from_partition() function that you defined in the previous chapter. This function accepts two parameters: A graph, and a partition.
  • Compute the biadjacency matrix using nx.bipartite.biadjacency_matrix(), setting the row_order parameter to people_nodes and the column_order parameter to clubs_nodes. Remember to also pass in the graph G.
  • Compute the user-user projection by multiplying (with the @ operator) the biadjacency matrix bi_matrix by its transposition, bi_matrix.T.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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)
Edit and Run Code