# Start writing code here...
!pip install networkx==2.6.3
import csv
from operator import itemgetter
import networkx as nx
from networkx.algorithms import community
Collecting networkx==2.6.3
Downloading networkx-2.6.3-py3-none-any.whl (1.9 MB)
|████████████████████████████████| 1.9 MB 27.6 MB/s
Installing collected packages: networkx
Successfully installed networkx-2.6.3
WARNING: You are using pip version 20.1.1; however, version 22.0.3 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
with open('node.csv', 'r') as nodecsv:
nodereader = csv.reader(nodecsv) # Read the node
nodes = [n for n in nodereader][1:]
node_id = [n[0] for n in nodes] # 节点id是唯一标识
with open('edge.csv', 'r') as edgecsv:
edgereader = csv.reader(edgecsv) # Read the edge
edges = [tuple(e) for e in edgereader][1:] # Retrieve the data
edges
print(len(node_id))
819
print(len(edges))
3334
G = nx.Graph() # Initialize a Graph object
G.add_nodes_from(node_id) # Add nodes to the Graph
G.add_edges_from(edges) # Add edges to the Graph
print(nx.info(G)) # Print information about the Graph
Graph with 236 nodes and 1859 edges
# Create an empty dictionary for each attribute
cp_dict = {}
lb_dict = {}
for node in nodes: # Loop through the list of nodes, one row at a time
cp_dict[node[0]] = node[1]
lb_dict[node[0]] = node[2]
# Add each dictionary as a node attribute to the Graph object
nx.set_node_attributes(G, cp_dict, 'caipin')
nx.set_node_attributes(G, lb_dict, 'label')
# # Loop through each node, to access and print all the "birth_year" attributes
# for n in G.nodes():
# print(n, G.nodes[n]['caipin'])
for n in G.nodes():
print(n, G.nodes[n]['label'])
0 3-yellow chick
1 honey
2 Chinese prickly ash
3 octagon
4 salt
5 spices
6 shrimps
7 Green Chinese onion
8 Ginger slice
9 flour
10 garlic
11 sugar
12
13 The sea cucumber
14 scallion
15 oil
16 wine
17 The soy sauce
19 Clear soup
20 pork
21 cucumber
22 egg
23 Black fungus
26 starch
27 Vegetable oil
28 The streaky
32 Rock sugar
33 cinnamon
34 The carp
36 vinegar
37 ketchup
40 starch
41 Salad oil
42 Sesame oil
46 tofu
47 Chicken breast
48 Shrimps.
50 The side dishes
52 Pig head
53 Chicken gizzard
55 Refined salt
60 Five-spice powder
63 Sweet bean sauce
64 Bean paste
66 water
67 Pork tenderloin
72 Black pepper
74 loin
76 carrot
77 The old pump
78 Balsamic vinegar
82 Spring Onions
83 Chinese cabbage
87 Peanut oil
92 Monosodium glutamate
93 apple
98 Sweet ChunYa
102 Long eggplant
103 Green red pepper
104 cloves
105 parsley
108 Chicken essence
111 Light soy sauce
113 potatoes
114 Yellow pepper
115 Red pepper
122 White vinegar
125 Yellow croaker
148 Pork liver
154 White pepper
159 Egg qing dynasty
161 pepper
172 Pasta sauce
174 The soy sauce
176 Oyster sauce
177 abalone
178 Chopped green onion
180 Edible salt
182 shrimp
184 stock
186 Cooking oil
191 Cooked shrimp
193 Shredded ginger
194 liquor
204 Small rape
210 Dry starch
212 BeiDouFu
215 Soaked dried shrimp
216 Soaked mushrooms
218 vermicelli
219 Oyster sauce
220 Cooked pig intestine
221 Soya sauce
225 Clear soup
229 tomatoes
231 chicken
240 eggplant
247 Glutinous rice flour
248 yeast
249 Red bean paste
250 milk
251 Pig lean meat
254 powder
256 beer
260 lettuce
263 Yellow pepper
273 Egg white
274 rapeseed
275 Green peppers
276 Xianggu mushroom
282 Vegetable oil
283 Lean meat
285 Capsicum frutescens var
290 Egg white
294 Yellow bean sprouts
295 leeks
296 tapioca
297 Cooked pork bellies
298 Onions
317 Cooking oil
318 Edible salt
319 Thirteen powder
327 Five-spice powder
328 Chinese prickly ash
331 yam
332 Soft sugar
341 Fresh soup
342 Pig's feet
343 kelp
345 CuiDouFu
346 Lotus root
352 aniseed
356 Yellow pepper
365 cowpeas
366 Ma juice
374 Egg white
394 The brine tofu
395 pork
396 Clear water
399 Salty cedar
403 anise
404 Crucian carp
407 Rice vinegar
411 Large yellow croaker
423 celery
431 Egg white
432 beef
440 Taste is very fresh
443 Wax gourd
452 Chinese wolfberry
459 white radish
460 Pepper dish
472 Pickled Chinese toon
480 The pig lean meat
482 eggplant
486 tomatoes
490 prawns
491 Mung beans
494 Mayo
504 Chicken wings
513 Oyster sauce
515 radishes
516 sausage
520 The little yellow croaker
525 Black tempeh
529 Very beautiful fresh
531 tofu
532 Chinese wolfberry
537 Cooking wine
546 Edible salt
551 Chicken soup
553 Dried shrimp
559 Spinach heart
560 Dried shrimp
561 Pig fat tiger
562 Oil bean curd
563 Winter bamboo shoot
564 Dry mushrooms
565 Egg qing dynasty
567 Raw powder
578 Edible salt
582 Cabbage
583 peanuts
586 Sesame paste
588 Coriander stalks
593 Egg white
596 tofu
606 The pig lean meat
608 Green vegetables
616 Sea clams
621 tremella
625 Vegetable oil
627 Nun pumpkin
637 peanuts
638 Dried shrimp
642 Water bamboo
643 beans
651 Black sesame
653 Green beans
661 Oyster sauce
664 Sweet potato fans
667 Sunflower oil
669 Oyster sauce
680 Shrimps.
681 celery
689 Egg white
691 Duck leg
693 Red oil douban
698 bass
704 Edible salt
710 Edible salt
714 Streaky pork
718 Oyster sauce
721 kidneys
730 Kidney beans
731 roast duck
734 Edible salt
736 pomfret
738 ginger
740 Pickled peppers
741 Raw powder
742 Sweet bean paste in June
747 Green crab
757 Pasta sauce
761 Oyster sauce
765 Dried shrimp
766 Dry mushrooms
768 vermicelli
769 Oyster sauce
781 Corn flour
782 Clear water
783 scallop
796 CuiDouFu
809 Fresh meat
810 Hind legs meat
814 Five-spice powder
density = nx.density(G)#网络密度
print("Network density:", density)
Network density: 0.06703930760908763
shrimp_cabage_path = nx.shortest_path(G, source='6', target='83')# shrimp和Chinese carbage之间的最短路径
print("Shortest path between pork and wine:", shrimp_cabage_path)
print("Length of that path:", len(shrimp_cabage_path)-1)
Shortest path between pork and wine: ['6', '7', '83']
Length of that path: 2
print(nx.is_connected(G))
# 找到最大连通图
components = nx.connected_components(G)
largest_component = max(components, key=len)
# Create a "subgraph" of just the largest component
# Then calculate the diameter of the subgraph, just like you did with density.
#
subgraph = G.subgraph(largest_component)
diameter = nx.diameter(subgraph)
print("Network diameter of largest component:", diameter)
False
Network diameter of largest component: 4
triadic_closure = nx.transitivity(G)
print("Triadic closure:", triadic_closure)
Triadic closure: 0.29681052103821975
degree_dict = dict(G.degree(G.nodes()))
nx.set_node_attributes(G, degree_dict, 'degree')
print(G.nodes['20'])
{'caipin': '97', 'label': 'pork', 'degree': 41}
sorted_degree = sorted(degree_dict.items(), key=itemgetter(1), reverse=True)
print("Top 20 nodes by degree:")
for d in sorted_degree[:20]:
print(d)
Top 20 nodes by degree:
('4', 134)
('7', 116)
('8', 109)
('11', 108)
('17', 104)
('12', 95)
('10', 82)
('22', 82)
('108', 82)
('26', 74)
('87', 62)
('9', 61)
('42', 53)
('76', 50)
('115', 50)
('111', 48)
('36', 46)
('66', 46)
('21', 43)
('20', 41)
betweenness_dict = nx.betweenness_centrality(G) # Run betweenness centrality
eigenvector_dict = nx.eigenvector_centrality(G) # Run eigenvector centrality
# Assign each to an attribute in your network
nx.set_node_attributes(G, betweenness_dict, 'betweenness')
nx.set_node_attributes(G, eigenvector_dict, 'eigenvector')
sorted_betweenness = sorted(betweenness_dict.items(), key=itemgetter(1), reverse=True)
print("Top 20 nodes by betweenness centrality:")
for b in sorted_betweenness[:20]:
print(b)
#First get the top 20 nodes by betweenness as a list
top_betweenness = sorted_betweenness[:20]
#Then find and print their degree
for tb in top_betweenness: # Loop through top_betweenness (or eigenvector)
degree = degree_dict[tb[0]] # Use degree_dict to access a node's degree, see footnote 2
print("ID:", tb[0], "| Betweenness Centrality:", tb[1], "| Degree:", degree)
Top 20 nodes by betweenness centrality:
('4', 0.14353277774411674)
('9', 0.09657011447965594)
('11', 0.08668734217584056)
('17', 0.08608831748975987)
('7', 0.0795788667361031)
('108', 0.0682633304674705)
('22', 0.06541022846958919)
('8', 0.06539923121177074)
('12', 0.0461161123652701)
('10', 0.045962406544418175)
('26', 0.03689933309782013)
('115', 0.029231459361790564)
('82', 0.028882672325906168)
('76', 0.0286309302416837)
('16', 0.026412239249000612)
('87', 0.021729194266955076)
('23', 0.016439407763856377)
('186', 0.0156523393591171)
('66', 0.014854546898914696)
('20', 0.011498155300033426)
ID: 4 | Betweenness Centrality: 0.14353277774411674 | Degree: 134
ID: 9 | Betweenness Centrality: 0.09657011447965594 | Degree: 61
ID: 11 | Betweenness Centrality: 0.08668734217584056 | Degree: 108
ID: 17 | Betweenness Centrality: 0.08608831748975987 | Degree: 104
ID: 7 | Betweenness Centrality: 0.0795788667361031 | Degree: 116
ID: 108 | Betweenness Centrality: 0.0682633304674705 | Degree: 82
ID: 22 | Betweenness Centrality: 0.06541022846958919 | Degree: 82
ID: 8 | Betweenness Centrality: 0.06539923121177074 | Degree: 109
ID: 12 | Betweenness Centrality: 0.0461161123652701 | Degree: 95
ID: 10 | Betweenness Centrality: 0.045962406544418175 | Degree: 82
ID: 26 | Betweenness Centrality: 0.03689933309782013 | Degree: 74
ID: 115 | Betweenness Centrality: 0.029231459361790564 | Degree: 50
ID: 82 | Betweenness Centrality: 0.028882672325906168 | Degree: 23
ID: 76 | Betweenness Centrality: 0.0286309302416837 | Degree: 50
ID: 16 | Betweenness Centrality: 0.026412239249000612 | Degree: 22
ID: 87 | Betweenness Centrality: 0.021729194266955076 | Degree: 62
ID: 23 | Betweenness Centrality: 0.016439407763856377 | Degree: 39
ID: 186 | Betweenness Centrality: 0.0156523393591171 | Degree: 33
ID: 66 | Betweenness Centrality: 0.014854546898914696 | Degree: 46
ID: 20 | Betweenness Centrality: 0.011498155300033426 | Degree: 41
sorted_eigenvector = sorted(eigenvector_dict.items(), key=itemgetter(1), reverse=True)
print("Top 20 nodes by eigenvector centrality:")
for b in sorted_eigenvector[:20]:
print(b)
#First get the top 20 nodes by eigenvector as a list
top_eigenvector = sorted_eigenvector[:20]
#Then find and print their degree
for tb in top_eigenvector: # Loop through top_eigenvector
degree = degree_dict[tb[0]] # Use degree_dict to access a node's degree, see footnote 2
print("ID:", tb[0], "| Eigenvector Centrality:", tb[1], "| Degree:", degree)
Top 20 nodes by eigenvector centrality:
('4', 0.25014711428174885)
('7', 0.23306127579744945)
('8', 0.2315091510318178)
('11', 0.21381895651996302)
('12', 0.20893225823759318)
('17', 0.20301636459043884)
('10', 0.18861796088184513)
('22', 0.17434073532666447)
('26', 0.17394756401002645)
('108', 0.17335289877295187)
('87', 0.15641666127956727)
('42', 0.15283911133396796)
('9', 0.13839821066894276)
('115', 0.1363722035054998)
('21', 0.12976449306450002)
('111', 0.12943607619438782)
('66', 0.12639903689455384)
('36', 0.12544529463533466)
('20', 0.12241642480619327)
('76', 0.12208792779363463)
ID: 4 | Eigenvector Centrality: 0.25014711428174885 | Degree: 134
ID: 7 | Eigenvector Centrality: 0.23306127579744945 | Degree: 116
ID: 8 | Eigenvector Centrality: 0.2315091510318178 | Degree: 109
ID: 11 | Eigenvector Centrality: 0.21381895651996302 | Degree: 108
ID: 12 | Eigenvector Centrality: 0.20893225823759318 | Degree: 95
ID: 17 | Eigenvector Centrality: 0.20301636459043884 | Degree: 104
ID: 10 | Eigenvector Centrality: 0.18861796088184513 | Degree: 82
ID: 22 | Eigenvector Centrality: 0.17434073532666447 | Degree: 82
ID: 26 | Eigenvector Centrality: 0.17394756401002645 | Degree: 74
ID: 108 | Eigenvector Centrality: 0.17335289877295187 | Degree: 82
ID: 87 | Eigenvector Centrality: 0.15641666127956727 | Degree: 62
ID: 42 | Eigenvector Centrality: 0.15283911133396796 | Degree: 53
ID: 9 | Eigenvector Centrality: 0.13839821066894276 | Degree: 61
ID: 115 | Eigenvector Centrality: 0.1363722035054998 | Degree: 50
ID: 21 | Eigenvector Centrality: 0.12976449306450002 | Degree: 43
ID: 111 | Eigenvector Centrality: 0.12943607619438782 | Degree: 48
ID: 66 | Eigenvector Centrality: 0.12639903689455384 | Degree: 46
ID: 36 | Eigenvector Centrality: 0.12544529463533466 | Degree: 46
ID: 20 | Eigenvector Centrality: 0.12241642480619327 | Degree: 41
ID: 76 | Eigenvector Centrality: 0.12208792779363463 | Degree: 50
communities = community.greedy_modularity_communities(G)#模块化!!!
modularity_dict = {} # Create a blank dictionary
for i,c in enumerate(communities): # Loop through the list of communities, keeping track of the number for the community
for name in c: # Loop through each person in a community
modularity_dict[name] = i # Create an entry in the dictionary for the person, where the value is which group they belong to.
# Now you can add modularity information like we did the other metrics
nx.set_node_attributes(G, modularity_dict, 'modularity')
# First get a list of just the nodes in that class
class0 = [n for n in G.nodes() if G.nodes[n]['modularity'] == 0]
# Then create a dictionary of the eigenvector centralities of those nodes
class0_eigenvector = {n:G.nodes[n]['eigenvector'] for n in class0}
# Then sort that dictionary and print the first 5 results
class0_sorted_by_eigenvector = sorted(class0_eigenvector.items(), key=itemgetter(1), reverse=True)
print("Modularity Class 0 Sorted by Eigenvector Centrality:")
for node in class0_sorted_by_eigenvector[:5]:
print("Name:", node[0], "| Eigenvector Centrality:", node[1])
Modularity Class 0 Sorted by Eigenvector Centrality:
Name: 4 | Eigenvector Centrality: 0.25014711428174885
Name: 7 | Eigenvector Centrality: 0.23306127579744945
Name: 8 | Eigenvector Centrality: 0.2315091510318178
Name: 12 | Eigenvector Centrality: 0.20893225823759318
Name: 10 | Eigenvector Centrality: 0.18861796088184513
for i,c in enumerate(communities): # Loop through the list of communities
if len(c) > 1: # Filter out modularity classes with 2 or fewer nodes
print('Class '+str(i)+':', list(c)) # Print out the classes and their members
Class 0: ['22', '494', '273', '275', '0', '8', '593', '122', '282', '680', '3', '553', '64', '681', '5', '586', '486', '12', '276', '332', '15', '661', '689', '537', '63', '34', '47', '72', '583', '356', '460', '4', '480', '582', '532', '92', '327', '159', '814', '513', '28', '531', '653', '60', '67', '50', '504', '423', '627', '643', '431', '443', '810', '33', '251', '10', '21', '274', '254', '113', '115', '721', '7', '27', '26', '551', '154', '41', '32', '331', '161', '114', '588', '148', '616', '1', '452', '48', '783', '606', '263', '328', '482', '459', '256', '747']
Class 1: ['194', '46', '19', '247', '698', '177', '6', '345', '184', '182', '11', '472', '596', '66', '516', '250', '108', '704', '404', '731', '193', '317', '78', '186', '14', '249', '248', '346', '407', '515', '342', '260', '191', '295', '178', '319', '17', '734', '83', '796', '352', '2', '180', '730', '578', '403', '9', '13', '343', '546', '37', '651', '318', '710', '399', '432', '440', '42', '93']
Class 2: ['738', '36', '231', '285', '102', '736', '240', '111', '105', '210', '53', '341', '637', '103', '104', '290', '411', '638', '220', '221', '204', '225', '229', '55', '742', '740', '809', '20', '52', '374', '741', '283', '16', '642']
Class 3: ['766', '625', '74', '23', '691', '769', '394', '693', '218', '768', '395', '40', '77', '216', '765', '396', '621', '608', '215', '219', '297', '82', '298', '76', '212']
Class 4: ['525', '757', '98', '125', '174', '529', '172', '761', '718', '176', '714', '520', '87']
Class 5: ['567', '565', '561', '559', '564', '560', '562', '563']
Class 6: ['664', '669', '667', '296', '294']
Class 7: ['366', '365']
Class 8: ['490', '491']
Class 9: ['782', '781']
nx.write_gexf(G, 'shicai_network.gexf')