我正在尝试使用 csv 文件制作图表,该文件包含有关节点的边缘、职业和年龄的信息。我将社区分配给每个节点并执行链接预测。
import networkx as nx
import csv
engineers1 = []
engineers2 = []
engineers3 = []
engineers4 = []
engineers5 = []
actors1= []
actors2= []
actors3= []
actors4= []
actors5= []
writers1 = []
writers2= []
writers3= []
writers4 = []
writers5 = []
doctors1= []
doctors2= []
doctors3= []
doctors4= []
doctors5= []
drivers1=[]
drivers2=[]
drivers3=[]
drivers4=[]
drivers5=[]
teachers1=[]
teachers2=[]
teachers3=[]
teachers4=[]
teachers5=[]
nodes=[]
g=nx.Graph()
for i in range(0,4038):
g.add_node(i)
with open("asd1.csv",'r') as csv_file:
csv_reader=csv.DictReader(csv_file)
for line in csv_reader:
g.add_edge(line['first'],line['second'])
csv_file.close()
with open("asd1.csv",'r') as csv_file:
csv_reader=csv.DictReader(csv_file)
for line in csv_reader:
if (line['profession'] == 'actor' and line['age'] >= '13' and
line['age'] <= '17'):
actors1.append(line['name'])
if (line['profession'] == 'actor' and line['age'] >= '18' and
line['age'] <= '29'):
actors2.append(line['name'])
if (line['profession'] == 'actor' and line['age'] >= '30' and
line['age'] <= '49'):
actors3.append(line['name'])
if (line['profession'] == 'actor' and line['age'] >= '50' and line['age'] <= '64'):
actors4.append(line['name'])
if (line['profession'] == 'actor' and line['age'] >= '65'):
actors5.append(line['name'])
if (line['profession'] == 'eng' and line['age'] >= '13' and line['age'] <= '17'):
engineers1.append(line['name'])
if (line['profession'] == 'eng' and line['age'] >= '18' and line['age'] <= '29'):
engineers2.append(line['name'])
if (line['profession'] == 'eng' and line['age'] >= '30' and line['age'] <= '49'):
engineers3.append(line['name'])
if (line['profession'] == 'eng' and line['age'] >= '50' and line['age'] <= '64'):
engineers4.append(line['name'])
if (line['profession'] == 'eng' and line['age'] >= '65'):
engineers5.append(line['name'])
if (line['profession'] == 'teacher' and line['age'] >= '13' and line['age'] <= '17'):
teachers1.append(line['name'])
if (line['profession'] == 'teacher' and line['age'] >= '18' and line['age'] <= '29'):
teachers2.append(line['name'])
if (line['profession'] == 'teacher' and line['age'] >= '30' and line['age'] <= '49'):
teachers3.append(line['name'])
if (line['profession'] == 'teacher' and line['age'] >= '50' and line['age'] <= '64'):
teachers4.append(line['name'])
if (line['profession'] == 'teacher' and line['age'] >= '65'):
teachers5.append(line['name'])
if (line['profession'] == 'driver' and line['age'] >= '13' and line['age'] <= '17'):
drivers1.append(line['name'])
if (line['profession'] == 'driver' and line['age'] >= '18' and line['age'] <= '29'):
drivers2.append(line['name'])
if (line['profession'] == 'driver' and line['age'] >= '30' and line['age'] <= '49'):
drivers3.append(line['name'])
if (line['profession'] == 'driver' and line['age'] >= '50' and line['age'] <= '64'):
doctors4.append(line['name'])
if (line['profession'] == 'driver' and line['age'] >= '65'):
drivers5.append(line['name'])
if (line['profession'] == 'doctor' and line['age'] >= '13' and line['age'] <= '17'):
doctors1.append(line['name'])
if (line['profession'] == 'doctor' and line['age'] >= '18' and line['age'] <= '29'):
doctors2.append(line['name'])
if (line['profession'] == 'doctor' and line['age'] >= '30' and line['age'] <= '49'):
doctors3.append(line['name'])
if (line['profession'] == 'doctor' and line['age'] >= '50' and line['age'] <= '64'):
drivers4.append(line['name'])
if (line['profession'] == 'doctor' and line['age'] >= '65'):
doctors5.append(line['name'])
csv_file.close()
print("actors having age between 13 and 17: ",actors1)
print("actors having age between 18 and 29: ",actors2)
print("actors having age between 30 and 49: ",actors3)
print("actors having age between 50 and 64: ",actors4)
print("actors having age 65 and above: ",actors5)
print('\n')
print("engineers having age between 13 and 17: ",engineers1)
print("engineers having age between 18 and 29: ",engineers2)
print("engineers having age between 30 and 49: ",engineers3)
print("engineers having age between 50 and 64: ",engineers4)
print("engineers having age 65 and above: ",engineers5)
print('\n')
print("teachers having age between 13 and 17: ",teachers1)
print("teachers having age between 18 and 29: ",teachers2)
print("teachers having age between 30 and 49: ",teachers3)
print("teachers having age between 50 and 64: ",teachers4)
print("teachers having age 65 and above: ",teachers5)
print('\n')
print("drivers having age between 13 and 17: ",drivers1)
print("drivers having age between 18 and 29: ",drivers2)
print("drivers having age between 30 and 49: ",drivers3)
print("drivers having age between 50 and 64: ",drivers4)
print("drivers having age 65 and above: ",drivers5)
print('\n')
print("doctors having age between 13 and 17: ",doctors1)
print("doctors having age between 18 and 29: ",doctors2)
print("doctors having age between 30 and 49: ",doctors3)
print("doctors having age between 50 and 64: ",doctors4)
print("doctors having age 65 and above: ",doctors5)
print('\n')
for i in range(0,4038):
g.node[i]['community']=0
for x1 in actors1:
g.node[x1]['community']=0
for x2 in actors2:
g.node[x2]['community']=1
for x3 in actors3:
g.node[x3]['community']=2
for x4 in actors4:
g.node[x4]['community']=3
for x5 in actors5:
g.node[x5]['community']=4
for x6 in engineers1:
g.node[x6]['community']=5
for x7 in engineers2:
g.node[x7]['community']=6
for x8 in engineers3:
g.node[x8]['community']=7
for x9 in engineers4:
g.node[x9]['community']=8
for x10 in engineers5:
g.node[x10]['community']=9
for x11 in teachers1:
g.node[x11]['community']=10
for x12 in teachers2:
g.node[x12]['community']=11
for x13 in teachers3:
g.node[x13]['community']=12
for x14 in teachers4:
g.node[x14]['community']=13
for x15 in teachers5:
g.node[x15]['community']=14
for x16 in drivers1:
g.node[x16]['community']=15
for x17 in drivers2:
g.node[x17]['community']=16
for x18 in drivers3:
g.node[x18]['community']=17
for x19 in drivers4:
g.node[x19]['community']=18
for x20 in drivers5:
g.node[x20]['community']=19
for x21 in doctors1:
g.node[x21]['community']=20
for x22 in doctors2:
g.node[x22]['community']=21
for x23 in doctors3:
g.node[x23]['community']=22
for x24 in doctors4:
g.node[x24]['community']=23
for x25 in doctors5:
g.node[x25]['community']=24
print(g.nodes())
l=list(nx.cn_soundarajan_hopcroft(g))
print(l)