Queries on a graph
Now that you know some basic properties of the graph and have practiced using NetworkX's drawing facilities to visualize components of it, it's time to explore how you can query it for nodes and edges. Specifically, you're going to look for "nodes of interest" and "edges of interest". To achieve this, you'll make use of the .nodes()
and .edges()
methods that Eric went over in the video. The .nodes()
method returns a Node view iterable, while the .edges()
method returns an edge view iterable, in which each tuple shows the nodes that are present on that edge. Recall that passing in the keyword argument data=True
in these methods retrieves the corresponding metadata associated with the nodes and edges as well.
You'll write list comprehensions to effectively build these queries in one line. For a refresher on list comprehensions, refer to Part 2 of DataCamp's Python Data Science Toolbox course. Here's the recipe for a list comprehension:
[
output expression for
iterator variable in
iterable if
predicate expression ]
.
You have to fill in the _iterable_
and the _predicate expression_
. Feel free to prototype your answer by exploring the graph in the IPython Shell before submitting your solution.
This is a part of the course
“Introduction to Network Analysis in Python”
Exercise instructions
- Use a list comprehension to get a list of nodes from the graph
T
that have the'occupation'
label of'scientist'
.- The output expression
n
has been specified for you, along with the iterator variablesn
andd
. Your task is to fill in the iterable and the conditional expression. - Use the
.nodes()
method ofT
access its nodes, and be sure to specifydata=True
to obtain the metadata for the nodes. - The iterator variable
d
is a dictionary. The key of interest here is'occupation'
and value of interest is'scientist'
.
- The output expression
- Use a list comprehension to get a list of edges from the graph
T
that were formed for at least 6 years, i.e., from before 1 Jan 2010.- Your task once again is to fill in the iterable and conditional expression.
- Use the
.edges()
method ofT
to access its edges. Be sure to obtain the metadata for the edges as well. - The dates are stored as
datetime.date
objects in the metadata dictionaryd
, under the key'date'
. To access the date 1 Jan 2009, for example, the dictionary value would bedate(2009, 1, 1)
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use a list comprehension to get the nodes of interest: noi
noi = [n for n, d in ____ if d['____'] == '____']
# Use a list comprehension to get the edges of interest: eoi
eoi = [(u, v) for u, v, d in ____ if ____ < ____]
This exercise is part of the course
Introduction to Network Analysis in Python
This course will equip you with the skills to analyze, visualize, and make sense of networks using the NetworkX library.
In this chapter, you'll be introduced to fundamental concepts in network analytics while exploring a real-world Twitter network dataset. You'll also learn about NetworkX, a library that allows you to manipulate, analyze, and model graph data. You'll learn about the different types of graphs and how to rationally visualize them.
Exercise 1: Introduction to NetworksExercise 2: What is a network?Exercise 3: Basics of NetworkX API, using Twitter networkExercise 4: Basic drawing of a network using NetworkXExercise 5: Queries on a graphExercise 6: Types of graphsExercise 7: Checking the un/directed status of a graphExercise 8: Specifying a weight on edgesExercise 9: Checking whether there are self-loops in the graphExercise 10: Network visualizationExercise 11: Visualizing using Matrix plotsExercise 12: Visualizing using Circos plotsExercise 13: Visualizing using Arc plotsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.