Thursday, February 29, 2024
HomePythonThree methods of network analysis using Python libraries! Introducing supplementary information!

# Three methods of network analysis using Python libraries! Introducing supplementary information!

## Two libraries for Python network analysis

Python can perform various simulations, but did you know that not only engineers but also beginners can easily perform network analysis?

Network analysis helps you isolate, manage, and plan future actions.This time, let’s draw a graph casually using the NetworkX and Matplotlib libraries.

### Library for network analysis 1: Matplotlib

Matplotlib and NetworkX are also characterized by being able to draw graphs in cooperation.

Matplotlib is a library for drawing line graphs, bar graphs, 3D graphs, etc.It is widely applied in the medical field, nature observation, disaster simulation, etc.

Installation is available for download from https://matplotlib.org/. It’s open source, so you don’t have to worry about pricing.

### Network analysis library 2: NetworkX

As the name suggests, NetworkX has a function library for generating network structures and dynamics.

This is also open source, so the price is free. Installation is OK if you use the pip command and import as pip install networkx. If no error has occurred, the connection confirmation has been completed and the installation should have been successful.

## Two merits of network analysis using Python library

One of the major advantages of Python is that it enables network analysis through programming.Data exchange can also be performed on one program, so efficient work can be expected without analog work.

Another advantage is that the Python source code itself is easy to write and read. The fact that the code is the same regardless of who writes it also makes analysis easier.

### Merit 1 of network analysis: Centrality analysis is possible

Nodes (knots) displayed on the graph also have degrees of importance, and high analytical power is required to find nodes with high importance.

In this regard, Python’s NetworkX implements a function called PageRank(), and it is possible to express the importance of a node according to the score value.Specifically, they can be identified by their color depth and size.

### Network analysis advantage 2: Networks can be visualized in graphs and diagrams

Even if the network is analyzed, it takes time to decipher it, which is a big disadvantage in terms of work efficiency.

In that respect, Python’s Matplotlib converts the results of network analysis into graphs and figures and visualizes them, so readers can always receive easy-to-read results.

## Three ways to analyze networks using Python libraries

Now, after installing the two Python libraries, NetworkX and Matplotib, let’s take a look at how the work progresses, and specifically introduce the network analysis method in Python.

As for the order, after setting NetworkX, we will combine with Matplotlib.First, let’s write import networkx as nx.

### Network analysis method 1: NetworkX configuration

Building a NetworkX graph can be expressed as G = nx.Graph().Initialize the graph with G = nx.Graph () and call various methods on the generated variable G.

Vertices (nodes) and edges (edges) apply to specific methods, and are described as follows.

Let’s quickly check the source code of the NetworkX node of the graph.

The vertex is G.add_node().Alphanumeric characters and strings such as ‘a’, ‘b’, and ‘c’ are entered in parentheses. This is because they have no special meaning and serve only as labels.

Edges are also written as G.add_edge().The concept of addition and deletion in () is the same as that of a node, but since an edge is literally a line that connects nodes, there will be two nodes listed in ().

For example, if you write G.add_node(1) and G.add_node(2), the edge will be G.add_edge(1, 2). This will render the 1 and 2 nodes and the edge connecting them when rendering.

If you want to read a file that shows the connections between nodes, if the link name to read the external file is edge.txt, G = nx.read_edgelist(“edge.txt”, nodetype=int).

When writing the above, please specify the int type firmly and make the result an easy-to-read graph representation.

### Network Analysis Method 3: Combining with Matplotlib

Assuming that Matplotlib is already installed, write import matplotlib.pyplot as plt in advance.

Matplotlib’s library comes into play at the visualization stage after building the graph, the code is draw_networkx_nodes() and draw_networkx_edges().Here is the code to draw each of the edges and nodes we set up earlier.

## Two supplements of the network analysis method using the Python library

It’s not a technical part, but from the point of view of what I’ve noticed, there are some tricks to using Python libraries for network analysis.If you keep the tricks down, it is guaranteed that the accuracy of the graph and the visual appeal will be improved.

Both NetworkX and Matplotlib use functions to easily express graphs, but please keep this in mind for more efficient operation.

### Network analysis method supplement 1: Let’s change the node color depending on the value

As I touched on centrality analysis briefly, in order to make better use of the Python libraries NetworkX and Matplotlib, it is important to emphasize visualization.

For example, let’s assume that there is one value that you want to change the color of the node among several.It can be reflected by passing a list of colors corresponding to nodes to the node_color parameter of draw_networkx () by conditional branching.

### Network Analysis Method Supplement 2: Prepare more sample data

The large amount of sample data has little to do with the Python library, which is the theme of this article, but since it is a theory in the first place, I will add it.

The larger the data sample, the larger the denominator and the higher the accuracy. 10 to 100, 100 to 1000, 1000 to 10000, and so on.

Depending on the subject of analysis, analysis based on as much data collection as possible can be said to be a more credible network analysis.

## Five terms to remember when programming for network analysis

Finally, there are some terms that should be remembered at the very least when programming for network analysis is put into practice. When asked about the meaning of terms that we are used to hearing, we often find it difficult to answer.

Internet terminology is very important for understanding concepts.Please keep in mind how it is related to the network, including a review of terminology.

### Term 1 to remember when programming: IP address

An IP address is an identification number for information equipment and is assigned to almost all network equipment. Numbers are separated by dots and are usually written in quadruple notation.Network analysis is powerful because it identifies the range of behavior that purchased the service by analyzing the IP address.

As an aside, more than 4 billion IP addresses have already been allocated, and the problem of IP address depletion due to population growth and increasing user population is being talked about.

### Term 2 to remember when programming: Sockets

A socket has a meaning as a connection port that connects a program and a network on a network.If you imagine the socket at the end of a light bulb, it may be relatively easy to understand because it corresponds to the connection part.

In addition, programming languages ​​have socket functions related to network communication, so they are also used in this sense. However, there are many cases where it is used in a broader sense than Python’s network analysis, so caution is required.

### Term 3 to remember when programming: Server

A server is a computer that provides services. For example, when accessing the web from a computer or smartphone, a request is sent from the user side.

By receiving this request and responding, you can see the information of the access destination.This set of counterparts is the server.

By the way, programming languages ​​such as Java and PHP are famous as server-side languages.

### Term 4 to remember when programming: Client

In network communication, the client refers to the side of the browser that sends a request to the server.

In the above-mentioned server side, the server executes program processing and sends a response to the client side, whereas the client side receives the server’s response and executes the program processing itself.

### Term 5 to Remember When Programming: Port Numbers

In TCP/IP communication, the IP address identifies the network device, while the port number plays the role of identifying which computer program the communication packet is given to.

Port numbers are assigned to both the client side and the server side. While the port number on the client side changes each time for security reasons, the port number on the server side is determined by the content of access.

## Let’s program network analysis in the Python environment

Python is a programming language with a high future potential, and we can expect more and more users to use it in the future. Even now, there is a lot of information available in books such as reference books and on the Internet, so it can be said that it is suitable for starting seriously with the aim of mastering.

The Python library, which is characterized by its richness and simple code, has many tools in the field of network analysis and is easy to use once you get used to it.You will see more and more content in the future.

RELATED ARTICLES