Skip to content

Instantly share code, notes, and snippets.

View manekgarg's full-sized avatar

Manik Garg manekgarg

  • Berlin, Germany
View GitHub Profile
# Attribute enrichment of the nodes
node_attr = pd.read_csv('/content/quakers_nodelist.csv', index_col = 'Name')
node_attr
# Add the atribute to the graph G
node_attr_d = node_attr.to_dict(orient = 'index')
nx.classes.function.set_node_attributes(G, node_attr_d)
nx.get_node_attributes(G, 'age')
# Attribute enrichment of the nodes
node_attr = pd.read_csv('/content/quakers_nodelist.csv', index_col = 'Name')
node_attr
# Add the atribute to the graph G
node_attr_d = node_attr.to_dict(orient = 'index')
nx.classes.function.set_node_attributes(G, node_attr_d)
nx.get_node_attributes(G, 'age')
pr = nx.pagerank(G)
# Plot the the graph again with Page Rank
plt.figure(figsize=(25,18))
nx.draw(G, edge_color = '#ced7d9',width = 4, arrowsize = 30, node_color = c_values, font_size =8,
alpha = .5, with_labels= True, node_size = [10000*pr[n] for n in G.nodes()])
bc = nx.centrality.betweenness.betweenness_centrality(G)
bc
# Plot the the graph again with the Between Centrality
plt.figure(figsize=(25,18))
nx.draw(G, edge_color = '#ced7d9',width = 4, arrowsize = 30, node_color = c_values, font_size =8,
alpha = .5, with_labels= True, node_size = [10000*bc[n] for n in G.nodes()])
# Import the packages
import community
from importlib import reload
# reload(community)
# or use Girvan-Newman algorithm
# Assgin the community function to c_values
communities = community.best_partition(G.to_undirected(), resolution = .75)
c_values = [communities.get(node) for node in G.nodes()]
# Plot the the graph again with community using c_values as color
# Add information of people
plt.figure(figsize=(22,16))
nx.draw(G, edge_color = '#ced7d9',width = 4, arrowsize = 30, with_labels= True)
G = nx.from_pandas_edgelist(df,
source='Source',
target='Target',
create_using=nx.DiGraph()
)
print(nx.info(G))
# Check the closeness of the network
nx.average_clustering(G)
@manekgarg
manekgarg / prepare_data.py
Created May 23, 2020 17:45
Prepare the basic graph and df for the analysis
# Create an empty graph
G = nx.Graph()
# Import the dataset as a dataframe
df = pd.read_csv('/content/edgelist.csv')
# Check the dimension of the data set
print(df.shape)
df
@manekgarg
manekgarg / import.py
Last active May 23, 2020 11:35
import.py
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
#from utils.pos import pos
from IPython.display import Image
pip install networkx
pip install IPython