# 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('segundo.csv', index_col=None)
datos.head(10)
| 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 |
relationship_df = pd.DataFrame(datos)
pd.set_option('display.max_rows', None)
relationship_df
| 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 |
relationship_df = pd.DataFrame(np.sort(relationship_df.values, axis = 1), columns = relationship_df.columns)
relationship_df
| 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 |
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 | 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 |
# 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("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
# 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
{'Brian': 0,
'Fernando': 0,
'Dzain': 0,
'Misael': 0,
'Estefania': 1,
'Karen': 1,
'Marisol': 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("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