Social Network Analysis
Social Network Analysis
# To add a node
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(7)
G.add_node(9)
#visualize
nx.draw_networkx(G)
1
[3]: # Adding multiple nodes at a time
G1=nx.Graph()
G1.add_nodes_from(['A','B','C','D','E','F'])
#visualize
nx.draw_networkx(G1)
2
[4]: # Adding multiple edges at a time
G1.add_edges_from([('A','B'),␣
↪('C','A'),('B','D'),('D','A'),('F','E'),('E','C'),('D','F')])
#visualize
nx.draw_networkx(G1)
3
[5]: # To add an edge since the graph is undirected, the order of nodes in edges␣
↪doesn't matter
G.add_edge(1,2)
G.add_edge(3,1)
G.add_edge(2,4)
G.add_edge(4,1)
G.add_edge(9,1)
G.add_edge(1,7)
G.add_edge(2,9)
#visualize
nx.draw_networkx(G)
4
[6]: # Homework - Try to generate an adjacency matrix for the above.
#1 Node List
[1, 2, 3, 4, 7, 9]
#2 Edge List
[(1, 2), (1, 3), (1, 4), (1, 9), (1, 7), (2, 4), (2, 9)]
5
#visualize
nx.draw_networkx(G)
Node 3 is removed
[1, 2, 4, 7, 9]
[9]: G1.remove_node('E')
nx.draw_networkx(G1)
6
[ ]:
#visualize
nx.draw_networkx(G)
7
[11]: # To find number of nodes
n = G.number_of_nodes()
print("Number of Nodes")
print(n)
Number of Nodes
5
Number of Edges
8
5
The degree of Node 2
2
Neighbour list of Node 2
[4, 9]
# G is the Graph
close_centrality
9
0.2.3 3. Betweenness Centrality - Most connected(important) node
It is a measure of how often a node appears in the shortest path connecting two other
nodes.Betweenness centrality quantifies how many times a particular node comes in the shortest
chosen path between two other nodes.
This measure shows which nodes are ‘bridges’ between nodes in a network. It does this by identifying
all the shortest paths and then counting how many times each node falls on one.
For Graphs with a large number of nodes, the value of betweenness centrality is very high. So,
we can normalize the value by dividing with number of node pairs (excluding the current node).
For Directed Graphs, the number of node pairs are (|N|-1)(|N|-2), while for Undirected Graphs, the
number of node pairs are (1/2)(|N|-1)*(|N|-2).
# parameters normalized and endpoints ensure whether we normalize the value and␣
↪consider the endpoints respectively.
bet_centrality
[18]: 0.0
[19]: {1: 0, 2: 0, 4: 0, 7: 0, 9: 0}
#visualize
nx.draw_networkx(G)
10
0.4 Example - Symmetric Network
Below you see a network of Bollywood actors as nodes. They are connected with solid lines if they
have worked together in at least one movie.
The first network of actors that we created below is a symmetric network because the relationship
“working together in a movie” is a symmetric relationship. If A is related to B, B is also related to
A. Let us create the network we saw above in NetworkX.
We will be using the Graph() method to create a new network and add_edge() to add an edge
between two nodes.
[23]: display.Image("C:/Users/ramaleer/Desktop/python/Practical 4/image.png")
[23]:
11
Each network consists of:
• Nodes: The individuals whose network we are building. Actors in the above example.
• Edges: The connection between the nodes. It represents a relationship between the nodes of
the network. In our example, the relationship was that the actors have worked together.
[24]: # Graph = This class implements an undirected graph
12
[25]: #visualize what we have generated
nx.draw_networkx(G_symmetric)
13
[ ]:
[28]: print("List of all nodes we can go to in a single step from node 2: ",list(G.
↪successors(2)))
14
print("List of all nodes from which we can go to node 2 in a single step:␣
↪",list(G.predecessors(2)))
[31]: (574467, 2)
[32]: hero_network.describe()
15
top CAPTAIN AMERICA CAPTAIN AMERICA
freq 8149 8350
plt.figure(figsize = (20,20))
nx.draw(G, with_labels=True,node_size = 8)
plt.show()
16
[37]: # Degree Centrality
deg_centrality=nx.degree_centrality(G)
degcentral_node= max(deg_centrality, key=deg_centrality.get)
print("Degree Centrality :", degcentral_node)
# Closeness Centrality
close_centrality=nx.closeness_centrality(G)
closeness_node=max(close_centrality, key=close_centrality.get)
print("Closeness Centrality :", closeness_node)
# Between Centrality
bet_centrality = nx.betweenness_centrality(G, normalized = True, endpoints =␣
↪False)
17
betcentral_node=max(bet_centrality, key=bet_centrality.get)
print("Betweenness Centrality :",betcentral_node)
[ ]:
[38]: #nx.degree_centrality(G)
Global CC
18
[40]: 0.027260432414710115
Local CC
19
'STORM, CHILI': 0,
'THUNDERSTRIKE/ERIC K': 0,
'HULK DOPPELGANGER': 0,
'TUATARA/COMMANDER AR': 0,
'USAGENT/CAPTAIN JOHN': 0,
'TYPHON': 0,
'RODGERS, MARIANNE': 0,
'ULTIMO': 0,
'ANT-MAN/DR. HENRY J.': 0,
'BYRD, SEN. HARRINGTO': 0,
'AQUARIAN/WUNDARR': 0,
'SPIDER-MAN/PETER PAR': 0,
'CYCLOPS/SCOTT SUMMER': 0,
'VENUS/APHRODITE/VICT': 0,
'MOON KNIGHT/MARC SPE': 0,
'MR. FANTASTIC/REED R': 0,
'ARBOGAST, BAMBI': 0,
'BINARY/CAROL DANVERS': 0,
'SCARLET WITCH/WANDA ': 0,
'MIRAGE': 0,
'DOLLAR BILL': 0,
'DR. SPECTRUM/JOSEPH ': 0,
'HAWK': 0}
[42]: # Find the maximum clustering coefficient and its corresponding node
max_clustering_node = max(local_cc, key=local_cc.get)
max_clustering_node
dataset: https://www.kaggle.com/datasets/csanhueza/the-marvel-universe-social-
network/code?select=nodes.csv
blog article furthur reading : https://www.roxanne-euproject.org/news/blog/social-network-
analysis-for-criminology-in-roxanne
[ ]:
20