Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python open source tools for graph clustering

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use Python open source tools for image clustering". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Recently, another Python visualization tool has become popular.

This time, the function is to detect and visualize the community structure of graph clustering.

As soon as the project's post was posted on reddit, it topped the list of the "machine learning section".

Let's see what it can be used to do.

Functional highlight

This tool, called communities, is a Python library used to detect the community structure of graph clustering problems.

It supports a variety of algorithms, including:

Louvain algorithm

Girvan-Newman algorithm

Hierarchical clustering algorithm

Spectral clustering algorithm

Bron-Kerbosch algorithm.

Even better, communities can also visualize these algorithms.

Learn more about it.

Import the algorithm and insert the matrix

Here, take Louvain algorithm as an example.

This is a community discovery algorithm based on modularity, but also a greedy algorithm.

It arranges the vertices into a community structure according to the shared edges of the vertices, that is, it divides the nodes into several communities, and each community shares few connections, but the nodes in the same community share many connections.

Finally, the whole social network presents a structure of module aggregation to maximize the modularity of the whole social network.

So first of all, we need to construct an adjacency matrix representing an undirected graph, which can be weighted or unweighted, and the matrix is a 2Dnumpy array.

The noun matrix indicates that there are n nodes, and each position of the matrix represents the edge relationship between the nodes, 1 if there is an edge, and 0 if there is no edge.

Then, simply import the algorithm from communities.algorithms and insert the matrix.

Import numpy as np from communities.algorithms import louvain_method adj_matrix = np.array ([0,1,1,0,0,0], [1,0,1,0,0,0], [1,1,0,1,0,0], [0,0,1,0,1,1], [0] 0,0,1,0,1], [0,0,0,1,0]) communities, _ = louvain_method (adj_matrix) # > > [{0,1,2}, {3,4,5}]

Next, output a list of communities, each of which is a set of nodes.

Visualization and color coding

Use communities to visualize the image, divide the nodes into the community and carry out color coding, you can also choose dark or light background, save the picture, choose the resolution of the picture, and so on.

Draw_communities (adj_matrix: numpy.ndarray, communities: list, dark: bool = False, filename: str = None, seed: int = 1)

The specific meanings of the parameters are:

Adj_matrix (numpy.ndarray): adjacency Matrix of graphs

Dark (bool, optional (default=False)): if True, the drawing has a dark background, otherwise a light background

Filename (str or None, optional (default=None)): you can save the diagram in PNG format through the filename path; set None to display the diagram interactively.

Dpi (int or None, optional (default=None)): dots per inch to control the resolution of the image

Seed (int, optional (default=2)): random seeds.

For the visualization of the Louvain algorithm, the code is as follows:

From communities.algorithms import louvain_method from communities.visualization import draw_communities adj_matrix = [...] Communities, frames = louvain_method (adj_matrix) draw_communities (adj_matrix, communities)

Animation rendering algorithm

Communities can also animate the process of node assignment to a community.

Louvain_animation (adj_matrix: numpy.ndarray, frames: list, dark: bool = False, duration: int = 15, filename: str = None, dpi: int = None, seed: int = 2)

The meanings of the parameters are as follows:

Adj_matrix (numpy.ndarray): adjacency Matrix of graphs

Frames (list): dictionary list of each iteration of the algorithm

Each dictionary has two keys: "C" contains a node-to-community lookup table, and "Q" represents the modularity value of the graph.

This dictionary list is the second return value of louvain_method

Dark (bool, optional (default=False)): if True, the animation is dark background and color scheme, otherwise light color scheme

Duration (int, optional (default=15)): the duration required for animation in seconds

Filename (str or None, optional (default=None)): save animation as GIF; through filename path and set None to display animation interactively

Dpi (int or None, optional (default=None)): dots per inch, which controls the resolution of the animation

Seed (int, optional (default=2)): random seeds.

For example, the animation of the Louvain algorithm in the karate club network:

From communities.algorithms import louvain_method from communities.visualization import louvain_animation adj_matrix = [...] Communities, frames = louvain_method (adj_matrix) louvain_animation (adj_matrix, frames)

We can see the dynamic process of the Louvain algorithm:

First scan all the nodes in the data and treat each node as an independent community

Next, traverse the neighbor nodes of each node to determine whether to add the node to the community where the neighbor node is located to improve the modularity

This process iterates repeatedly until the community membership of each node is stable.

Finally, all the nodes in the same community are compressed into a new node, and the weight of the new node is calculated until the modularity of the whole graph is stable.

You can try other algorithms by yourself through the link at the end of the article.

This is the end of the content of "how to use Python open source tools for map clustering". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report