Original link:tecdat.cn/?p=7295

Original source:Tuo End number according to the tribe public number

 

In this article, I examine communities in social networks using R and Python.

 

 

 

 

Build a network

Kaggle’s data was in 110.egonet files (corresponding to 110 anonymous Facebook users), each containing his friends’ networks.

Let’s focus on file 0.egonet, which contains all the information about user 0’s network. Each line of the file is a list of the friends of the first user in that line who are directly part of the network.

 

1234 1: 146 189 229 201 204… 2: 146 191 229 201 204… 3: 185 80 61 188 22 222… 4: 72 61 187 163 177 138…

 

Below, I attach Python code to access each egonet file and build a list of nodes and edges of the Networkx constructor. After the graph is built, its adjacency matrix is computed and saved in a CSV file.

 

import networkx as nx from os import listdir from os.path import isfile, Join from sklearn.cluster import KMeans def load_egonet_files(PATH): """ Given the path of the. Egonet file, a list containing all files is returned. """ onlyfiles = [fyle for fyle in listdir(path) if fyle.endswith('.egonet')] return onlyfiles # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Copy the code

 

The result of the provided code is 110 CSV files containing an adjacency matrix for each self-network graph.

 

Detection of the community

First, let’s draw a graph to see what it looks like before the community cluster detection. Below the R code, load the data from the CSV file, build the network (we used 0.egonet) and draw.

Dat = read-.csv ('graph-0.csv', header=TRUE, row.names=1, Check.names =FALSE) m = as.matrix(dat) # Build graph from adjacency matrix g = graph.adjacency(m,mode="undirected",weighted=NULL) # Draw graphCopy the code

 

  

R provides several powerful community detection algorithms.

Modularity is essentially the score of the edges that belong to a given group minus the expected score if the edges are randomly distributed. So the higher the better.

Here, you can find the results on the User 0 network.

 

> Modularity (wc) [1] 0.4629543 > Modularity (WC) [1] 0.4463902 > Modularity (WC) [1] 0.4330911 > modularity(WC) [1] 0.4649535 > Modularity (WC) [1] 0.4511259 > Modularity (WC) [1] 0.4314803Copy the code

 

Spinglass.com MUnity algorithm is the best algorithm, and its modularity is 0.4649. It turns out that communities can be found in small self-social networks.

Below, you can also find a good visualization of the detected clusters in R.


Most welcome insight

1. All acupoints were analyzed by WEB complex network of SPS-Modeler

2. Community detection in social networks using R language and Python

3.R language text mining NASA data Network analysis, TF-IDF and topic modeling

4. Epidemic modeling using airline complex networks in R language

5. Python membership graph model Detection of dense overlapping communities in model-based networks

6. Analyze social networks using Python and SAS Viya

7. Association network analysis: Data portraits of migrants who have moved out of Beijing

8. Emotional Semantic network: Travel data perception of tourist destination image

9. Use association rule data mining to explore the rules of drug compatibility