{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# lab 12\n", "\n", "Explore migration-related databy interpreting some of the metrics. Along are presented some forms of calculating relevant metrics.\n", "\n", "data: migracoes2015.csv\n", "\n", "You may use, for example:\n", "\n", "•\tSlides of the course\n", "\n", "•\thttps://networkx.github.io/documentation/stable/ (https://networkx.github.io/documentation/stable/\n", "\n", "•\thttps://www.analyticsvidhya.com/blog/2018/04/introduction-to-graph-theorynetwork-analysis-python-codes/ " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#libraries that you may use\n", "import numpy as np\n", "import pandas as pd\n", "import networkx as nx\n", "import matplotlib.pyplot as plt\n", "G = nx.DiGraph()\n", "\n", "G.add_edge(a,b, weight= c)\n", "\n", "for a,b,c in zip(df.origem, df.destino, df.numeros):\n", " G.add_edge(a,b, weight= c)\n", "\n", "G.degree(weight='weight')\n", "degree=G.degree()\n", "\n", "#dictionary with degrees\n", "degree_values = dict(degree).values()\n", "G.nodes\n", "G.edges\n", "\n", "#draw chart\n", "nx.draw(G, dim=100, with_labels=True)\n", "\n", "#or\n", "plt.figure(figsize=(20, 20))\n", "po1 = nx.spring_layout(G,scale=2)\n", "nx.draw(G, node_size=10, pos=po1, edge_color='y', label='r', with_labels=True)\n", "plt.show()\n", "\n", "#or for po1 and po1 = nx.random_layout(G)\n", "degree =[deg for node, deg in nx.degree(G)]\n", "max(degree)\n", "min(degree)\n", "kavg = np.mean(degrees)\n", "print(kavg)\n", "\n", "#convert directed to undirected\n", "G1 = G.to_undirected()\n", "Coeficiente de clustering\n", "nx.clustering(G1)\n", "\n", "#clioseness centrality\n", "nx.closeness_centrality(G)\n", "\n", "#eigenvector centrality\n", "nx.eigenvector_centrality(G)\n", "\n", "#degree centrality\n", "nx.degree_centrality(G)\n", "\n", "#number of linked components\n", "nx.number_connected_components(G1)\n", "\n", "#node linked to component\n", "nx.node_connected_component(G1, 'João')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }