{"id":24308,"date":"2021-11-26T21:07:00","date_gmt":"2021-11-26T21:07:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=24308"},"modified":"2023-02-16T19:56:46","modified_gmt":"2023-02-16T19:56:46","slug":"network-analysis-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/network-analysis-in-python","title":{"rendered":"Network Analysis in Python &#8211; A Complete Guide"},"content":{"rendered":"\n<p>An approach for evaluating, managing, and tracking processes of management and workflows are called network analysis. Moreover, data analysis helps in creating <a href=\"https:\/\/www.askpython.com\/python-modules\/networkx-package\" data-type=\"post\" data-id=\"18292\">graphical diagrams<\/a> of nodes and elements of the structure, but unlike a workflow, a network diagram examines the chronological series of events, objectives, and assignments, along with their timeframes and dependencies, and depicts them visually as a tree or as a table, such as in a Gantt chart.<\/p>\n\n\n\n<p>When developing a project plan, project leaders might need network analysis as it helps in dealing with the following factors:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Inter &#8211; dependence of tasks<\/li><li>The duration between actions and how they should be effectively buffered.<\/li><li>Start and end dates, first from earliest to one of the most current<\/li><li>Activity intervals<\/li><li>Developing the path for most important tasks and activities.<\/li><\/ul>\n\n\n\n<p>The network analysis method is commonly used within the design to the development phase, to enhance project control and make sure tasks are delivered on time and within budget.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to implement network analysis in Python<\/h2>\n\n\n\n<p>There are many ways of doing network analysis in Python. Moreover, many tools are available to plot network analysis graphs, but in this article, we will be specifically using networkx and <a href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\/python-matplotlib\" data-type=\"post\" data-id=\"3182\">matplotlib<\/a> as these are powerful network plotting tools.<\/p>\n\n\n\n<p>We will be understanding network plotting by using some user databases available online. In this example, we have fetched two 16th century based Chinese population records, that may have lived at the time the author of a famous novel lived, and we will try to create a graph of people that might have known him.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Let&#8217;s start by importing packages<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport networkx as nx\nimport matplotlib.pyplot as plt\n<\/pre><\/div>\n\n\n<p>Github link to extract databases: <a href=\"https:\/\/github.com\/vierth\/networkanalysis\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/github.com\/vierth\/networkanalysis\" rel=\"noreferrer noopener\">LINK<\/a><\/p>\n\n\n\n<p>There are multiple files in the git folder, but we will be needing only, &#8216;edges.tsv&#8217; and &#8216;nodes.tsv&#8217;. These word files contain all the historical data.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design12-1024x512.png\" alt=\"rawdata-csv.png\" class=\"wp-image-24310\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design12-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design12-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design12-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design12.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Raw data when extracted from .tsv file<\/figcaption><\/figure><\/div>\n\n\n\n<p>These historical databases are in .tsv file format. As you see in the above image, the data is scattered and unfiltered. To graph this data, we need to segregate it, so that the compiler can start reading the data easily. <\/p>\n\n\n\n<p><strong>The code below demonstrates how to load these files into a (get_data) method and segregate them as per our need.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef data_extraction(name_ofile):\n    # Here, data loading will be done through a context manager\n    with open(name_ofile, &#039;r&#039;, encoding=&#039;utf8&#039;) as rf:\n        # transform file into string and split along new line\n        filelines = rf.read().split(&quot;\\n&quot;)\n\n        # new line will be created at tab spaces\n        filedata = &#x5B;line.split(&quot;\\t&quot;) for line in filelines]\n\n        # picks the header\n        fileheader = filedata&#x5B;0]\n\n        # header gets deleted\n        filedata = filedata&#x5B;1:]\n\n    # return header and data\n    return fileheader, filedata\n\n# load data in from file\nheaderofnode, data_ofnode = data_extraction(&#039;nodes.tsv&#039;)\nheaderofedge, data_ofedge = data_extraction(&#039;edges.tsv&#039;)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/data-data1-1024x512.png\" alt=\"segregate-data.png\" class=\"wp-image-24312\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/data-data1-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/data-data1-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/data-data1-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/data-data1.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>The above image represents how the compiler starts to segregate the data after reading the above lines of code.<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating the graph and adding node information to<\/strong> <strong>it:<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nGraph = nxnas.Graph()\n\n# graph gets data of node added to it\nfor nxnode in data_ofnode:\n    # sequentially adding id, name, chinese name, and index year\n    Graph.add_node(int(nxnode&#x5B;0]), pname=nxnode&#x5B;1], chinese_name=nxnode&#x5B;2], year_inindex=int(nxnode&#x5B;3]))\n\n#  graph gets data of edge added to it\nfor nxedge in data_ofedge:\n    # sequentially adding node 1, node 2, kin, and label\n    Graph.add_edge(int(nxedge&#x5B;0]), int(nxedge&#x5B;1]), nxkin=nxedge&#x5B;2], nxlabel=nxedge&#x5B;3])\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding data metrics for the graph<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndegree_centrality = nxnas.degree_centrality(Graph)\ncloseness_centrality = nxnas.closeness_centrality(Graph)\nbetweenness_centrality = nxnas.betweenness_centrality(Graph)\n<\/pre><\/div>\n\n\n<p>Metrics are a wide variety of algorithms that are present in the networkx python package that lets you study your network. In this example, we&#8217;ve used three data metrics to plot our graph. Let&#8217;s understand their functions and purpose.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Degree centrality: The number of edges a node has.<\/li><li>Closeness_centrality: Finds the nodes with the slightest distance between them. Through this way the efficiecny of nodes to transfer data is measured.<\/li><li>Betweeness centrality: Finds shortest path.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Complete Code for Network Analysis in Python<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport networkx as nxnas\nimport matplotlib.pyplot as myplot\n\n# This function is employed to extract data from the .tsv files\ndef data_extraction(name_ofile):\n    # Here, a data loading will be done through a context manager\n    with open(name_ofile, &#039;r&#039;, encoding=&#039;utf8&#039;) as rf:\n        # transform file into string and split along new line\n        filelines = rf.read().split(&quot;\\n&quot;)\n\n        # new line will be created at tab spaces\n        filedata = &#x5B;line.split(&quot;\\t&quot;) for line in filelines]\n\n        # picks the header\n        fileheader = filedata&#x5B;0]\n\n        # header gets deleted\n        filedata = filedata&#x5B;1:]\n\n    # return header and data\n    return fileheader, filedata\n\n# load data in from file\nheaderofnode, data_ofnode = data_extraction(&#039;nodes.tsv&#039;)\nheaderofedge, data_ofedge = data_extraction(&#039;edges.tsv&#039;)\n\n# create graph object\nGraph = nxnas.Graph()\n\n# graph gets data of node added to it\nfor nxnode in data_ofnode:\n    # sequentially adding id, name, chinese name, and index year\n    Graph.add_node(int(nxnode&#x5B;0]), pname=nxnode&#x5B;1], chinese_name=nxnode&#x5B;2], year_inindex=int(nxnode&#x5B;3]))\n\n#  graph gets data of edge added to it\nfor nxedge in data_ofedge:\n    # sequentially adding node 1, node 2, kin, and label\n    Graph.add_edge(int(nxedge&#x5B;0]), int(nxedge&#x5B;1]), nxkin=nxedge&#x5B;2], nxlabel=nxedge&#x5B;3])\n\n# Data metrics for the graph\ndegree_centrality = nxnas.degree_centrality(Graph)\ncloseness_centrality = nxnas.closeness_centrality(Graph)\nbetweenness_centrality = nxnas.betweenness_centrality(Graph)\n\n# The process of depicting the graph\nnxnas.draw_spring(Graph)\nmyplot.show()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design14-1024x512.png\" alt=\"graph-output.png\" class=\"wp-image-24368\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design14-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design14-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design14-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/Untitled-design14.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Network Graph<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>This article provides a detailed explanation of network analysis graphs and how to plot them. We have learned how to plot network graphs for records available in public domains and draw out relations from them. We also learned about networkx metrics and how to invoke and use them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/networkx.org\/documentation\/stable\/reference\/algorithms\/centrality.html\" target=\"_blank\" rel=\"noopener\">Networkx metrics<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>An approach for evaluating, managing, and tracking processes of management and workflows are called network analysis. Moreover, data analysis helps in creating graphical diagrams of nodes and elements of the structure, but unlike a workflow, a network diagram examines the chronological series of events, objectives, and assignments, along with their timeframes and dependencies, and depicts [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":24370,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-24308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/24308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=24308"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/24308\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/24370"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=24308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=24308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=24308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}