Código.¶

In [64]:
# Importamos las librerias y dependencias necesarias.
import pandas as pd
import numpy as np
import spacy
from spacy import displacy
import networkx as nx
import matplotlib.pyplot as plt

Los datos están almacenados en un archivo .csv, en donde se especifica cada relación que existe en el grupo que se esta analizando.¶

In [65]:
datos = pd.read_csv('sexto.csv', index_col=None)
In [66]:
datos.head(10)
Out[66]:
source target
0 Alexis Ceci
1 Alexis Vianey
2 Alexis Mago
3 Aldahir David
4 Aldahir Israel
5 Aldahir Ramon
6 Aldahir Naye
7 Ceci Alexis
8 Ceci Vianey
9 Ceci Aldahir
In [67]:
relationship_df = pd.DataFrame(datos)
In [68]:
pd.set_option('display.max_rows', None)
relationship_df
Out[68]:
source target
0 Alexis Ceci
1 Alexis Vianey
2 Alexis Mago
3 Aldahir David
4 Aldahir Israel
5 Aldahir Ramon
6 Aldahir Naye
7 Ceci Alexis
8 Ceci Vianey
9 Ceci Aldahir
10 David Aldahir
11 David Israel
12 David Mago
13 David Ramon
14 David Naye
15 Israel Alexis
16 Israel Aldahir
17 Israel Ceci
18 Israel David
19 Israel Mago
20 Israel Ramon
21 Israel Naye
22 Israel Vianey
23 Mago Alexis
24 Mago David
25 Mago Israel
26 Mago Ramon
27 Ramon Alexis
28 Ramon Aldahir
29 Ramon David
30 Ramon Israel
31 Ramon Mago
32 Ramon Naye
33 Ramon Vianey
34 Naye Aldahir
35 Naye David
36 Naye Israel
37 Naye Ramon
38 Vianey Alexis
39 Vianey Ceci
40 Vianey Israel
41 Vianey Ramon
In [69]:
relationship_df = pd.DataFrame(np.sort(relationship_df.values, axis = 1), columns = relationship_df.columns)
relationship_df
Out[69]:
source target
0 Alexis Ceci
1 Alexis Vianey
2 Alexis Mago
3 Aldahir David
4 Aldahir Israel
5 Aldahir Ramon
6 Aldahir Naye
7 Alexis Ceci
8 Ceci Vianey
9 Aldahir Ceci
10 Aldahir David
11 David Israel
12 David Mago
13 David Ramon
14 David Naye
15 Alexis Israel
16 Aldahir Israel
17 Ceci Israel
18 David Israel
19 Israel Mago
20 Israel Ramon
21 Israel Naye
22 Israel Vianey
23 Alexis Mago
24 David Mago
25 Israel Mago
26 Mago Ramon
27 Alexis Ramon
28 Aldahir Ramon
29 David Ramon
30 Israel Ramon
31 Mago Ramon
32 Naye Ramon
33 Ramon Vianey
34 Aldahir Naye
35 David Naye
36 Israel Naye
37 Naye Ramon
38 Alexis Vianey
39 Ceci Vianey
40 Israel Vianey
41 Ramon Vianey
In [70]:
relationship_df["value"] = 1
relationship_df = relationship_df.groupby(["source","target"], sort=False, as_index=False).sum()
In [71]:
relationship_df.head(20)
Out[71]:
source target value
0 Alexis Ceci 2
1 Alexis Vianey 2
2 Alexis Mago 2
3 Aldahir David 2
4 Aldahir Israel 2
5 Aldahir Ramon 2
6 Aldahir Naye 2
7 Ceci Vianey 2
8 Aldahir Ceci 1
9 David Israel 2
10 David Mago 2
11 David Ramon 2
12 David Naye 2
13 Alexis Israel 1
14 Ceci Israel 1
15 Israel Mago 2
16 Israel Ramon 2
17 Israel Naye 2
18 Israel Vianey 2
19 Mago Ramon 2

Red social (Grafo)¶

In [72]:
# Create a graph from a pandas dataframe
G = nx.from_pandas_edgelist(relationship_df, 
                            source = "source", 
                            target = "target", 
                            edge_attr = "value", 
                            create_using = nx.Graph())
In [73]:
plt.figure(figsize=(10,10))
pos = nx.kamada_kawai_layout(G)
nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues, pos = pos)
plt.show()
In [74]:
from pyvis.network import Network
net = Network(notebook = True, width="1366px", height="768px", bgcolor='#222222', font_color='white')

node_degree = dict(G.degree)

#Setting up node size attribute
nx.set_node_attributes(G, node_degree, 'size')

net.from_nx(G)
net.show("r6.html")
Warning: When  cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.
r6.html
Out[74]:

Visualizar comunidades dentro de la red¶

In [75]:
# Importamos librerias necesarias para mostrar la comunidades en una red
import community as community_louvain
import community.community_louvain as community_louvain
In [76]:
communities = community_louvain.best_partition(G)
In [77]:
communities
Out[77]:
{'Alexis': 0,
 'Ceci': 0,
 'Vianey': 0,
 'Mago': 0,
 'Aldahir': 1,
 'David': 1,
 'Israel': 0,
 'Ramon': 1,
 'Naye': 1}
In [78]:
nx.set_node_attributes(G, communities, 'group')
In [79]:
com_net = Network(notebook = True, width="1366px", height="768px", bgcolor='#222222', font_color='white')
com_net.from_nx(G)
com_net.show("r6_communities.html")
Warning: When  cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.
r6_communities.html
Out[79]:
In [ ]:
 
In [ ]:
 
In [ ]: