Código.¶

In [32]:
# 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 [33]:
datos = pd.read_csv('segundo.csv', index_col=None)
In [34]:
datos.head(10)
Out[34]:
source target
0 Brian Fernando
1 Brian Dzain
2 Brian Misael
3 Fernando Brian
4 Fernando Dzain
5 Fernando Misael
6 Dzain Brian
7 Dzain Fernando
8 Dzain Misael
9 Misael Brian
In [35]:
relationship_df = pd.DataFrame(datos)
In [36]:
pd.set_option('display.max_rows', None)
relationship_df
Out[36]:
source target
0 Brian Fernando
1 Brian Dzain
2 Brian Misael
3 Fernando Brian
4 Fernando Dzain
5 Fernando Misael
6 Dzain Brian
7 Dzain Fernando
8 Dzain Misael
9 Misael Brian
10 Misael Fernando
11 Misael Dzain
12 Karen Estefania
13 Karen Marisol
14 Estefania Karen
15 Estefania Marisol
16 Marisol Estefania
17 Marisol Karen
In [37]:
relationship_df = pd.DataFrame(np.sort(relationship_df.values, axis = 1), columns = relationship_df.columns)
relationship_df
Out[37]:
source target
0 Brian Fernando
1 Brian Dzain
2 Brian Misael
3 Brian Fernando
4 Dzain Fernando
5 Fernando Misael
6 Brian Dzain
7 Dzain Fernando
8 Dzain Misael
9 Brian Misael
10 Fernando Misael
11 Dzain Misael
12 Estefania Karen
13 Karen Marisol
14 Estefania Karen
15 Estefania Marisol
16 Estefania Marisol
17 Karen Marisol
In [38]:
relationship_df["value"] = 1
relationship_df = relationship_df.groupby(["source","target"], sort=False, as_index=False).sum()
In [39]:
relationship_df.head(20)
Out[39]:
source target value
0 Brian Fernando 2
1 Brian Dzain 2
2 Brian Misael 2
3 Dzain Fernando 2
4 Fernando Misael 2
5 Dzain Misael 2
6 Estefania Karen 2
7 Karen Marisol 2
8 Estefania Marisol 2

Red social (Grafo)¶

In [40]:
# 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 [41]:
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 [42]:
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("r2.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.
r2.html
Out[42]:

Visualizar comunidades dentro de la red¶

In [43]:
# Importamos librerias necesarias para mostrar la comunidades en una red
import community as community_louvain
import community.community_louvain as community_louvain
In [44]:
communities = community_louvain.best_partition(G)
In [45]:
communities
Out[45]:
{'Brian': 0,
 'Fernando': 0,
 'Dzain': 0,
 'Misael': 0,
 'Estefania': 1,
 'Karen': 1,
 'Marisol': 1}
In [46]:
nx.set_node_attributes(G, communities, 'group')
In [47]:
com_net = Network(notebook = True, width="1366px", height="768px", bgcolor='#222222', font_color='white')
com_net.from_nx(G)
com_net.show("r2_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.
r2_communities.html
Out[47]:
In [ ]:
 
In [ ]:
 
In [ ]: