|  | 
|  | 1 | +{ | 
|  | 2 | + "cells": [ | 
|  | 3 | +  { | 
|  | 4 | +   "cell_type": "markdown", | 
|  | 5 | +   "metadata": {}, | 
|  | 6 | +   "source": [ | 
|  | 7 | +    "# Brief introduction to NetworkX\n", | 
|  | 8 | +    "\n", | 
|  | 9 | +    "Network Analysis is a broad, emerging field across many disciplines. It focuses on the role of relationships (\"edges\") between entities (\"nodes\"). Nodes could be people or neurons or power plants or genes. Edges might include friendships or connections or co-presence at events.\n", | 
|  | 10 | +    "\n", | 
|  | 11 | +    "Here, I show a very brief introduction to how to use the [networkx](https://networkx.github.io/) package to do basic network analysis." | 
|  | 12 | +   ] | 
|  | 13 | +  }, | 
|  | 14 | +  { | 
|  | 15 | +   "cell_type": "code", | 
|  | 16 | +   "execution_count": null, | 
|  | 17 | +   "metadata": {}, | 
|  | 18 | +   "outputs": [], | 
|  | 19 | +   "source": [ | 
|  | 20 | +    "import networkx as nx\n", | 
|  | 21 | +    "import matplotlib.pyplot as plt\n", | 
|  | 22 | +    "import pandas as pd\n", | 
|  | 23 | +    "import numpy as np" | 
|  | 24 | +   ] | 
|  | 25 | +  }, | 
|  | 26 | +  { | 
|  | 27 | +   "cell_type": "markdown", | 
|  | 28 | +   "metadata": {}, | 
|  | 29 | +   "source": [ | 
|  | 30 | +    "## Creating a graph object\n", | 
|  | 31 | +    "\n", | 
|  | 32 | +    "`networkx` has a number of built-in functions that let you create random networks. Here I show a Barabasi-Albert graph, which is highly skewed with a few central nodes." | 
|  | 33 | +   ] | 
|  | 34 | +  }, | 
|  | 35 | +  { | 
|  | 36 | +   "cell_type": "code", | 
|  | 37 | +   "execution_count": null, | 
|  | 38 | +   "metadata": {}, | 
|  | 39 | +   "outputs": [], | 
|  | 40 | +   "source": [ | 
|  | 41 | +    "G = nx.barabasi_albert_graph(100,2)" | 
|  | 42 | +   ] | 
|  | 43 | +  }, | 
|  | 44 | +  { | 
|  | 45 | +   "cell_type": "markdown", | 
|  | 46 | +   "metadata": {}, | 
|  | 47 | +   "source": [ | 
|  | 48 | +    "It's often useful to visualize these networks. `networkx` includes a number of visualization options. Here's a simple one." | 
|  | 49 | +   ] | 
|  | 50 | +  }, | 
|  | 51 | +  { | 
|  | 52 | +   "cell_type": "code", | 
|  | 53 | +   "execution_count": null, | 
|  | 54 | +   "metadata": {}, | 
|  | 55 | +   "outputs": [], | 
|  | 56 | +   "source": [ | 
|  | 57 | +    "nx.draw_spring(G);" | 
|  | 58 | +   ] | 
|  | 59 | +  }, | 
|  | 60 | +  { | 
|  | 61 | +   "cell_type": "markdown", | 
|  | 62 | +   "metadata": {}, | 
|  | 63 | +   "source": [ | 
|  | 64 | +    "We also often want to get information about the network. For example, here is the degree distribution (a histogram of how many edges each node has)." | 
|  | 65 | +   ] | 
|  | 66 | +  }, | 
|  | 67 | +  { | 
|  | 68 | +   "cell_type": "code", | 
|  | 69 | +   "execution_count": null, | 
|  | 70 | +   "metadata": {}, | 
|  | 71 | +   "outputs": [], | 
|  | 72 | +   "source": [ | 
|  | 73 | +    "plt.hist([v for k,v in nx.degree(G)]);" | 
|  | 74 | +   ] | 
|  | 75 | +  }, | 
|  | 76 | +  { | 
|  | 77 | +   "cell_type": "markdown", | 
|  | 78 | +   "metadata": {}, | 
|  | 79 | +   "source": [ | 
|  | 80 | +    "And here is the betweenness centrality. Other measures have a similar syntax." | 
|  | 81 | +   ] | 
|  | 82 | +  }, | 
|  | 83 | +  { | 
|  | 84 | +   "cell_type": "code", | 
|  | 85 | +   "execution_count": null, | 
|  | 86 | +   "metadata": {}, | 
|  | 87 | +   "outputs": [], | 
|  | 88 | +   "source": [ | 
|  | 89 | +    "plt.hist(nx.centrality.closeness_centrality(G).values());" | 
|  | 90 | +   ] | 
|  | 91 | +  }, | 
|  | 92 | +  { | 
|  | 93 | +   "cell_type": "markdown", | 
|  | 94 | +   "metadata": {}, | 
|  | 95 | +   "source": [ | 
|  | 96 | +    "There are also network-level measures." | 
|  | 97 | +   ] | 
|  | 98 | +  }, | 
|  | 99 | +  { | 
|  | 100 | +   "cell_type": "code", | 
|  | 101 | +   "execution_count": null, | 
|  | 102 | +   "metadata": {}, | 
|  | 103 | +   "outputs": [], | 
|  | 104 | +   "source": [ | 
|  | 105 | +    "nx.diameter(G)" | 
|  | 106 | +   ] | 
|  | 107 | +  }, | 
|  | 108 | +  { | 
|  | 109 | +   "cell_type": "code", | 
|  | 110 | +   "execution_count": null, | 
|  | 111 | +   "metadata": {}, | 
|  | 112 | +   "outputs": [], | 
|  | 113 | +   "source": [ | 
|  | 114 | +    "nx.cluster.average_clustering(G)" | 
|  | 115 | +   ] | 
|  | 116 | +  }, | 
|  | 117 | +  { | 
|  | 118 | +   "cell_type": "markdown", | 
|  | 119 | +   "metadata": {}, | 
|  | 120 | +   "source": [ | 
|  | 121 | +    "## Importing from Pandas\n", | 
|  | 122 | +    "\n", | 
|  | 123 | +    "You likely want to create a network from data. In order to create a graph object, you need to convert the data into what's called an \"edgelist\". The easiest way to do this is with a data frame which has at least two columns.\n", | 
|  | 124 | +    "\n", | 
|  | 125 | +    "The first is the source node, the second is the target node, and after that are any attributes of the edges (e.g., weight or valence). Here's a simple example" | 
|  | 126 | +   ] | 
|  | 127 | +  }, | 
|  | 128 | +  { | 
|  | 129 | +   "cell_type": "code", | 
|  | 130 | +   "execution_count": null, | 
|  | 131 | +   "metadata": {}, | 
|  | 132 | +   "outputs": [], | 
|  | 133 | +   "source": [ | 
|  | 134 | +    "nodes = list(range(100))\n", | 
|  | 135 | +    "\n", | 
|  | 136 | +    "df = pd.DataFrame({'from': np.random.choice(nodes, 100),\n", | 
|  | 137 | +    "                   'to': np.random.choice(nodes,100)\n", | 
|  | 138 | +    "                  })" | 
|  | 139 | +   ] | 
|  | 140 | +  }, | 
|  | 141 | +  { | 
|  | 142 | +   "cell_type": "code", | 
|  | 143 | +   "execution_count": null, | 
|  | 144 | +   "metadata": {}, | 
|  | 145 | +   "outputs": [], | 
|  | 146 | +   "source": [ | 
|  | 147 | +    "df" | 
|  | 148 | +   ] | 
|  | 149 | +  }, | 
|  | 150 | +  { | 
|  | 151 | +   "cell_type": "code", | 
|  | 152 | +   "execution_count": null, | 
|  | 153 | +   "metadata": {}, | 
|  | 154 | +   "outputs": [], | 
|  | 155 | +   "source": [ | 
|  | 156 | +    "G = nx.from_pandas_edgelist(df, source='from', target='to')" | 
|  | 157 | +   ] | 
|  | 158 | +  }, | 
|  | 159 | +  { | 
|  | 160 | +   "cell_type": "code", | 
|  | 161 | +   "execution_count": null, | 
|  | 162 | +   "metadata": {}, | 
|  | 163 | +   "outputs": [], | 
|  | 164 | +   "source": [ | 
|  | 165 | +    "nx.draw(G);" | 
|  | 166 | +   ] | 
|  | 167 | +  }, | 
|  | 168 | +  { | 
|  | 169 | +   "cell_type": "code", | 
|  | 170 | +   "execution_count": null, | 
|  | 171 | +   "metadata": {}, | 
|  | 172 | +   "outputs": [], | 
|  | 173 | +   "source": [ | 
|  | 174 | +    "plt.hist([v for k,v in nx.degree(G)]);" | 
|  | 175 | +   ] | 
|  | 176 | +  } | 
|  | 177 | + ], | 
|  | 178 | + "metadata": { | 
|  | 179 | +  "kernelspec": { | 
|  | 180 | +   "display_name": "Python 3", | 
|  | 181 | +   "language": "python", | 
|  | 182 | +   "name": "python3" | 
|  | 183 | +  }, | 
|  | 184 | +  "language_info": { | 
|  | 185 | +   "codemirror_mode": { | 
|  | 186 | +    "name": "ipython", | 
|  | 187 | +    "version": 3 | 
|  | 188 | +   }, | 
|  | 189 | +   "file_extension": ".py", | 
|  | 190 | +   "mimetype": "text/x-python", | 
|  | 191 | +   "name": "python", | 
|  | 192 | +   "nbconvert_exporter": "python", | 
|  | 193 | +   "pygments_lexer": "ipython3", | 
|  | 194 | +   "version": "3.7.4" | 
|  | 195 | +  } | 
|  | 196 | + }, | 
|  | 197 | + "nbformat": 4, | 
|  | 198 | + "nbformat_minor": 2 | 
|  | 199 | +} | 
0 commit comments