Skip to content
Course Notes: Introduction to Network Analysis in Python
  • AI Chat
  • Code
  • Report
  • Course Notes

    Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! The datasets used in this course are available in the datasets folder.

    # Import any packages you want to use here

    Take Notes

    Add notes here about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    # Add your code snippets here

    What is the size of the graph T, the type of T.nodes(), and the data structure of the third element of the last edge listed in T.edges(data=True)? The len() and type() functions will be useful here. To access the last entry of T.edges(data=True), you can use list(T.edges(data=True))[-1].

    # Import necessary modules
    import matplotlib.pyplot as plt
    import networkx as nx
    # Draw the graph to screen

    # Use a list comprehension to get the nodes of interest: noi
    noi = [n for n, d in T.nodes(data=True) if d['occupation'] == 'scientist']
    # Use a list comprehension to get the edges of interest: eoi
    eoi = [(u, v) for u, v, d in T.edges(data=True) if d['date'] < date(2010, 1, 1)]

    network_name.edges[node1, node2]['attribute'] = value. Here, the 'attribute' is 'weight'

    # Set the weight of the edge
    T.edges[1, 10]['weight'] = 2
    # Iterate over all the edges (with metadata)
    for u, v, d in T.edges(data=True):
        # Check if node 293 is involved
        if 293 in [u, v]:
            # Set the weight to 1.1
            T.edges[u, v]['weight'] = 1.1
    # Define find_selfloop_nodes()
    def find_selfloop_nodes(G):
        Finds all nodes that have self-loops in the graph G.
        nodes_in_selfloops = []
        # Iterate over all the edges of G
        for u, v in G.edges(data=False):
        # Check if node u and node v are the same
            if u == v:
                # Append node u to nodes_in_selfloops
        return nodes_in_selfloops
    # Check whether number of self loops equals the number of nodes in self loops
    assert T.number_of_selfloops() == len(find_selfloop_nodes(T))

    Visualizing using Matrix plots It is time to try your first "fancy" graph visualization method: a matrix plot. To do this, nxviz provides a MatrixPlot object.

    nxviz is a package for visualizing graphs in a rational fashion. Under the hood, the MatrixPlot utilizes nx.to_numpy_matrix(G), which returns the matrix form of the graph. Here, each node is one column and one row, and an edge between the two nodes is indicated by the value 1. In doing so, however, only the weight metadata is preserved; all other metadata is lost, as you'll verify using an assert statement.

    A corresponding nx.from_numpy_matrix(A) allows one to quickly create a graph from a NumPy matrix. The default graph type is Graph(); if you want to make it a DiGraph(), that has to be specified using the create_using keyword argument, e.g. (nx.from_numpy_matrix(A, create_using=nx.DiGraph)).

    One final note, matplotlib.pyplot and networkx have already been imported as plt and nx, respectively, and the graph T has been pre-loaded. For simplicity and speed, we have sub-sampled only 100 edges from the network.

    # Import nxviz
    import nxviz as nv
    # Create the MatrixPlot object: m
    m = nv.MatrixPlot(T)
    # Draw m to the screen
    # Display the plot
    # Convert T to a matrix format: A
    A = nx.to_numpy_matrix(T)
    # Convert A back to the NetworkX form as a directed graph: T_conv
    T_conv = nx.from_numpy_matrix(A, create_using=nx.DiGraph())
    # Check that the `category` metadata field is lost from each node
    for n, d in T_conv.nodes(data=True):
        assert 'category' not in d.keys()