1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-18 04:22:28 +00:00
sitc/sna/3_Network_Analysis.ipynb

2231 lines
828 KiB
Plaintext
Raw Normal View History

2024-04-17 15:23:28 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "skip"
},
"tags": []
},
"source": [
"![](images/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © Carlos A. Iglesias"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## [Introduction to Network Analysis](0_Intro_Network_Analysis.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Network Analysis"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Case study: Zackary's karate club"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [],
"source": [
"import networkx as nx\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "skip"
},
"tags": []
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.simplefilter(action='ignore', category=FutureWarning)\n",
"warnings.simplefilter(action='ignore', category=DeprecationWarning)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [],
"source": [
"KCG = nx.karate_club_graph()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [],
"source": [
"pos = nx.spring_layout(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAADbOUlEQVR4nOyddViUSxuH711aEUQEJVRMFFTswMYWO0BsPcejx2PHp2J3d3cHdnd3d8s5InaggnTsfn8srCzskguCzn1dXuq+8847u+LOb5555vdI5HK5HIFAIBAIBL8t0p89AIFAIBAIBD8XIQYEAoFAIPjNEWJAIBAIBILfHCEGBAKBQCD4zRFiQCAQCASC3xwhBgQCgUAg+M0RYkAgEAgEgt8c3aQ0kslkvH37lmzZsiGRSNJ6TAKBQCAQCLSAXC7n+/fvWFtbI5VqXv8nSQy8ffuWPHnyaG1wAoFAIBAI0o9Xr15ha2ur8XqSxEC2bNmUnZmYmGhnZAKBQCAQCNKUgIAA8uTJo5zHNZEkMRCzNWBiYiLEgEAgEAgEmYzEtvhFAqFAIBAIBL85QgwIBAKBQPCbI8SAQCAQCAS/OUIMCAQCgUDwmyPEgEAgEAgEvzlCDAgEAoFA8JsjxIBAIBAIBL85QgwIBAKBQPCbI8SAQCAQCAS/OUIMCAQCgUDwmyPEgEAgEAgEvzlCDAgEAoFA8JsjxIBAIBAIBL85QgwIBAKBQPCbI8SAQCAQCAS/OUIMCAQCgUDwm6P7swcgSCeioiAkDGQykErByAB0dH72qAQCgUCQARBi4FcmKATefQI/fwgNi3/d0ADMTcHKArIapf/4BAKBQJAhEGLgVyQkDJ77wNfvCbcLDYM3HxW/zLJBYTtFxEAgEAgEvxUiZ+BX490nuPEgcSEQl6/fFfe9+5Q24xIIBAJBhkVEBn4lXr4Fn7cpv18mh2cv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V81EyumTFsvXbtWrp27crw4cOZPHmydju/9zRJWxXJxiybIvIhEAgEggRJ6vwtxEBySCTsjqGBInxtZZEys56fgEwmw9jYGCMjIz5//oxEokW/gpAwuPEgZcmMmpBKoFxxkTwoEAgESSCp87c4m5UUQsIUq9wbDxWhb02JcaFhius3HiraZwJTHKlUioeHB1++fOHo0aPa7Tyj+R8IBAKBQC0iMpAY7z79/KN6acyHDx/InTs3ZcqU4ebNm9p/wMu32vM/yGeV+n4EAoHgN0FEBrTBy7fw7GXKw9wyueL+l1qYCNOQXLlyUbp0aW7dusX79+8TvyG55LOGIvlSbpkslSjuF0JAIBAI0gQhBjTx7pN2VrOg6CdOSd+MxqRJkwAYPnx42jzAykKx12+WLXn3mWVT3JfBoysCgUCQmRHbBOqITnwLDAxihtcGrj56wLUnj/j6PYA1Q0fTpWGTeLdsO32c2ds288TXBx0dHYrnL8D/2nbCtXJVRYMMnvgml8sxNzcnNDSUgIAAdHXT8NRprERMeUho/KTFTJiIKRAIBBkRsU2QGp77gEzOZ/9vjF+3kse+PjgVLKyx+YJdXriP8ySnaXam/tWbUR274R8YSOPhA9h17pSikUyu6DeDIpFI6NGjByEhIaxduzZtHxbjf1CxBHOun6Vqv7+o+HcXrkZ+h6qloWIJxXUhBAQCgSBdEGIgLkEhyrPxVuY5ebfzMC+99jOjZ1+NtyzYtY3yRR3YP2U2PZu1on+bdpybvxxjoyysO3rwR8Ov3xX9Z1A8PT2RSqXa9xtIgHcfP/DG/yvXHj/ELywkU3o1CAQCQWZHiIG4xNrbN9DXJ7d5zkRvCQgKxDJ7DpVwt0lWY4yNjDDSj7MtkIFzB7Jly0aNGjV48eIF9+/fT5dn+vn5kSNHDgDCwjL+UUyBQCD4FRFiIC5+/sm+pWapshy
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nx.draw_networkx(KCG, pos, node_color='pink')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"NodeDataView({0: {'club': 'Mr. Hi'}, 1: {'club': 'Mr. Hi'}, 2: {'club': 'Mr. Hi'}, 3: {'club': 'Mr. Hi'}, 4: {'club': 'Mr. Hi'}, 5: {'club': 'Mr. Hi'}, 6: {'club': 'Mr. Hi'}, 7: {'club': 'Mr. Hi'}, 8: {'club': 'Mr. Hi'}, 9: {'club': 'Officer'}, 10: {'club': 'Mr. Hi'}, 11: {'club': 'Mr. Hi'}, 12: {'club': 'Mr. Hi'}, 13: {'club': 'Mr. Hi'}, 14: {'club': 'Officer'}, 15: {'club': 'Officer'}, 16: {'club': 'Mr. Hi'}, 17: {'club': 'Mr. Hi'}, 18: {'club': 'Officer'}, 19: {'club': 'Mr. Hi'}, 20: {'club': 'Officer'}, 21: {'club': 'Mr. Hi'}, 22: {'club': 'Officer'}, 23: {'club': 'Officer'}, 24: {'club': 'Officer'}, 25: {'club': 'Officer'}, 26: {'club': 'Officer'}, 27: {'club': 'Officer'}, 28: {'club': 'Officer'}, 29: {'club': 'Officer'}, 30: {'club': 'Officer'}, 31: {'club': 'Officer'}, 32: {'club': 'Officer'}, 33: {'club': 'Officer'}})"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"KCG.nodes(data=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"mr_hi_nodes = [x for x,y in KCG.nodes(data=True) if y['club']=='Mr. Hi']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 16, 17, 19, 21]\n"
]
}
],
"source": [
"print(mr_hi_nodes)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type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n/Isn21Epk22FPVANFTmhxYBGo9HERGznb70yECNHUJ7r36KEACgRENlb3hz37gf8BhREiYe4h9i9SYxGI61ateLZs2dsjT4tYTyojkqKZAvsUFsfv6GFgEaj0dgWLQaiZR5QFpXtLvbpfpWDmz9q37wpib3C3rhx4wC1ZWB7phDZdyDu2AOpgd2Ae4It0mg0Gk1EtBiwykLUJCbEJ2Y+jHWo/fOE9PF6yZAhA8WLF+fkyZM8ePDAxr0bUU/zk1BbKfHxWS0DHEPlL9BoNBqNrdFiwCIXga9t1JcJWAv8ZKP+Xg9jx44FYMiQIa+hdwOqoNIZVHIkiD5TovlcemAmqkJhjtdgl0aj0WhAiwELhABtAeHlSxgxAmrVgjRpVDnd+fMtX7ViBZQpA6lSgZsbVKwIGzeGb+EBXHrNtsefWrVqkTp1apYvX05wcPBrGuUjlFPhFZQPxqcEB4dlOxQxoJwuW6DyI9wBuqN/TTUajeb1ov/LRmE9cBwI5skTGDUKLl6EokWtXzFjhsqnnzYtTJgAw4eDlxfUqxcWR6+2G0a9duvji8FgoHPnzvj5+THfmuKxGXlQ4ZkHmDJlJNmypSBtWtizZyPwH7AYaEzC0yJrNBqNJjZoMRCFGZiXqTNlgvv3VaKcSZOiuWIGlCypMup16QJ9+sC+fZA8Ofz5p7lVMLASePRarU8IHh4eGI3GUIfCN8GTJ8+ws3Pj6VPw84uLk6ZGo9FobIUWAxF4hPJYV85+Tk6QMWPMV3l7Q/r0ahvBTMqUSgwki1BcLwS1/J04SZEiBRUrVuT69eucPXv2jYz59OlT0qRJA0BAQOKOutBoNJp3FS0GInA8XldVqgRbtqgVghs34NIl6N5dbRX07h2
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colors = ['yellow' if not node in mr_hi_nodes else 'pink' for node in KCG.nodes()]\n",
"nx.draw_networkx(KCG, pos, node_color=colors)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Case study: directed graph"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"D = nx.DiGraph()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"D.add_edges_from([('A', 'B'), ('C','A')])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"dpos = nx.layout.spring_layout(D)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAow0lEQVR4nO3deXSU9b3H8c+ECSTsueFKGYgQTCYbYbk0oHA0CYReg1exiB6WU8UKFVALKFhRzgXaArVIi1oBleJyr8gVxaJHobJkoAIFiqzZJkHAQEBKTMmODJn7R4wGyJ6ZPLO8X+dwBOaZZ775wzNv5vk9vzE5nU6nAACAXwswegAAAGA8ggAAABAEAACAIAAAACIIAACACAIAACCCAAAASDI35qDKykrl5+erU6dOMplM7p4JAAC4gNPpVHFxsSwWiwIC6v8MoFFBkJ+fr7CwMJcMBwAAWldeXp569epV7zGNCoJOnTp9f8LOnTu3fDIAAOB2RUVFCgsL+/59vD6NCoLqywSdO3cmCAAA8DKNudzPokIAAEAQAAAAggAAAIggAAAAIggAAIAIAgAAIIIAAACIIAAAACIIAACACAIAACCCAAAAiCAAAAAiCAAAgAgCAAAgggAAAIggAAAAksxGD+BKJQ6HcsvLddnpVDuTSRHBwepo9qkfEQAAt/D6d8uM0lKtzs/XpwUF+r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KAMBABAEMVR0F//znP4kCADAQQQDDRUdHy2azEQUAYCCCAB6hZhSwpgAAWh9BAI9RffngwoULRAEAtDKCAB4lJiaGKAAAAxAE8DjXR8HFixeNHgkAfB5BAI8UExOjHTt
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nx.draw_networkx(D, dpos, node_color='c')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Case study: weighted graph"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W.add_weighted_edges_from([('@user1', '@user2', 5),('@user1', '@user3', 2), ('@user1', '@user4', 4),('@user3', '@user4', 2)]) "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"wpos = nx.spring_layout(W) # positions for all nodes"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{('@user1', '@user2'): Text(0.6018955503921715, -0.24679947126373863, '5'),\n",
" ('@user1', '@user3'): Text(-0.17970137346946247, 0.29333141640278315, '2'),\n",
" ('@user1', '@user4'): Text(-0.21840580053176595, -0.1501865110092797, '4'),\n",
" ('@user3', '@user4'): Text(-0.6018896056692925, 0.24679395449426322, '2')}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACX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Xl8ejjz7K8uXLT40e5OUtw+3eMkS97WD06AeG6LNFRET6LqymDM7U1tZGSUkJDQ0NZGVlWdbtBwIBPvzwOtzuvwC+kPXNZoslJeWLFBW9qQ2GRERkSETslMGZ4uPjKSoqOus1NpuNyZPXs2vXFPz+0AUCu93J5MnrFAZERCQshM0+BBfC6cxj0qTnQvqZEyc+i9PZj1OVREREhlBUBAKA7Owl5OevDMln5fNNsmNVOigiIuEjagIBQEHBavLzi8994QXI55sUcC98WgZNzYP6WSIiIgMlqgKBzWajsHA1U6a8jN2eyMAtoYjBTgJTWEWh7T6zbsDvh48PQKuOMhYRkeEvqgJBl+zsJcyatZfU1C90fiXmPN/JvC414TPM4jWybQt7Nrd74eP94BuKPRBERET6LioDAZiFhkVFW5gxYzNpafMAG+YGf66qgO7r0tLmMWPGZoou/wvO3OnWlze3wKcHu89NFhERGYbCuuzwQtlsNtLT55OePp+WljKOHn0VT9X/h8f7Pj56Hz/scGSQlDQLl+tKsrJu77kD4cSx0NoGbk/vD6pvhNLDMGHsIH4
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"labels = nx.get_edge_attributes(W,'weight')\n",
"nx.draw_networkx(W, wpos, node_color='y', node_size=600)\n",
"nx.draw_networkx_edges(W,wpos,width=4, edge_color='pink', arrows=False)\n",
"nx.draw_networkx_edge_labels(W,wpos,edge_labels=labels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"We will use a helper function to draw the measures"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib.colors as mcolors\n",
"\n",
"# for Notebook\n",
"%matplotlib inline\n",
"\n",
"# Adapted from https://aksakalli.github.io/2017/07/17/network-centrality-measures-and-their-visualization.html\n",
"def draw(G, pos, measures, measure_name):\n",
" nodes = nx.draw_networkx_nodes(G, pos, node_size=250, cmap=plt.cm.YlOrRd, \n",
" node_color=list(measures.values()),\n",
" nodelist=measures.keys())\n",
" nodes.set_norm(mcolors.SymLogNorm(linthresh=0.01, linscale=1))\n",
" labels = nx.draw_networkx_labels(G, pos)\n",
" edges = nx.draw_networkx_edges(G, pos)\n",
" plt.title(measure_name)\n",
" plt.colorbar(nodes)\n",
" plt.axis('off')\n",
" plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Micro level analysis"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Degree centrality"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Undirected Graphs\n",
"\n",
"Calculate degree"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 16), (1, 9), (2, 10), (3, 6), (4, 3), (5, 4), (6, 4), (7, 4), (8, 5), (9, 2), (10, 3), (11, 1), (12, 2), (13, 5), (14, 2), (15, 2), (16, 2), (17, 2), (18, 2), (19, 3), (20, 2), (21, 2), (22, 2), (23, 5), (24, 3), (25, 3), (26, 2), (27, 4), (28, 3), (29, 4), (30, 4), (31, 6), (32, 12), (33, 17)]\n"
]
}
],
"source": [
"print(KCG.degree())"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
}
],
"source": [
"print(KCG.degree(1))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Normalized centrality"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(33, 0.5151515151515151),\n",
" (0, 0.48484848484848486),\n",
" (32, 0.36363636363636365),\n",
" (2, 0.30303030303030304),\n",
" (1, 0.2727272727272727),\n",
" (3, 0.18181818181818182),\n",
" (31, 0.18181818181818182),\n",
" (8, 0.15151515151515152),\n",
" (13, 0.15151515151515152),\n",
" (23, 0.15151515151515152)]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"degree = nx.degree_centrality(KCG)\n",
"sorted(degree.items(), key=lambda x:x[1], reverse=True)[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfYAAAGbCAYAAAA2gbI4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAD3gUlEQVR4nOyddXhUx/eH37ubbFxJQkiw4O4W3N2tWJECpWjRtrSlBnXa4vZDi7QQrHix4BTXAoXgFieuuzu/PzZZWDYJMSzfeZ9nH5I7c0duQj53zpw5RxFCCCQSiUQikeQJVK97ABKJRCKRSHIPKewSiUQikeQhpLBLJBKJRJKHkMIukUgkEkkeQgq7RCKRSCR5CCnsEolEIpHkIaSwSyQSiUSSh5DCLpFIJBJJHkIKu0QikUgkeQgp7BLJ/zB37txBURSWL19uvPbVV1+hKMrrG5REIskRUtglL2T58uUoimL8WFtb4+XlRatWrZg1axbR0dGve4h5mjVr1jBjxozXOobvvvuOzZs3v9YxSCSSzCGFXZJpvvnmG1auXMn8+fMZPXo0AGPHjqVixYpcvHjxNY8u7/Kqhf3zzz8nPj7e5JoUdonk7cHidQ9A8vbQpk0batSoYfx+8uTJ7N+/n/bt29OxY0euXr2KjY3NKxuPEIKEhIRX2uebTkJCAhqNBpUq++/sFhYWWFjIPw0SyduKXLFLckTTp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GwsbGRgBmR98SExOFi4uLcHJyEvHx8S98jhK
"text/plain": [
"<Figure size 640x480 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"draw(KCG, pos, nx.degree_centrality(KCG), 'Degree centrality')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Directed grahps"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 2), ('B', 1), ('C', 1)]\n"
]
}
],
"source": [
"print(D.degree())"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 1), ('B', 1), ('C', 0)]\n"
]
}
],
"source": [
"print(D.in_degree)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 1), ('B', 0), ('C', 1)]\n"
]
}
],
"source": [
"print(D.out_degree)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Weighted graphs"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('@user1', 3), ('@user2', 1), ('@user3', 2), ('@user4', 2)]\n"
]
}
],
"source": [
"print(W.degree)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('@user1', 11), ('@user2', 5), ('@user3', 4), ('@user4', 6)]\n"
]
}
],
"source": [
"print(W.degree(weight='weight')) #name of relationship with numeric weight"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(W.degree('@user2', weight='weight'))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Betweenness centrality"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 231.0714285714286),\n",
" (33, 160.5515873015873),\n",
" (32, 76.69047619047619),\n",
" (2, 75.85079365079366),\n",
" (31, 73.00952380952378),\n",
" (8, 29.529365079365082),\n",
" (1, 28.478571428571424),\n",
" (13, 24.215873015873015),\n",
" (19, 17.1468253968254),\n",
" (5, 15.833333333333334)]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"betweenness = nx.betweenness_centrality(KCG, normalized=False)\n",
"sorted(betweenness.items(), key=lambda x:x[1], reverse=True)[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfYAAAGkCAYAAADDrQdAAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAD/PElEQVR4nOyddXhURxeH37ubbNwgAkGDS3F3d9fgrkVL+ShVKEVKkUJxh5aU4l5cGqBI8ba4QyBGXDe7e78/Nlmy7AaiENJ5n+c+sHfmjmwCvztnzjkjybIsIxAIBAKBIEegeN8DEAgEAoFAkHkIYRcIBAKBIAchhF0gEAgEghyEEHaBQCAQCHIQQtgFAoFAIMhBCGEXCAQCgSAHIYRdIBAIBIIchBB2gUAgEAhyEELYBQKBQCDIQQhhFwgE74WpU6ciSZLR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br29vaGv8+bN48BAwawe/duDh8+zNixY5k1axbnzp0jf/782NjY4Ovry4kTJ9i/fz8HDx5k8+bNNG7cmMOHD6c4ltSQ9L3/8ssvJo6MgIljZUb6SsLKysrkhUSr1dKsWTNCQkL47LPPKFWqFHZ2dvj5+TFgwIA0WSreRpkyZahSpQobN26kX79+bNy4EZVKRffu3TOtD4HgQ0QIew5Bo9EAEBUVBUDRokU5evQoderUMTKNmuP17F/JqVevHr6+vnh5eVGxYkUcHByoUKECTk5OHDx4kMuXLxtinZP6Bf0KsWnTpim26+bmhoODA1qt9o31ktq
"text/plain": [
"<Figure size 640x480 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"draw(KCG, pos, nx.betweenness_centrality(KCG, normalized=False), 'Betweeenness centrality')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Closeness centrality"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 0.5689655172413793),\n",
" (2, 0.559322033898305),\n",
" (33, 0.55),\n",
" (31, 0.5409836065573771),\n",
" (8, 0.515625),\n",
" (13, 0.515625),\n",
" (32, 0.515625),\n",
" (19, 0.5),\n",
" (1, 0.4852941176470588),\n",
" (3, 0.4647887323943662)]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"closeness = nx.closeness_centrality(KCG)\n",
"sorted(closeness.items(), key=lambda x:x[1], reverse=True)[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcwAAAGbCAYAAABJUKDJAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACX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j7J161ZKlixJw4YNczQ+JCSEhg0bkpCQwNixY3F3d2f58uV06dKF9evX0717d8Dorhs7diy9evVi3LhxJCUl8e+//3Ly5En69+9vmqt+/fooisLo0aPx8PBg586dDB06lJiYGMaPH29272+//RaVSsXkyZOJjo7mu+++44033uDkyZOmPvv376d9+/bUqlWL6dOno1KpWLp0KS1btuTw4cPUrVsXgHfffZf169czevRoKlasSHh4OEeOHOHKlSvUrFmTlJQU2rVrR3JyMmPGjMHLy4t79+6xbds2oqKicHFxyfAz2rNnD506dcLb25tx48bh5eXFlStX2LZtm+nLyKVLl2jUqBE+Pj588MEHODg4sHbtWrp168aGDRtMn2MaY8aMwc3NjenTpxMUFMSsWbMYPXo0a9asAWDWrFmMGTMGR0dHPv74YwAKFSpkNsfIkSPx8PBg2rRpxMfHA3D69GmOHTtGv379KFKkCEFBQSxYsIDmzZtz+fJl7O3ts/R74eHhwYIFCxgxYgTdu3enR48eAFStWpUSJUowatQoVq1aRY0aNczGrVq1iubNm+Pj45Ol+0gkzwUh+c8RHR0tANG1a9csjylevLgYNGiQ6f348eMFIA4fPmy6FhsbK0qUKCF8fX2FXq8XQgjRtWtXUalSpafOPXToUOHt7S3CwsLMrvfr10+4uLiIhIQEIYQQfn5+AhAVKlQQycnJpn6zZ88WgLhw4YIQQgiDwSDKlCkj2rVrJwwGg6lfQkKCKFGihGjTpo3pmouLixg1alSGazt37pwAxLp16576DE+i0+lEiRIlRPHixUVkZKRZ2+NratWqlahSpYpISkoya2/YsKEoU6aM6drSpUsFIFq3bm02fsKECUKtVouoqCjTtUqVKolmzZpZrCltjsaNGwudTmfWlvYZP87x48cFIH777TfTtbSfgZ+fn+naoEGDRPHixU3vQ0NDBSCmT59uMefrr78uChcubPr9EEKIs2fPCkAsXbrUor9E8iIhXbL/QWJiYgBwcnLK8Rw7duygbt26NG7c2HTN0dGRd955h6CgIC5fvgyAq6srd+/e5fTp0+nOI4Rgw4YNdO7cGSEEYWFhple7du2Ijo42c48CDBkyBI1GY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8irZwfMXLZZZf369QwaNIgff/zRdC0pKYmoqKg8v//AgQP58ccf2bp1Kzt37sTDwyPDLwwSyYuEdMn+R3nvvfdwcHDg7bffJiQkxKL9+vXrFscfHqdDhw6cOnWK48ePm67Fx8ezePFifH19qVixIgDh4eFm4zQaDRUrVkQIgVarRa1W07NnTzZs2JCuxfv4kYmsUqtWLUqVKsUPP/xAXFxchnPq9XoLd6OnpyeFCxcmOTkZMO736nQ6sz5VqlRBpVKZ+qRHzZo1KVGiBLNmzbIQnTRr0NPTk+bNm7No0SIePHiQ4Tqzi4ODQ7aFTq1WW1ipc+bMQa/XZ/v+aRG1Ga2hatWqVK1alSVLlrBhwwb69euHlZX87i558ZG/pf9RSpUqxerVq+nbty8VKlQwy/Rz7Ngx1q1b99RMMR988AG///477du3Z+zYsRQoUIDly5dz8+ZNNmzYgEpl/C7Wtm1bvLy8aNSoEYUKFeLKlSvMnTuXjh07mvasvv32W/z8/KhXrx7Dhg2jYsWKREREcPbsWfbu3UtERES2nk2lUrFkyRLat29PpUqVGDJkCD4+Pty7dw8/Pz+cnZ3
"text/plain": [
"<Figure size 640x480 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"draw(KCG, pos, nx.closeness_centrality(KCG), 'Closeness centrality')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Eigenvector centrality"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(33, 0.37337121301323506),\n",
" (0, 0.35548349418519426),\n",
" (2, 0.3171893899684447),\n",
" (32, 0.3086510477336959),\n",
" (1, 0.2659538704545024),\n",
" (8, 0.22740509147166046),\n",
" (13, 0.22646969838808145),\n",
" (3, 0.21117407832057056),\n",
" (31, 0.191036269797917),\n",
" (30, 0.17476027834493088)]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eigenvector = nx.eigenvector_centrality(KCG)\n",
"sorted(eigenvector.items(), key=lambda x:x[1], reverse=True)[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfYAAAGbCAYAAAA2gbI4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAD78klEQVR4nOzddXyV1R/A8c9z791dF2yDbXR3hzQI0jFSOgREWgkVFbH9oRg0KEoj3dLdJSA5YDSMBaz7xvn9cdmFy93Gkpjn/Xrdl+zJ81wm3+fU9yhCCIEkSZIkSbmC6mUXQJIkSZKk7CMDuyRJkiTlIjKwS5IkSVIuIgO7JEmSJOUiMrBLkiRJUi4iA7skSZIk5SIysEuSJElSLiIDuyRJkiTlIjKwS5IkSVIuIgO7hKIofPHFFy+7GFIu0LhxYxo3bmz++datWyiKwoIFC15amSTpv0YG9lxqwYIFKIqS6ufYsWMvu4ivlO+++47169e/7GLkuEuXLvHFF19w69atl1aGLVu2yBdJScpBmpddAClnffXVVxQtWtRqe4kSJcx/jo+PR6P5b/8qfPfdd3Tp0gU/P7+XXZQcdenSJb788ksaN25MkSJFcvx+hQsXJj4+HhsbG/O2LVu2MHPmTBncJSmH/Lf/Nf8PaNWqFTVq1EjzGDs7uxdUmv+W2NhYHB0dX3YxMk0IQUJCAvb29pm+hqIo8vdLkl4w2RQvpdjHvm/fPmrUqIGdnR3Fixdn7ty5fPHFFyiKYnX+kiVLqF69Ovb29uTJk4fu3btz9+5di2MaN25MhQoVuHTpEk2aNMHBwQFfX19++OEH8zHBwcFoNBq+/PJLq3tcuXIFRVGYMWOGeVtERATvv/8+BQsWxNbWlhIlSjB58mSMRqPFuUajkalTp1KxYkXs7Ozw9PSkZcuWnDp1yvz8sbGxLFy40NxV0b9/f/P5Z86coVWrVri4uODk5ETTpk2tujKSuz7279/PsGHD8PLyokCBAml+7wkJCXzxxReUKlUKOzs7vL296dSpE9evX7co+6+//kr58uWxs7MjX758DBkyhPDwcItrFSlShLZt23Lo0CFq1aqFnZ0dxYoVY9GiRRZl7Nq1KwBNmjQxP+u+ffssrrF9+3Zq1KiBvb09c+fOBWD+/Pm8+eabeHl5YWtrS7ly5Zg9e3aazwfWfez9+/dn5syZ5u89+SOEoEiRInTo0CHF78nV1ZUhQ4Y8936SJMkae64XGRnJw4cPLbYpikLevHlTPefMmTO0bNkSb29vvvzySwwGA1999RWenp5Wx3777bdMnDiRbt26MWjQIEJDQ5k+fToNGzbkzJkzuLm5mY8NDw+nZcuWdOrUiW7durF69Wo++ugjKlasSKtWrciXLx+NGjVi5cqVTJo0yeI+K1asQK1WmwNTXFwcjRo14v79+wwZMoRChQpx5MgRJkyYwIMHD/j111/N5w4cOJAFCxbQqlUrBg0ahF6v5+DBgxw7dowaNWqwePFiBg0aRK1atXj33XcBKF68OAAXL16kQYMGuLi48OGHH2JjY8PcuXNp3Lgx+/fvp3bt2hblHDZsGJ6ennz++efExsam+h0bDAbatm3L7t276d69O6NHjyY6OpqdO3dy4cIF8/2HDBnCggULGDBgAKNGjeLmzZvMmDGDM2fOcPjwYYsm7oCAALp06cLAgQPp168ff/75J/3796d69eqUL1+ehg0bMmrUKKZNm8Ynn3xC2bJlAcz/BdMLVI8ePRgyZAiDBw+mdOnSAMyePZvy5cvTvn17NBoNmzZtYtiwYRiNRoYPH57qcz5ryJAhBAYGsnPnThYvXmzerigKvXv35ocffiAsLIw8efKY923atImoqCh69+6d7vtI0n+akHKl+fPnCyDFj62trcWxgJg0aZL553bt2gkHBwdx//5987Zr164JjUYjnv6VuXXrllCr1eLbb7+1uN758+eFRqOx2N6oUSMBiEWLFpm3JSYmivz584vOnTubt82dO1cA4vz58xbXLFeunHjzzTfNP3/99dfC0dFRXL161eK4jz/+WKjV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//DDFa5YqVSqjj5MtsjKC/FlGoxEvLy+LGuzTnn2pSe/3m5aUyn/9+nWaNm1KmTJl+PnnnylYsCBarZYtW7bwyy+/WA1WzIru3bvzwQcfsHTpUj755BOWLFlCjRo1Uny5kiQpZTKwSxa8vLyws7MjICDAat+z24oXL44QgqJFi2ZrIPXz82PIkCHm5virV68yYcIEq3vHxMTQrFmzNK9VvHhxtm/fbjUg61kpNfN6enri4ODAlStXrPb5+/ujUqkoWLBgeh4pxXIdP34cnU5n1bLx9DG7du2iXr162fbCkJnm7E2bNpGYmMjGjRstWgWe7QrIjjLkyZOHNm3asHTpUnr16sXhw4ctBkJKkvR8so9dsqBWq2nWrBnr168nMDDQvD0gIMCqb75Tp06o1Wq+/PJLq1qhEIJHjx5lqgxubm60aNGClStXsnz5crRarVXimG7dunH06FG2b99udX5ERAR6vR6Azp07I4RIcQrd02V2dHQkIiLCYr9araZ58+Zs2LDBIlNbcHAwy5Yto379+ri4uGTqGTt37szDhw8tpu89W65u3bphMBjMXRJP0+v1VuVNj+R59Rk5N7kl4OnvKzIykvnz52f4/ukpQ58+fbh06RLjx49HrVbTvXv3TN1Hkv6rZI09l9u6dSv+/v5W2+vWrWsxIOxpX3zxBTt27KBevXoMHToUg8HAjBkzqFChAmfPnjUfV7x4cb755hsmTJjArVu38PPzw9nZmZs3b7Ju3Treffddxo0bl6lyv/322/Tu3ZtZs2bRokULi2lzAOPHj2fjxo20bdvWPKUrNjaW8+fPs3r1am7duoWHhwdNmjShT58+TJs2jWvXrtGyZUuMRiMHDx6kSZMmjBgxAoDq1auza9cufv75Z3x8fChatCi1a9fmm2++YefOndSvX59hw4ah0WiYO3cuiYmJFnPwM6p
"text/plain": [
"<Figure size 640x480 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"draw(KCG, pos, nx.eigenvector_centrality(KCG), 'Eigenvector centrality')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Local clustering coefficient"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.6666666666666666"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.clustering(KCG, 3)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(7, 1.0),\n",
" (12, 1.0),\n",
" (14, 1.0),\n",
" (15, 1.0),\n",
" (16, 1.0),\n",
" (17, 1.0),\n",
" (18, 1.0),\n",
" (20, 1.0),\n",
" (21, 1.0),\n",
" (22, 1.0)]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"clustering = nx.clustering(KCG)\n",
"sorted(clustering.items(), key=lambda x:x[1], reverse=True)[0:10]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Meso analysis. Group level metrics"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Cliques"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAABc60lEQVR4nO3dd1RUV9sF8D0IAnbEErEiHaQJI2o0ajQqqLFEE8XeNcaWaL5oNKZoAnaxx2401qixx5pYYJCmKChFiQViQ1TKUIa53x8KLyNIkXKn7N9arCTOMPfRFbn7nvOccySCIAggIiIinaUndgFEREQkLoYBIiIiHccwQEREpOMYBoiIiHQcwwAREZGOYxggIiLScQwDRE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Rw/s2bMHlSpVErukXAqFIvd4YH9/fzYTiiwtLQ0hISEqmyPFx8cD+F9zYs7oAZsTSRMwDBABiIiIQLt27eDs7IyTJ0/CyMhI7JLyuXjxIj744ANs2LABY8aMEbscekNOc2LO6EHe5kQXFxeV1QsWFhZqM+pEBDAMEOHevXto27Yt6tSpg3/++Qc1a9YUu6S3Gj58OI4dO4aoqCiYmpqKXQ4VIisrK9+xztHR0QD+15yYM4IglUrZnEiiYhggnZaYmIj27dtDLpfD399f7ed7Hz16BBsbGwwcOBDr1q0TuxwqoZzmxLybIz1//hwAYGdnpzJ64ODgwOZEqjAMA6SzUlNT0aVLF8TGxuLy5cuwtrYWu6RiWbVqFaZMmYLAwEBIpVKxy6FSUCqVBR7rnJ2djapVq+bbOVHdwyppLoYB0kl
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"CG = nx.Graph()\n",
"CG.add_edges_from([('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('B', 'E'),('C','D'), ('D', 'E')])\n",
"nx.draw_networkx(CG, node_color='c')"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[['D', 'B', 'A', 'C'], ['D', 'B', 'E']]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(nx.find_cliques(CG))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4, 0, 1, 2, 3], [4, 5], [6, 5], [6, 7], [7, 8, 9, 10, 11]]\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAApQAAAHzCAYAAACe1o1DAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAB44ElEQVR4nO3dd3RUxd8G8GfTe0hCgCTU0ISEUEJHKaLwQ0WU0IJgewFBivQmhCZIB7sg2CgCARQiIEgCFkAwoYZepGUTCCEJqbvJ7r5/rBtTttzdezf1+ZzjQfbeO3eIIE9m5jsj02g0GhARERERWcimrDtARERERBUbAyURERERicJASURERESiMFASERERkSgMlEREREQkCgMlEREREYnCQElEREREojBQEhEREZEoDJREREREJAoDJRERERGJwkBJRERERKIwUBIRERGRKAyURERERCQKAyURERERicJASURERESiMFASERERkSgMlEREREQkCgMlEREREYnCQElEREREojBQEhEREZEoDJREREREJAoDJRERERGJwkBJRERERKIwUBIRERGRKAyURERERCQKAyURERERicJASURERESiMFASERERkSgMlEREREQkCgMlEREREYnCQElEREREojBQEhEREZEoDJREREREJAoDJRERERGJwkBJRERERKIwUBIRERGRKAyURERERCSKXVl3gIiIiKhcUamAHAWgVgM2NoCzI2BrW9a9KtcYKImIiIiycoDEZCAlHchVlLzu5Aj4eAJ+voCrc+n3r5yTaTQaTVl3goiIiKhM5CiA67eB1Azhz3i5A43ra0cuCQADJREREVVVicnAjbuA2oIoZCMDGtXVjlgSAyURERFVQXfkwG25+Hbq+wP1/MW3U8GxypuIiIiqlsRkacIkoG0nMVmatiowFuUQERFR1ZGj0E5z63Hxn5uY/+1XiLt2GUmPU+Di6ITm9QMxbcgw9O3c1XCbN+4C1Tyq9JpKjlASERFR1XH9tsE1k3ceJCEjOwtv9H4JH42fgrmv/x8A4OXZU7A+arfhNtUabbtVGNdQEhERUdWQlQPEXjTrEZVKhdBRw5GrVOLKpp3Gb24bVGW3FOIIJREREZU9lQrIzAaeZGp/VKmkf4cFax1tbW1Rp0ZNpGUK2FaoCq+l5BpKIiIiKhulvZl4SrrAbuUgR6FAelYm9h7/HQdOnsDgZ58T1n4jkX2soBgoiYiIqHQJ3Uw8VwEkPNT+I3Yz8XyV/tCqx5TP12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kusoTWOgJCIiolL3448/QqPRoH///toPbG0BNxc8zM3CmetXAVtbBAcHIzg4GD/88IPF71m3bh2uXr2KNWvW6N0mqLCEhAQAYKC0AAMlERERlbrIyEh069YNNWrUKPJ58crq8PBw7NmzB1lZWWa/4/Hjx5g3bx7efvtttGrVyuT9crkcADjlbQEGSiIiIipVjx49QkxMDAYOHFjiWvFAOWTIEGRnZyMqKsrs9yxcuBBKpRIffPCBoPvlcjkcHBzg4+MDAHBycoKzszNHKAVgoCQiIqJStWfPHqjV6v+muwvx8fFBSkpKwc8DAwPRoUMHs6e9r169is8++wzvv/8+atWqJeiZhIQE+Pv7F5ka516UwjBQEhERUamKjIxE165dUbNmzRLXvL29kZaWBpVKVfBZeHg4Dhw4YNbU89SpU1G7dm1MnDhR8DOFtwwq3B9OeZvGQElERESl5vHjx4iOjtY73Q1oA5xGo0F6enrBZ4MGDYJKpcLu3bsFvePXX3/Fzz//jOXLl8PJyUlw3wpvaq7j5eXFEUoBGCiJiIio1Pz0009QqVQICwvTe11XWV142tvPzw/du3cXNO2dn5+PyZMn4+mnn8aAAQPM6puhEUoGStMYKImIiKjUREZG4pl
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Another example\n",
"BG = nx.barbell_graph(m1=5, m2=2)\n",
"gl = list(nx.find_cliques(BG))\n",
"print(gl)\n",
"nx.draw(BG, node_color='pink', with_labels=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Community detection"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Helper functions"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# from Network Science with Python and NetworkX \n",
"def set_node_community(G, communities):\n",
" '''Add community to node attributes'''\n",
" for c, v_c in enumerate(communities):\n",
" for v in v_c:\n",
" # Add 1 to save 0 for external edges\n",
" G.nodes[v]['community'] = c + 1\n",
" \n",
"def set_edge_community(G):\n",
" '''Find internal edges and add their community to their attributes'''\n",
" for v, w, in G.edges:\n",
" if G.nodes[v]['community'] == G.nodes[w]['community']:\n",
" # Internal edge, mark with community\n",
" G.edges[v, w]['community'] = G.nodes[v]['community']\n",
" else:\n",
" # External edge, mark as 0\n",
" G.edges[v, w]['community'] = 0\n",
"def get_color(i, r_off=1, g_off=1, b_off=1):\n",
" r0, g0, b0 = 0, 0, 0\n",
" n = 16\n",
" low, high = 0.1, 0.9\n",
" span = high - low\n",
" r = low + span * (((i + r_off) * 3) % n) / (n - 1)\n",
" g = low + span * (((i + g_off) * 5) % n) / (n - 1)\n",
" b = low + span * (((i + b_off) * 7) % n) / (n - 1)\n",
" return (r, g, b)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Let's calculate the communities"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import networkx.algorithms.community as nxcom\n",
"\n",
"\n",
"result = nxcom.girvan_newman(KCG)\n",
"communities = next(result)\n",
"len(communities)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# Set node and edge communities\n",
"set_node_community(KCG, communities)\n",
"set_edge_community(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# Set community color for nodes\n",
"node_color = [get_color(KCG.nodes[v]['community']) for v in KCG.nodes]\n",
"\n",
"# Set community color for internal edges\n",
"external = [(v, w) for v, w in KCG.edges if KCG.edges[v, w]['community'] == 0]\n",
"internal = [(v, w) for v, w in KCG.edges if KCG.edges[v, w]['community'] > 0]\n",
"internal_color = [get_color(KCG.edges[e]['community']) for e in internal]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Let's see the result"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAD4cUlEQVR4nOydd3gU1frHPzPbkt1Nr4QQQu+9K0oHEQTs3mv9WbFe1Hvt/drLtYFi93rtigoCKlVEeu+9hxBSN9neZn5/bLJkyaYHTOB8noeHZOacM2c2yZx33vO+31dSVVVFIBAIBALBWYv8V09AIBAIBALBX4swBgQCgUAgOMsRxoBAIBAIBGc5whgQCAQCgeAsRxgDAoFAIBCc5QhjQCAQCASCsxxhDAgEAoFAcJajrUkjRVHIzs4mKioKSZJO9ZwEAoFAIBA0AKqqYrVaSUtLQ5Yrf/+vkTGQnZ1NixYtGmxyAoFAIBAITh9HjhwhPT290vM1MgaioqKCg0VHRzfMzAQCgUAgEJxSSkpKaNGiRXAdr4waGQNlWwPR0dHCGBAIBAKBoIlR3Ra/CCAUCAQCgeAsRxgDAoFAIBCc5QhjQCAQCASCsxxhDAgEAoFAcJYjjAGBQCAQCM5yhDEgEAgEAsFZjjAGBAKBQCA4yxHGgEAgEAgEZznCGBAIBAKB4CxHGAMCgUAgEJzlCGNAIBAIBIKzHGEMCAQCgUBwliOMAYFAIBAIznKEMSAQCAQCwVmOMAYEAoFAIDjLEcaAQCAQCARnOdq/egKCU49P9VDkO4ZXcSJJGsyaeMxyPJIk/dVTEwgEAkEjQBgDZyg2fyHbHUvY61qNxXcMFTXkvEEy0kzfgS7GYWQYuiFLwkkkEAgEZ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 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Draw external edges\n",
"nx.draw_networkx(\n",
" KCG, pos=pos, node_size=0,\n",
" edgelist=external, edge_color=\"#333333\", with_labels=False)\n",
"# Draw nodes and internal edges\n",
"nx.draw_networkx(\n",
" KCG, pos=pos, node_color=node_color,\n",
" edgelist=internal, edge_color=internal_color)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"it classifies well most of the nodes (except for 8 and 2)."
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAADjRElEQVR4nOyddXhURxeH390oKRAgeLDixaFFW9ytuJVCgRZ3KRKgFIcCLVagVKC4FynuXhyKSz/cLQnEkz3fH8NGd6MLJDDv8+yT7L1zZ87dyPzuzBGDiAgajUaj0WjeW4xv2wCNRqPRaDRvFy0GNBqNRqN5z9FiQKPRaDSa9xwtBjQajUajec/RYkCj0Wg0mvccLQY0Go1Go3nP0WJAo9FoNJr3HPvYNDKZTNy7d48UKVJgMBhet00ajUaj0WhsgIjw4sULMmfOjNFo/fk/VmLg3r17ZM2a1WbGaTQajUajeXPcvn2bLFmyWD0fKzGQIkWK0M5SpkxpG8s0Go1Go9G8Vry9vcmaNWvoPG6NWIkB89ZAypQptRjQaDQajSaJEdMWv3Yg1Gg0Go3mPUeLAY1Go9Fo3nO0GNBoNBqN5j1HiwGNRqPRaN5ztBjQaDQajeY9R4sBjUaj0Wjec7QY0Gg0Go3mPUeLAY1Go9Fo3nO0GNBoNBqN5j1HiwGNRqPRaN5ztBjQaDQajeY9R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ZGo9FobIUWAxF4hPJYV85+Tk6QMWPMV3l7Q/r0ahvBTMqUSgwki1BcLwS1/J04SZEiBRUrVuT69eucPXv2jYz59OlT0qRJA0BAQOKOutBoNJp3FS0GInA8XldVqgRbtqgVghs34NIl6N5dbRX07h2
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colors = ['yellow' if not node in mr_hi_nodes else 'pink' for node in KCG.nodes()]\n",
"nx.draw_networkx(KCG, pos, node_color=colors)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Alternatively, we can draw with the library *nx_altair* if installed. "
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"communities = list(nx.community.label_propagation_communities(KCG))\n",
"centralities = nx.eigenvector_centrality(KCG)\n",
"\n",
"for f in KCG.nodes():\n",
" for i, c in enumerate(communities):\n",
" if f in c:\n",
" attrs = {f: {\"community\" : str(i),\n",
" \"centrality\" : centralities[f],\n",
" \"name\" : f\n",
" }}\n",
" nx.set_node_attributes(KCG, attrs)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" #altair-viz-b41c24c09eb8466f801b9980b5335fca.vega-embed {\n",
" width: 100%;\n",
" display: flex;\n",
" }\n",
"\n",
" #altair-viz-b41c24c09eb8466f801b9980b5335fca.vega-embed details,\n",
" #altair-viz-b41c24c09eb8466f801b9980b5335fca.vega-embed details summary {\n",
" position: relative;\n",
" }\n",
"</style>\n",
"<div id=\"altair-viz-b41c24c09eb8466f801b9980b5335fca\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-b41c24c09eb8466f801b9980b5335fca\") {\n",
" outputDiv = document.getElementById(\"altair-viz-b41c24c09eb8466f801b9980b5335fca\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm/vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm/vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm/vega-lite@5.17.0?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"5.17.0\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"layer\": [{\"data\": {\"name\": \"data-796e8487f401da08203543097a1d3975\"}, \"mark\": {\"type\": \"line\", \"color\": \"black\", \"opacity\": 1, \"strokeWidth\": 1}, \"encoding\": {\"detail\": {\"field\": \"edge\", \"type\": \"quantitative\"}, \"x\": {\"axis\": {\"grid\": false, \"labels\": false, \"ticks\": false, \"title\": \"\"}, \"field\": \"x\", \"type\": \"quantitative\"}, \"y\": {\"axis\": {\"grid\": false, \"labels\": false, \"ticks\": false, \"title\": \"\"}, \"field\": \"y\", \"type\": \"quantitative\"}}}, {\"data\": {\"name\": \"data-dd61032c40862ff1bf0ec28f2e9815c3\"}, \"mark\": {\"type\": \"point\", \"opacity\": 1, \"strokeWidth\": 1.0}, \"encoding\": {\"fill\": {\"field\": \"community\", \"scale\": {\"scheme\": \"accent\"}, \"type\": \"nominal\"}, \"size\": {\"field\": \"centrality\", \"legend\": null, \"type\": \"quantitative\"}, \"tooltip\": [{\"field\": \"name\", \"type\": \"quantitative\"}, {\"field\": \"centrality\", \"type\": \"quantitative\"}, {\"field\": \"community\", \"type\": \"nominal\"}], \"x\": {\"axis\": {\"grid\": false, \"labels\": false, \"ticks\": false}, \"field\": \"x\", \"type\": \"quantitative\"}, \"y\": {\"axis\": {\"grid\": false, \"labels\": false, \"ticks\": false}, \"field\": \"y\", \"type\": \"quantitative\"}}}], \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.17.0.json\", \"datasets\": {\"data-796e8487f401da08203543097a1d3975\": [{\"pair\": [0, 1], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 4, \"community\": 1, \"target\": 1, \"edge\": 0}, {\"pair\": [0, 1], \"source\": 0, \"y\": -0.10333340467694771, \"x\": 0.22157555692712255, \"weight\": 4, \"community\": 1, \"target\": 1, \"edge\": 0}, {\"pair\": [0, 2], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 5, \"community\": 0, \"target\": 2, \"edge\": 1}, {\"pair\": [0, 2], \"source\": 0, \"y\": -0.04889377094377533, \"x\": 0.02665036665852286, \"weight\": 5, \"community\": 0, \"target\": 2, \"edge\": 1}, {\"pair\": [0, 3], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 3, \"community\": 1, \"target\": 3, \"edge\": 2}, {\"pair\": [0, 3], \"source\": 0, \"y\": -0.35099864408790443, \"x\": 0.13737656718587196, \"weight\": 3, \"community\": 1, \"target\": 3, \"edge\": 2}, {\"pair\": [0, 4], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 3, \"community\": 1, \"target\": 4, \"edge\": 3}, {\"pair\": [0, 4], \"source\": 0, \"y\": -0.28606246472555474, \"x\": 0.6925676154518609, \"weight\": 3, \"community\": 1, \"target\": 4, \"edge\": 3}, {\"pair\": [0, 5], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 3, \"community\": 1, \"target\": 5, \"edge\": 4}, {\"pair\": [0, 5], \"source\": 0, \"y\": -0.42069004334136134, \"x\": 0.7905284165415447, \"weight\": 3, \"community\": 1, \"target\": 5, \"edge\": 4}, {\"pair\": [0, 6], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 3, \"community\": 1, \"target\": 6, \"edge\": 5}, {\"pair\": [0, 6], \"source\": 0, \"y\": -0.5015268559996672, \"x\": 0.7260729889733195, \"weight\": 3, \"community\": 1, \"target\": 6, \"edge\": 5}, {\"pair\": [0, 7], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 2, \"community\": 1, \"target\": 7, \"edge\": 6}, {\"pair\": [0, 7], \"source\": 0, \"y\": -0.24720557271361956, \"x\": 0.1839750134271702, \"weight\": 2, \"community\": 1, \"target\": 7, \"edge\": 6}, {\"pair\": [0, 8], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 2, \"community\": 0, \"target\": 8, \"edge\": 7}, {\"pair\": [0, 8], \"source\": 0, \"y\": 0.11686676861815107, \"x\": -0.052697367481038815, \"weight\": 2, \"community\": 0, \"target\": 8, \"edge\": 7}, {\"pair\": [0, 10], \"source\": 0, \"y\": -0.2302323693491078, \"x\": 0.3513651685988333, \"weight\": 2, \"community\": 1, \"target\": 10, \"edge\": 8}, {\"pair\": [0
"</script>"
],
"text/plain": [
"alt.LayerChart(...)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import nx_altair as nxa\n",
"\n",
"nxa.draw_networkx(\n",
" G=KCG,\n",
" pos=pos,\n",
" node_size=\"centrality\",\n",
" node_color='community',\n",
" cmap='accent',\n",
" node_tooltip=['name', 'centrality', \"community\"]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Macro analysis. Global metrics"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Number of nodes"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"34"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"34"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(KCG.nodes)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"34"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(KCG.nodes())"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"34"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.number_of_nodes(KCG)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Number of edges"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"78"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(KCG.edges())"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Graph named \"Zachary's Karate Club\" with 34 nodes and 78 edges\n"
]
}
],
"source": [
"print(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"78"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.number_of_edges(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"483"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(list(nx.non_edges(KCG))) ## Number of non-existing edges"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DiGraph with 3 nodes and 2 edges\n"
]
}
],
"source": [
"print(D)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Shortest path"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 8, 33], [0, 13, 33], [0, 19, 33], [0, 31, 33]]"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(nx.all_shortest_paths(KCG, 0, 33)) # All shortest paths"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 8, 33]"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.shortest_path(KCG, 0, 33) #Just one"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.shortest_path_length(KCG, 0, 33) # Minimum distance"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2.408199643493761"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.average_shortest_path_length(KCG) #Average shortest path "
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.diameter(KCG) # Diameter (largest shortest path between 2 nodes)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Transitivity"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.2556818181818182"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.transitivity(KCG)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Average local clustering"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.5706384782076823"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.average_clustering(KCG)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Density"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.13903743315508021"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.density(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.13903743315508021"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 2 * m / n * (n - 1)\n",
"\n",
"2 * nx.number_of_edges(KCG) / ( nx.number_of_nodes(KCG) * (nx.number_of_nodes(KCG) - 1))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Minimal cut"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{0}"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import networkx.algorithms.connectivity as nxcon\n",
"nxcon.minimum_node_cut(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{(11, 0)}"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nxcon.minimum_edge_cut(KCG)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{2, 8, 13, 19, 30, 31}"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nxcon.minimum_st_node_cut(KCG, 0, 33) #Nodes we should remove to separate nodes 0 and 33 into disconnected halves"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Connectivity"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.node_connectivity(KCG) # it is size of the mininum_node_cut()"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.edge_connectivity(KCG) # it is the size of mininum_edge_cut()"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2.2174688057040997"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.average_node_connectivity(KCG)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# References\n",
"* Networkx tutorial https://networkx.github.io/documentation/stable/tutorial.html\n",
"* Network Centrality Measures and Their Visualization, Can Güney Aksakalli, 2017, https://aksakalli.github.io/2017/07/17/network-centrality-measures-and-their-visualization.html"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## Licence"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
"© Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"datacleaner": {
"position": {
"top": "50px"
},
"python": {
"varRefreshCmd": "try:\n print(_datacleaner.dataframe_metadata())\nexcept:\n print([])"
},
"window_display": false
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}