# 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
datos = pd.read_csv('sexto.csv', index_col=None)
datos.head(10)
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 |
relationship_df = pd.DataFrame(datos)
pd.set_option('display.max_rows', None)
relationship_df
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 |
relationship_df = pd.DataFrame(np.sort(relationship_df.values, axis = 1), columns = relationship_df.columns)
relationship_df
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 |
relationship_df["value"] = 1
relationship_df = relationship_df.groupby(["source","target"], sort=False, as_index=False).sum()
relationship_df.head(20)
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 |
# 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())
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()
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
# Importamos librerias necesarias para mostrar la comunidades en una red
import community as community_louvain
import community.community_louvain as community_louvain
communities = community_louvain.best_partition(G)
communities
{'Alexis': 0, 'Ceci': 0, 'Vianey': 0, 'Mago': 0, 'Aldahir': 1, 'David': 1, 'Israel': 0, 'Ramon': 1, 'Naye': 1}
nx.set_node_attributes(G, communities, 'group')
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