Get Started

Reading graphs

In this exercise, before you compute projections, you're going to practice working with one of NetworkX's disk I/O functions, read_edgelist(). read_edgelist() creates a graph from the edgelist file. The graph that you'll be working with is a bipartite graph describing the American Revolution. There are two node partitions - 'people' and 'clubs', and edges denote a person being a member of a club.

This is a part of the course

“Intermediate Network Analysis in Python”

View Course

Exercise instructions

  • Import networkx as nx.
  • Use nx.read_edgelist() to read in 'american-revolution.edgelist'.
  • In the dataset, 'clubs' do not have a . symbol in their node name. Use this information to assign nodes to 'clubs' or 'people' partitions. Remember the 'bipartite' keyword!
  • Print the edges of the graph.

Hands-on interactive exercise

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

# Import networkx
import networkx as nx

# Read in the data: g
G = ____

# Assign nodes to 'clubs' or 'people' partitions
for n, d in G.nodes(data=True):
    if '.' in n:
        G.nodes[n]['____'] = '____'
    else:
        ____ = '____'
        
# Print the edges of the graph
print(____)
Edit and Run Code

This exercise is part of the course

Intermediate Network Analysis in Python

AdvancedSkill Level
5.0+
1 reviews

Analyze time series graphs, use bipartite graphs, and gain the skills to tackle advanced problems in network analytics.

In this chapter, you will use a famous American Revolution dataset to dive deeper into exploration of bipartite graphs. Here, you will learn how to create the unipartite projection of a bipartite graph, a very useful method for simplifying a complex network for further analysis. Additionally, you will learn how to use matrices to manipulate and analyze graphs - with many computing routines optimized for matrices, you'll be able to analyze many large graphs quickly and efficiently!

Exercise 1: Concept of projectionExercise 2: Reading graphs
Exercise 3: Computing projectionExercise 4: Plot degree centrality on projectionExercise 5: Bipartite graphs as matricesExercise 6: Properties of bipartite adjacency matrices.Exercise 7: Compute adjacency matrixExercise 8: Find shared membership: TranspositionExercise 9: Representing network data with pandasExercise 10: Make nodelistExercise 11: Make edgelist

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free