{ "cells": [ { "cell_type": "markdown", "id": "d59ad4a5", "metadata": {}, "source": [ "# Lab 04\n", "\n", "\n", "The file https://raw.githubusercontent.com/masterfloss/data/main/migracoes2015.csv has information related to the migratory stock in 2015. It shows the number of people initially from a country (origin) living in another country (destination).\n", "1.\tcomplete comments \n", "2.\timprove outputs so that users may know the results' meaning\n", "3.\tinterpret the results\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1c8e115e", "metadata": {}, "outputs": [], "source": [ "#\n", "# inorder to use pyvis library, you need to install:\n", "# !pip install pyvis \n", "import pandas as pd\n", "import networkx as nx\n", "from pyvis.network import Network" ] }, { "cell_type": "code", "execution_count": null, "id": "93897c10", "metadata": {}, "outputs": [], "source": [ "#\n", "df =pd.read_csv('https://raw.githubusercontent.com/masterfloss/data/main/migracoes2015.csv',sep=\";\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "d3f33694", "metadata": {}, "outputs": [], "source": [ "#\n", "newdf=df[df['population']>500000]" ] }, { "cell_type": "code", "execution_count": null, "id": "a7ec7bc7", "metadata": {}, "outputs": [], "source": [ "#\n", "G=nx.Graph()\n", "G=nx.from_pandas_edgelist(newdf, 'origin', 'destination',['population'])\n", "\n", "net=Network(notebook=True)\n", "net.from_nx(G)\n", "net.width=1000\n", "net.height=1000\n", "net.show('example.html')" ] }, { "cell_type": "code", "execution_count": null, "id": "1db229ba", "metadata": {}, "outputs": [], "source": [ "#\n", "print(G['Portugal']['France']['population'])\n", "print(G['France']['Portugal']['population'])" ] }, { "cell_type": "code", "execution_count": null, "id": "3b1ba1e9", "metadata": {}, "outputs": [], "source": [ "G1 = nx.from_pandas_edgelist(newdf,'origin', target='destination',edge_attr=['population'],create_using=nx.DiGraph())\n", "\n", "net=Network(notebook=True,directed=True)\n", "net.from_nx(G1)\n", "net.width=1000\n", "net.height=1000\n", "net.show('example.html')" ] }, { "cell_type": "code", "execution_count": null, "id": "16c56674", "metadata": {}, "outputs": [], "source": [ "# \n", "dc=nx.degree_centrality(G1)\n", "print(max(dc, key=dc.get))\n", "#\n", "idc=nx.in_degree_centrality(G1)\n", "print(max(idc, key=idc.get))\n", "#\n", "odc=nx.out_degree_centrality(G1)\n", "print(max(odc, key=odc.get))\n", "#\n", "print(nx.degree(G1)['United States of America'])\n", "#\n", "print(G1.in_degree()['United States of America'])\n", "#\n", "print(G1.out_degree()['United States of America'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c3bf5c37", "metadata": {}, "outputs": [], "source": [ "#\n", "eig=nx.eigenvector_centrality(G1)\n", "print(max(eig, key=eig.get))\n", "#\n", "bitCent=nx.betweenness_centrality(G1)\n", "print(max(bitCent, key=bitCent.get))\n", "#\n", "bitCent=nx.closeness_centrality(G1)\n", "print(max(bitCent, key=bitCent.get))" ] }, { "cell_type": "code", "execution_count": null, "id": "ae0dbd1b", "metadata": {}, "outputs": [], "source": [ "#\n", "print(nx.clustering(G1))" ] }, { "cell_type": "code", "execution_count": null, "id": "ef3401f2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "badd4ce1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }