Chapter 22. Network Analysis

Your connections to all the things around you literally define who you are.

Aaron O’Connell

Many interesting data problems can be fruitfully thought of in terms of networks, consisting of nodes of some type and the edges that join them.

For instance, your Facebook friends form the nodes of a network whose edges are friendship relations. A less obvious example is the World Wide Web itself, with each web page a node and each hyperlink from one page to another an edge.

Facebook friendship is mutual—if I am Facebook friends with you, then necessarily you are friends with me. In this case, we say that the edges are undirected. Hyperlinks are not—my website links to whitehouse.gov, but (for reasons inexplicable to me) whitehouse.gov refuses to link to my website. We call these types of edges directed. We’ll look at both kinds of networks.

Betweenness Centrality

In Chapter 1, we computed the key connectors in the DataSciencester network by counting the number of friends each user had. Now we have enough machinery to take a look at other approaches. We will use the same network, but now we’ll use `NamedTuple`s for the data.

Recall that the network (Figure 22-1) comprised users:

````from` `typing` `import` `NamedTuple`

`class` `User``(``NamedTuple``):`
`id``:` `int`
`name``:` `str`

`users` `=` `[``User``(``0``,` `"Hero"``),` `User``(``1``,` `"Dunn"``),` `User``(``2``,` `"Sue"``),` `User``(``3``,` `"Chi"``),`
`User``(``4``,` `"Thor"``),` `User``(``5``,` `"Clive"``),` `User``(``6``,` `"Hicks"``),`
`User``(``7``,` `"Devin"``),` `User``(``8``,` `"Kate"``),` `User``(``9``,` `"Klein"``)]````

and friendships:

``friend_pairs ...``

Get Data Science from Scratch, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.