Professional Documents
Culture Documents
Class: BE9
Roll no : 59
Batch : C
Experiment No. 07
Aim:
Develop social network analysis model for community detection/influence analysis.
Screenshots:
import pandas as pd
import networkx as nx
Mounted at gdrive
/content/gdrive
df=pd.read_csv("/content/gdrive/MyDrive/SMA/EXP 1/tnet_malawi_pilot2.csv")
df.head()
0 0 0 22 71 81
1 1 40 22 79 9
2 2 40 22 4 84
3 3 40 22 31 37
4 4 60 22 79 9
tm_graph=nx.from_pandas_edgelist(df,source="id1",target="id2")
type(tm_graph)
networkx.classes.graph.Graph
tm_graph.nodes()
NodeView((71, 81, 79, 9, 4, 84, 31, 37, 57, 2, 65, 34, 14, 78, 38, 68, 18, 59, 43, 11, 3, 54, 66, 23, 27, 36, 49, 45, 8, 51, 30,
12, 70, 50, 24, 82, 40, 60, 52, 67, 61, 76, 63, 15, 48, 28, 29, 1, 13, 53, 55, 62, 46, 26, 39, 64, 32, 16, 42, 86, 47, 58, 21, 41,
69, 19, 75, 44, 33, 6, 7, 80, 77, 73, 83, 22, 25, 5, 17, 74, 20, 85, 35, 56, 72, 10))
tm_graph.edges()
EdgeView([(71, 81), (71, 2), (71, 38), (71, 5), (71, 57), (71, 42), (71, 79), (71, 69), (71, 16), (71, 78), (71, 35), (71, 86),
(81, 82), (81, 52), (81, 12), (81, 23), (81, 29), (81, 63), (81, 6), (81, 84), (81, 3), (81, 70), (81, 15), (81, 46), (81, 25),
(81, 74), (81, 54), (81, 16), (81, 33), (81, 21), (81, 26), (81, 83), (81, 65), (81, 4), (81, 20), (81, 31), (81, 60), (79, 9),
(79, 47), (79, 86), (79, 69), (79, 1), (79, 7), (79, 64), (79, 5), (9, 7), (9, 45), (9, 49), (9, 39), (9, 16), (4, 84), (4, 70),
(4, 54), (4, 6), (4, 24), (4, 15), (4, 80), (4, 63), (4, 32), (4, 74), (84, 54), (84, 24), (84, 70), (84, 85), (84, 6), (84, 52),
(84, 80), (84, 63), (84, 22), (31, 37), (31, 32), (31, 22), (31, 73), (31, 65), (31, 59), (31, 83), (31, 17), (31, 63), (31, 18),
(37, 65), (37, 59), (37, 17), (37, 18), (37, 32), (37, 13), (57, 2), (57, 68), (57, 69), (57, 42), (57, 86), (57, 78), (57, 16),
(57, 38), (2, 68), (2, 69), (2, 42), (2, 78), (2, 47), (2, 49), (2, 16), (2, 5), (2, 86), (65, 1), (65, 33), (65, 59), (65, 20),
(34, 14), (34, 41), (34, 64), (34, 44), (14, 64), (14, 41), (14, 44), (78, 38), (78, 69), (78, 68), (78, 74), (78, 5), (78, 16),
(78, 86), (38, 16), (38, 5), (38, 68), (38, 69), (38, 86), (68, 33), (68, 69), (18, 59), (18, 39), (18, 17), (18, 3), (18, 32),
(18, 70), (18, 33), (18, 22), (18, 60), (18, 44), (59, 3), (59, 39), (59, 17), (59, 83), (59, 63), (59, 69), (59, 35), (59, 32),
(43, 11), (43, 73), (43, 82), (43, 40), (11, 73), (11, 40), (11, 60), (11, 82), (3, 39), (3, 17), (3, 48), (3, 1), (3, 83), (54,
63), (54, 70), (54, 6), (54, 15), (54, 48), (54, 52), (54, 85), (54, 33), (54, 29), (54, 74), (54, 24), (66, 23), (66, 25), (66,
12), (66, 52), (66, 16), (23, 12), (23, 52), (23, 16), (27, 36), (27, 30), (27, 28), (27, 50), (36, 30), (36, 50), (36, 28), (49,
45), (49, 61), (49, 76), (49, 55), (49, 62), (49, 16), (49, 29), (45, 61), (45, 76), (45, 62), (45, 16), (8, 51), (8, 67), (51,
67), (51, 16), (51, 1), (30, 50), (30, 28), (12, 70), (12, 52), (12, 39), (12, 86), (12, 24), (70, 63), (70, 6), (70, 48), (70,
15), (70, 52), (70, 33), (70, 85), (70, 29), (70, 74), (70, 24), (50, 28), (82, 60), (82, 40), (82, 73), (82, 16), (82, 86), (40,
60), (40, 26), (40, 73), (60, 73), (60, 29), (60, 16), (52, 6), (52, 33), (52, 26), (52, 21), (52, 83), (52, 85), (52, 63), (52,
74), (61, 76), (61, 7), (76, 7), (76, 62), (76, 5), (63, 48), (63, 15), (63, 83), (63, 29), (63, 1), (63, 33), (63, 80), (15, 48),
(15, 6), (15, 74), (48, 1), (28, 74), (28, 20), (29, 1), (29, 33), (29, 85), (29, 6), (29, 39), (29, 32), (29, 74), (29, 21), (1,
33), (1, 74), (1, 39), (1, 83), (1, 86), (1, 35), (1, 20), (1, 13), (1, 16), (13, 53), (13, 58), (13, 16), (53, 58), (53, 16),
(55, 62), (55, 16), (55, 69), (62, 69), (62, 16), (46, 26), (46, 21), (26, 21), (26, 22), (26, 16), (26, 74), (26, 56), (26, 33),
(26, 35), (26, 20), (39, 83), (39, 75), (39, 56), (39, 74), (39, 32), (64, 41), (64, 44), (64, 16), (32, 83), (32, 22), (32, 20),
(32, 17), (32, 69), (32, 33), (16, 42), (16, 35), (16, 69), (16, 33), (16, 17), (16, 86), (16, 20), (16, 85), (16, 74), (16, 58),
(42, 35), (42, 69), (42, 7), (42, 86), (86, 47), (86, 69), (86, 5), (86, 20), (47, 69), (41, 44), (41, 73), (69, 56), (69, 5),
(69, 35), (19, 75), (19, 25), (75, 25), (33, 74), (6, 77), (6, 25), (6, 85), (80, 77), (80, 83), (80, 74), (77, 85), (73, 20),
(83, 22), (83, 74), (22, 35), (74, 56), (72, 10)])
nx.draw(tm_graph, with_labels=True)
DegreeView({71: 12, 81: 26, 79: 9, 9: 6, 4: 11, 84: 11, 31: 11, 37: 7, 57: 9, 2: 11, 65: 7, 34: 4, 14: 4, 78: 10, 38: 8, 68: 6,
18: 12, 59: 12, 43: 4, 11: 5, 3: 8, 54: 14, 66: 5, 23: 5, 27: 4, 36: 4, 49: 9, 45: 6, 8: 2, 51: 4, 30: 4, 12: 8, 70: 16, 50: 4,
24: 5, 82: 8, 40: 6, 60: 8, 52: 15, 67: 2, 61: 4, 76: 6, 63: 15, 15: 8, 48: 6, 28: 6, 29: 14, 1: 16, 13: 5, 53: 3, 55: 4, 62: 6,
46: 3, 26: 12, 39: 12, 64: 6, 32: 13, 16: 31, 42: 8, 86: 15, 47: 4, 58: 3, 21: 5, 41: 5, 69: 18, 19: 2, 75: 3, 44: 5, 33: 14, 6:
11, 7: 5, 80: 6, 77: 3, 73: 8, 83: 12, 22: 7, 25: 5, 5: 8, 17: 7, 74: 17, 20: 9, 85: 8, 35: 8, 56: 4, 72: 1, 10: 1})
nx.degree(tm_graph,40)
{71: 0.1411764705882353,
81: 0.3058823529411765,
79: 0.10588235294117647,
9: 0.07058823529411765,
4: 0.12941176470588234,
84: 0.12941176470588234,
31: 0.12941176470588234,
37: 0.08235294117647059,
57: 0.10588235294117647,
2: 0.12941176470588234,
65: 0.08235294117647059,
34: 0.047058823529411764,
14: 0.047058823529411764,
78: 0.11764705882352941,
38: 0.09411764705882353,
68: 0.07058823529411765,
18: 0.1411764705882353,
59: 0.1411764705882353,
43: 0.047058823529411764,
11: 0.058823529411764705,
3: 0.09411764705882353,
54: 0.16470588235294117,
66: 0.058823529411764705,
23: 0.058823529411764705,
27: 0.047058823529411764,
36: 0.047058823529411764,
49: 0.10588235294117647,
45: 0.07058823529411765,
8: 0.023529411764705882,
51: 0.047058823529411764,
30: 0.047058823529411764,
12: 0.09411764705882353,
70: 0.18823529411764706,
50: 0.047058823529411764,
24: 0.058823529411764705,
82: 0.09411764705882353,
40: 0.07058823529411765,
60: 0.09411764705882353,
52: 0.1764705882352941,
67: 0.023529411764705882,
61: 0.047058823529411764,
76: 0.07058823529411765,
63: 0.1764705882352941,
15: 0.09411764705882353,
48: 0.07058823529411765,
28: 0.07058823529411765,
29: 0.16470588235294117,
1: 0.18823529411764706,
13: 0.058823529411764705,
53: 0.03529411764705882,
55: 0.047058823529411764,
62: 0.07058823529411765,
46: 0.03529411764705882,
26: 0.1411764705882353,
39: 0.1411764705882353,
64: 0.07058823529411765,
32: 0.15294117647058825,
sorted(nx.degree_centrality(tm_graph).values())
m_influential = nx.degree_centrality(G)
for w in sorted(m_influential,key=m_influential.get, reverse=True):
print(w,m_influential[w])
16 0.36470588235294116
81 0.3058823529411765
69 0.21176470588235294
74 0.2
70 0.18823529411764706
1 0.18823529411764706
52 0.1764705882352941
63 0.1764705882352941
86 0.1764705882352941
54 0.16470588235294117
29 0.16470588235294117
33 0.16470588235294117
32 0.15294117647058825
71 0.1411764705882353
18 0.1411764705882353
59 0.1411764705882353
26 0.1411764705882353
39 0.1411764705882353
83 0.1411764705882353
4 0.12941176470588234
84 0.12941176470588234
31 0.12941176470588234
2 0.12941176470588234
6 0.12941176470588234
78 0.11764705882352941
79 0.10588235294117647
57 0.10588235294117647
49 0.10588235294117647
20 0.10588235294117647
38 0.09411764705882353
3 0.09411764705882353
12 0.09411764705882353
82 0.09411764705882353
60 0.09411764705882353
15 0.09411764705882353
42 0.09411764705882353
73 0.09411764705882353
5 0.09411764705882353
85 0.09411764705882353
35 0.09411764705882353
37 0.08235294117647059
65 0.08235294117647059
22 0.08235294117647059
17 0.08235294117647059
9 0.07058823529411765
68 0.07058823529411765
45 0.07058823529411765
40 0.07058823529411765
76 0.07058823529411765
48 0.07058823529411765
28 0.07058823529411765
62 0.07058823529411765
64 0.07058823529411765
80 0.07058823529411765
11 0.058823529411764705
66 0.058823529411764705
23 0.058823529411764705
24 0.058823529411764705
Betweeness Centrality
pos=nx.spring_layout(G)
betCent=nx.betweenness_centrality(G,normalized=True, endpoints=True)
node_color=[20000.0 * G.degree(v) for v in G]
node_size=[v * 10000 for v in betCent.values()]
plt.figure(figsize=(20,20))
nx.draw_networkx(G, pos=pos, with_labels=False, node_color=node_color, node_size=node_size)
sorted(betCent, key=betCent.get, reverse=True)[:5]
Closeness Centrality
closeness_centrality=nx.centrality.closeness_centrality(G)
(sorted(closeness_centrality.items(), key=lambda item: item[1], reverse=True ))[:8]
[(16, 0.5707539353769677),
(81, 0.526279602750191),
(74, 0.4941893830703013),
(1, 0.4824229691876751),
(20, 0.4739594083247334),
(33, 0.47120383036935704),
(29, 0.4604946524064171),
(26, 0.4604946524064171)]
Clustering
nx.average_clustering(G)
0.5268929497437728
The clustering coefficient of a node is defined as the probability that two randomly selected friends of v are friends with each other . The
number of unique triangles in the network.
triangles_per_node=list(nx.triangles(G).values())
sum(triangles_per_node)
1323
Bridges
nx.has_bridges(G)
True
bridges=list(nx.bridges(G))
len(bridges)
local_bridges = list(nx.local_bridges(G,with_span=False))
len(local_bridges)
28
Showing bridges and local bridges in the neowork. The bridges can be seen with the blue color and the local brides with the green color. Black
edges are neither local bridges nor bridges.
plt.figure(figsize=(15,8))
nx.draw_networkx(G, pos=pos, node_size=10, with_labels=False,width=0.15)
nx.draw_networkx_edges(G, pos, edgelist=local_bridges, width=0.5, edge_color="green")
nx.draw_networkx_edges(G, pos, edgelist=bridges, width=0.5, edge_color="blue")
plt.axis("off")
(-1.137210345615767,
0.4439755419564071,
-0.33592969488294977,
0.2960460938343247)