1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-17 20:12:28 +00:00
sitc/sna/1_First_Steps.ipynb

1302 lines
164 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": "slide"
}
},
"source": [
"## [Introduction to Network Analysis](0_Intro_Network_Analysis.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# Table of Contents\n",
"\n",
"* [Installation](#Installation)\n",
"* [First steps](#First-steps)\n",
"* [Reading Data from a File](#Reading-Data-from-a-File)"
]
},
{
"cell_type": "markdown",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"# Installation\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"For installing **networkx** we can use **conda** or **pip**: _'conda install networkx'_. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"It could be good to update first the packages: _'conda update --all'_."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# First steps\n",
"Create an empty graph with no nodes"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import networkx as nx"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.simplefilter(action='ignore', category=FutureWarning)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.number_of_nodes(G)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Add a node"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_node('1')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.number_of_nodes(G)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"NodeView(('1',))"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G.nodes"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"If you add edges of nodes that has not been added, network will add the nodes automatically."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_edge('2', '3')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"NodeView(('1', '2', '3'))"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G.nodes()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"For more info about Graph.nodes() see [Networkx manual](https://networkx.github.io/documentation/networkx-2.1/reference/classes/generated/networkx.Graph.nodes.html)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['1', '2', '3']\n"
]
}
],
"source": [
"print(G.nodes)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"EdgeView([('2', '3')])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G.edges"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('2', '3')]\n"
]
}
],
"source": [
"print(G.edges)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Building a simple graph"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import networkx as nx"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_edges_from([('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'D')]) # using a list of edge tuples"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb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GVAREpUzIyMpg7dy59+/alRYsWRsexqyZNmhAfH0/Tpk3p0qULq1atMjqSOCmVAREpU1asWMHp06eZP3++0VFKRfXq1dmyZQvDhg0jLCyMKVOmaGKh3DJNIBSRMuPKlStERkYycOBA/P39jY5Tajw9PVm2bBkBAQFMmjSJI0eO8O6772qOlxSaRgZEpMx49dVX+eWXX5g7d67RUUqdyWRiwoQJbN68mV27dtGmTRtOnDhhdCxxEioDIlImpKam8swzzzBy5Ejq169vdBzDdOvWjbi4OK5fv05gYCA7duwwOpI4AZUBESkTXnzxRa5evUpERITRUQx3zz33EB8fT7NmzXjwwQd58803jY4kDk5lQESc3s8//8yLL77I+PHj8fX1NTqOQ6hWrRqff/45I0aM4PHHH2fSpElkZWUZHUsclCYQiojTe/bZZzGZTEyfPt3oKA7F09OT119/naZNmzJx4kSOHDnCe++9h4+Pj9HRxMFoZEBEnNqZM2d49dVXmTJlCtWrVzc6jkMaO3YsmzdvJi4ujtatW3Ps2DGjI4mDURkQEacWGRmJl5cXkyZNMjqKQ+vatStxcXFkZ2cTFBREdHS00ZHEgagMiIjTOn78OCtXrmTGjBlaU18IjRs3Jj4+nubNm9OtWzeWL19udCRxECoDIuK05s6dS61atRg7dqzRUZxG1apV+eyzzwgPDyc8PJwJEyZoYqF
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nx.draw_networkx(G, node_color='orange')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Undirected and directed graphs"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Now we create an undirected graph using the class **Graph**."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"U = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"U.add_edge('A', 'B')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"U.add_edge('A', 'C')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1, 1],\n",
" [1, 0, 0],\n",
" [1, 0, 0]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.adjacency_matrix(U).todense()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1, 1],\n",
" [1, 0, 0],\n",
" [1, 0, 0]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.to_scipy_sparse_array(U).todense()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAsN0lEQVR4nO3dd3hUBdr+8XtCEEKPoLtGERASNa8F6QLSBAEpCsJMChAIQQJIkSYigvVn513FFdmFtayKrnV9VRQILRBC2gwWUJogMIgQQgghCUzm/P4I7IKSAkxypnw/1+Wl7Jw5c+Oqc+d5zpyxGIZhCAAABKwgswMAAABzUQYAAAhwlAEAAAIcZQAAgABHGQAAIMBRBgAACHCUAQAAAlxwRQ5yu91yOp2qW7euLBZLZWcCAAAeYBiG8vLyFBYWpqCg0n/+r1AZcDqdaty4scfCAQCAqrN3715dc801pT5eoTJQt27d/5ysXr16nkkGAAA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YAACgzM1M2m02//fabPvjgA1mtVrMjoQoxGQCAAGYYhhYsWKCOHTuqQYMGstvtFIEARBkAgAB19OhRDRkyRJMmTVJiYqI2bNig5s2bmx0LJmBNAAABKC0tTTabTUePHtUnn3yiQYMGmR0JJmIyAAABxDAMzZ8/X506ddKVV14pu91OEQBlAAACxZEjR3TPPfdo2rRpmjx5spKTk9W0aVOzY8ELsCYAgACQkpKiqKgo5efn6/PPP9eAAQPMjgQvwmQAAPyY2+3Wc889py5duqhx48ZyOBwUAfwBZQAA/NShQ4f
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nx.draw_networkx(U, node_color='c')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"We use the class **DiGraph** for Directed Graphs as follows."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"D = nx.DiGraph()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"D.add_edge('B', 'A')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1],\n",
" [0, 0]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.adjacency_matrix(D).todense() "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAr+UlEQVR4nO3de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Wrx4scLCwvTtt9+angUAcEEEgYuzWCzq06ePkpOTlZWVpZCQEL3//vumZwEAXAxB4Cbuv/9+paamqmPHjnriiSfUq1cvnT171vQsAICLIAjcSMmSJbVw4UItXLhQK1asUJ06dfTVV1+ZngUAcAEEgRvq0aOHUlJSZLVaFRoaqgULFig3N9f0LACAEyMI3FStWrW0a9cudevWTb1799YTTzyhM2fOmJ4FAHBSBIEbK1GihObNm6elS5dq1apVCg4O1p49e0zPAgA4IYLAA3Tt2lVpaWny8/NTnTp19M4773CEAAD4C4LAQ9SsWVM7d+5Ur1691K9fP3Xp0kUZGRmmZwEAnARB4EF8fX01e/ZsrVixQmvXrlVwcLDS09NNzwIAOAGCwAN16tRJdrtdAQEBqlu3rmbOnMkRAgB4OILAQ9WoUUPbt29X3759NWjQIHXs2FEnT540PQsAYAhB4MF8fHz05ptvKiYmRgkJCQoMDFRycrLpWQAAAwgC6JFHHpHdbleFChVUr149TZs2jSMEAPAwBAEkSdWrV1diYqIGDx6sYcOGqV27djp+/LjpWQCAIkIQ4A/FixfXlClTFBsbq23btslms2nHjh2mZwEAigBBgL9
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nx.draw_networkx(D, node_color='pink')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Weighted graphs"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W.add_edge('A', 'B', weight = 2)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W.add_weighted_edges_from([('A', 'C', 1), ('A', 'D', 3), ('B', 'D', 4)])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAA1tklEQVR4nO3deVyU9d7G8WsAFZRtxCVRQFzQ1MpKj9lmloD7rmUuZWoe1x63zDSfjuVTnbbTrqWVdrLFMnMXNMsyTdPsWJi4oCJjZTAIIhjLPH+QHjPZZ+ae5fN+vXil3vfMXJxT3hczv/v7M9lsNpsAAIDX8jE6AAAAMBZlAAAAL0cZAADAy1EGAADwcpQBAAC8HGUAAAAvRxkAAMDL+ZXnpKKiIlksFgUFBclkMjk6EwAAsAObzabs7GyFh4fLx6fkn//LVQYsFosiIiLsFg4AADhPamqqGjVqVOLxcpWBoKCgC08WHBxsn2QAAMChsrKyFBERceE6XpJylYHzHw0EBwdTBgAAcDNlfcTPAkIAALwcZQAAAC9HGQAAwMtRBgAA8HKUAQAAvBxlAAAAL0cZAADAy1EGAADwcpQBAAC8HGUAAAAvRxkAAMDLUQYAAPBylAEAALwcZQAAAC9HGQAAwMtRBgAA8HJ+RgewpzMFBTqUm6tzNptqmExqFhCgQD+P+hYBALA7t79SJuXkaIHFonXp6TqSlyfbRcdMkpr4+6t7WJj+Hh6uVrVqGRUTAACXZbLZbLayTsrKylJISIhOnz6t4OBgZ+QqU0pursYmJyvRapWfpIJSzj1/PNZs1sKYGEUHBDgnJAAABirv9dst1wwssljUatcubbFaJZVeBC4+vsVqVatdu7TIYnFoPgAA3InblYH5x45pTHKy8oqKyiwBlyqQlFdUpDHJyZp/7Jgj4gEA4Hbcqgwsslg0JyXFLs81JyVFi0+etMtzAQDgztxmAWFKbq4mHTpU+klpadL770u7d0u//SZVqyZFR0u33Sb16iXVqPGn0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JAgICtGrVKjVp0kRxcXE6dOiQ0ZEAAJBEGXCqkJAQbdiwQaGhoYqNjVVaWprRkQAAoAw4W926dZWQkKDCwkLFxcUpPT3d6EgAAC9HGTBAZGSkEhMTderUKXXr1k3Z2dlGRwIAeDHKgEFatGihDRs26MCBA+rbt6/y8vKMjgQA8FKUAQNdd911Wr16tb7++msNGTJEBQUFRkcCAHghyoDBbr31Vn300Udas2aNRo8eraKiIqMjAQC8DGXABfTo0UNLly7V0qVLNXXqVLY+BgA4FeOIXcSQIUOUmZmp8ePHKywsTI888ojRkQAAXoIy4ELGjRunjIwMzZkzR2azWRMnTjQ6EgDAC1AGXMzDDz+sjIwMTZo0SaGhoRo2bJjRkQAAHo4y4GJMJpOeeeYZZWZm6t5771VISIh69epldCwAgAdjAaELMplMWrhwofr06aNBgwbp888/NzoSAMCDUQZclJ+fn5YtW6ZbbrlFvXv31rfffmt0JACAh6IMuLAaNWrok08+UatWrdS1a1ft37/f6EgAAA9EGXBxgYGBWrdunRo0aKC4uDgdO3bM6EgAAA9DGXADtWvX1saNG1W9enXFxsbql19+MToSAMCDUAbcRHh4uBITE3XmzBl17dpVmZmZRkcCAHgIyoAbadKkiRISEnTs2DH16tVLZ8+eNToSAMADUAbcTJs2bbRu3Tp99913GjhwoH7//XejIwEA3BxlwA3dcMMNWrlypTZv3qx77rlHhYWFRkcCALgxyoCb6tKli9577z19+OGHmjhxIjsdAgAqjTLgxvr376833nhDCxYs0Jw5c4yOAwBwU+xN4Obuu+8+Wa1WTZ8+XWazWdOnTzc6EgD
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nx.draw_networkx(W, node_color='c')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{('A', 'B'): Text(-0.07776005662185159, -0.3386811967059936, '2'),\n",
" ('A', 'C'): Text(0.36944887879272426, 0.5256867154763809, '1'),\n",
" ('A', 'D'): Text(-0.26428629994186137, -0.13563634199376917, '3'),\n",
" ('B', 'D'): Text(-0.3694433520899971, -0.5256943837037334, '4')}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7JklEQVR4nO3dfXzNdePH8deZYXM3axUtd5NN1lVKViKkLs1dFMY2NxFyE5dIyk11lSglJELovrNIJEoXcw2lycbwK2ONoTaUddgcO2w75/eHuBKz4Wzfc/N+Ph49WOd7vt/3Hsl573Pz/ZocDocDERER8Vo+RgcQERERY6kMiIiIeDmVARERES+nMiAiIuLlVAZERES8nMqAiIiIl1MZEBER8XK+JTnIbreTlZVF1apVMZlMpZ1JREREnMDhcJCbm0twcDA+PkX//F+iMpCVlUXt2rWdFk5ERETKzi+//EKtWrWKfL1EZaBq1arnTlatWjXnJBMREZFSlZOTQ+3atc99jhelRGXg7NRAtWrVVAZERETcTHFT/FpAKCIi4uVUBkRERLycyoCIiIiXUxkQERHxcioDIiIiXk5lQERExMupDIiIiHg5lQEREREvpzIgIiLi5VQGREREvJzKgIiIiJdTGRAREfFyKgMiIiJeTmVARETEy6kMiIiIeDmVARERES/na3QAERERd3GioID0vDxOORxUNJlo4O9PFV/3/yh1/+9ARESkFO2yWpmXlcXX2dnss9lw/OU1E1Dfz48OQUEMCQ4mvHJlo2JeFZPD4XAUd1BOTg4BAQEcP36catWqlUUuERERQ2Xk5TE4LY21Fgu+QMEljj37etvAQOaHhRHi7182IYtR0s9vrRkQERH5m4VZWYQnJZFgsQCXLgJ/fT3BYiE8KYmFWVmlms/ZVAZERET+YvKBAwxKS8NmtxdbAv6uALDZ7QxKS2PygQOlEa9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AZERKTU5OTkEB0dzfDhwxk8eDDffvst9erVMzqW/I2mCUREpFTs2LGDqKgoDh8+zJIlS4iKijI6khRBIwMiIuJUDoeDRYsW0axZMypVqsTWrVtVBFycyoCIiDiN1WqlX79+DBw4kL59+5KYmEhoaKjRsaQYmiYQERGnSE1NpXv37uzfv5+PPvqI3r17Gx1JSkgjAyIictU+/vhjmjZtCkBSUpKKgJtRGRARkSuWl5fH448/Tp8+fejWrRtbtmwhPDzc6FhymTRNICIiV+Tnn38mKiqKPXv2sGDBAgYMGIDJZDI6llwBjQyIiMhlW7p0KXfeeScnT55k8+bNDBw4UEXAjakMiIhIiZ0+fZqRI0cSFRVFu3btSE5OpnHjxkbHkqukaQIRESmR/fv307NnT1JSUpg9ezbDhg3TaICHUBkQEZFirVy5kkcffZSAgAA2bdpERESE0ZHEiTRNICIiRcrPz2fs2LF07tyZli1bsm3bNhUBD6SRARERuahff/2V6OhoNm/ezLRp0xg9erSmBTyUyoCIiFxgzZo19OrVCz8/PzZu3Ejz5s2NjiSlSNMEIiJyTmFhIc8//zzt2rWjadOmpKSkqAh4AY0MiIgIAIcPHyY2NpYNGzbw8ssv8+yzz+Ljo58ZvYHKgIiIsH79emJiYnA4HMTHx9OmTRujI0kZUuUTEfFidrudKVOm8MADD3DzzTezfft2FQEvpDIgIuKljh49SseOHZk4cSLjx48nPj6emjVrGh1LDKBpAhERL/T999/Ts2dPbDYbq1evJjIy0uhIYiCNDIiIeBGHw8H06dNp3bo1devWJSUlRUVAVAZERLyFxWLhkUce4amnnmLUqFEkJCRQq1Yto2OJC9A0gYiIF0hOTqZHjx5
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pos = nx.spring_layout(W) # positions for all nodes\n",
"labels = nx.get_edge_attributes(W,'weight')\n",
"nx.draw_networkx(W, pos=pos, node_color='c')\n",
"nx.draw_networkx_edge_labels(W, pos, edge_labels=labels)\n",
"#nx.draw_networkx_edges(G,pos,width=4, edge_color='g', arrows=False)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Filter the subset of weigthed edges over a threshold"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"strong = [(u,v) for (u, v, d) in W.edges(data=True) if d['weight'] > 2]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 'D'), ('B', 'D')]\n"
]
}
],
"source": [
"print(strong)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Signed networks"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"S = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"S.add_edge('A', 'B', sign = '+')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"S.add_edge('A', 'C', sign = '-')"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{('A', 'B'): Text(0.49999933354753434, -0.08276893508159161, '+'),\n",
" ('A', 'C'): Text(-0.4998142645861261, 0.08273808168306745, '-')}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQG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sxBjxAjegaWjNkjR5p+ohp5lzeOT2w4AH1m9NP6bnp1pUDAPg8xoCHCA4I1j+7/FNf9/9a0aHRprnPd3yuhpMaakvKFgvbAQB8GWPAw/S4voeSEpLU/Nrmppm9aXvVYkoLvbfxPQ4bAACuGmPAA1UtU1Xf3/e9nmzxpGnmgvOCHl70sHp91ktpOWkWtgMA+BrGgIcKCgjSG53e0PwB8xUTFmOam/vLXMUlxmnj4Y0WtgMA+BLGgIfrVrubkkcmq9V1rUwzB9IPqNW0Vnp73dscNgAAXDbGgBe4Nuparbh3hZ5t/awMGQVmHE6HRi8ZrR6f9FDq2VSLGwIAvBljwEsE2gI1tsNYLR64WOVLlTfNzds5T/ZEu9YeXGthOwCAN2MMeJlONTtp+8jtal+tvWnmcMZhtf2wrV5f87qcLqeF7QAA3ogx4IUqRVbSkkFL9GLbF00PG+S58jRm2Rh1m9VNJ7JPWNwQAOBNGANeKsAWoJfavaSl9yxVxYiKprnFexbLPtGu7/d/b2E7AIA3YQx4uQ7VOyg5IVmdanQyzRzNOqoOMzro1e9fVZ4zz8J2AABvwBjwARUiKmjRwEUa236sbEbB/5c6XU69sPIF3TrzVh3LOmZxQwCAJ2MM+AibYdOzbZ7VyntXqnJkZdPcsn3
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pos = nx.spring_layout(S) # positions for all nodes\n",
"labels = nx.get_edge_attributes(S,'sign')\n",
"nx.draw_networkx(S, pos=pos, node_color='c',)\n",
"nx.draw_networkx_edges(S,pos,width=4, edge_color='g', arrows=False)\n",
"nx.draw_networkx_edge_labels(S,pos,edge_labels=labels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Representing other relationships"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"R = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"R.add_edge('A', 'B', relation = 'friend')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"R.add_edge('A', 'C', relation = 'father')"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"R.add_edge('A', 'D', relation = 'friend')"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{('A', 'B'): Text(-0.3527611634261536, 0.3731541229775912, 'friend'),\n",
" ('A', 'C'): Text(-0.1473141331865706, -0.4908557230159296, 'father'),\n",
" ('A', 'D'): Text(0.4999437033123544, 0.11724575282478678, 'friend')}"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7jklEQVR4nO3dd3iUVd7G8e9MeicEQhJa6EgvoYjYEQSx0MGKVHVXcdW1rWXdFVdlXVH0pSioKIgiKqKCIKgoIE2U3oUQEpIYIJ2UmXn/GIlCZlLI5JmZ5P5cFxdw5swzPxTy3DnPKSabzWZDREREajWzuwsQERER91MgEBEREQUCERERUSAQERERFAhEREQEBQIRERFBgUBEREQA34p0slqtJCcnExYWhslkqu6aRERExAVsNhvZ2dnExcVhNpc9BlChQJCcnEzjxo1dUpyIiIgY69ixYzRq1KjMPhUKBGFhYSUXDA8Pr3plIiIiUu2ysrJo3LhxyX28LBUKBGcfE4SHhysQiIiIeJmKPO7XpEIRERFRIBAREREFAhEREUGB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D/0nNfMJjMLhy4kNiwWs1n/i0VEpHy6W3i54e2Gs3niZi6qd1FJ25OXPUn/Fv2dTkA8a8+ePTz66KPcc889JCYmVnepIiLiwcq+Y4hXaFuvLZsmbmL8Z+PJLsjmn1f8s8z+//3vfzl16hTbt29nx44dJCQkEB8fzy+//ELHjs6PYxYRkZpLgaCGCPUPZdGwRRRaCrFYLfiYfZz2jY2N5eGHH6ZOnTrs2rWL2NhYli9fToMGDQysWER
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pos = nx.spring_layout(R) # positions for all nodes\n",
"labels = nx.get_edge_attributes(R,'relation')\n",
"friend_edges = [k for k,v in nx.get_edge_attributes(R, 'relation').items() if v == 'friend']\n",
"\n",
"edge_col = ['yellow' if not edge in friend_edges else 'green' for edge in R.edges()]\n",
"nx.draw_networkx(R, pos=pos, node_color='pink', )\n",
"nx.draw_networkx_edges(R,pos,width=4, edge_color=edge_col, arrows=False)\n",
"nx.draw_networkx_edge_labels(R,pos,edge_labels=labels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Multigraphs\n",
"\n",
"The same nodes can have several edges with different relationships."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"MG = nx.MultiGraph()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MG.add_edge('A', 'B', relation = 'friend', color = 'pink', weight = 1)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MG.add_edge('A', 'B', relation = 'neighbour', color = 'r', weight = 3)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MG.add_edge('A', 'C', relation = 'father', color = 'c', weight = 1)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MG.add_edge('A', 'D', relation = 'friend', color = 'b', weight = 2)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{0: {'relation': 'friend', 'color': 'pink', 'weight': 1},\n",
" 1: {'relation': 'neighbour', 'color': 'r', 'weight': 3}}"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MG.get_edge_data('A', 'B')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Drawing multigraphs require different methods"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAAGFCAYAAACCBut2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAu70lEQVR4nO3deXxcdb3/8fdMJluzZ7KcBEpJaQoUsKCUzQuy3AItlH1fG1kKCiIP4fq7gD5QqVe9qFwEAa2mBWRRkNUW20IVWcQKiNACIW1oaZKTNMk0WyfLZM7vj5OQJs3eSc7MnNfz8eDRfs+cOfPp41E673xXj2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n6QFWHQDAlPjoI+mYY6R//9tu+/12MDjtNGfrQowEgpWNjcP3DrS32xMIS0om9OyQpFWNjRMtDQAwRm++KX35y1LfIq9997V/njvySEfLQq+oDwStoZA2d3QMf8POnfav06ZN+DM2dXSoLTTeRYwAgLF6/nnppJOkpia7feihdhiYPdvRsrCLqA8Em4JBjbiVYl8Q6AsGE2BJqgwGJ/x+AMDwfv1rewJh3z+zJ50k/fWvEqu+o0vUB4LO0XZWTkuT8vKkkfYniMTnAADGxbLs1eDXXmvvNyBJl1wirVwpRcku+NhF1AeC5LEsRj3qKHva6oYNk/s5AIAxCYWkJUukO+/sv/atb0mPPCIlJTlWFkYQ9YFgVmqqRv2qvugiKSXF3vuyb4BqV9XV0lNPDft2T+/nAAD23M6d0rnn2kMFfX76U+nuuyVv1H/ruFfUb0yU7vNpZkqKNo00sXCvvaQ
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pos = nx.spring_layout(MG) # positions for all nodes\n",
"#pos = nx.circular_layout(MG)\n",
"\n",
"edges = MG.edges()\n",
"colors = []\n",
"weight = []\n",
"\n",
"for (u,v,attrib_dict) in list(MG.edges.data()):\n",
" colors.append(attrib_dict['color'])\n",
" weight.append(attrib_dict['weight'])\n",
"nx.draw_networkx(MG, pos, edgelist=edges, edge_color=colors, width=weight, node_color='c')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# References\n",
"* Networkx tutorial https://networkx.github.io/documentation/stable/tutorial.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
}