结点重要性与SIR模型基础代码SIR模型
# simulate the information diffusion under SI model
import networkx as nx
import numpy as np
import random
import matplotlib .pyplot as plt
max_iter_num =25
g = nx.karate_club_graph()
# init the graph with random edge weight and t the inactive status
for edge in g.edges:
g.add_edge(edge[0], edge[1], weight=0.01)
for node in g:
g.add_node(node, state =0, weight =0.1)
ed =33
all_active_nodes =[]
all_active_nodes.append(ed)
recover_influence_nodes =[]
for i in range(max_iter_num):
new_active =list()
tl ='%s time'% i +' %s nodes'%len(all_active_nodes)
print(tl)
# infection
for v in all_active_nodes:
for nbr ighbors(v):
# print(nbr)
des[nbr]['state']==0:
edge_data = g.get_edge_data(v, nbr)
if random.uniform(0,1)< edge_data['weight']:
在兹
new_active.append(nbr)
all_d(new_active)
for v in all_active_nodes:
# print(v)
if random.uniform(0,1)< g.nodes[v]['weight']:
recover_influence_nodes.append(v)笑也依然
all_ve(v)
print('all actived nodes: ', all_active_nodes)
print('recover influence nodes: ', recover_influence_nodes)
结点亲密度karate
import networkx as nx
import numpy as np
filename =r"F:\PyCharm\PyCharmProjects\machine-learning\sns_datats\karate\" n =34
# 初值邻接矩阵
adjacencyMatrix = np.zeros((n, n))
# 亲密度矩阵
intimacy = np.zeros((n, n))
# 结点影响⼒矩阵
KI = np.zeros(n)
with open(filename)as f:
for line adlines():
temp_list = line.split()
adjacencyMatrix[int(temp_list[0])-1][int(temp_list[1])-1]=1
adjacencyMatrix[int(temp_list[1])-1][int(temp_list[0])-1]=1
if __name__ =='__main__':
print(adjacencyMatrix)
for i in range(len(adjacencyMatrix)):
for j in range(len(adjacencyMatrix[0])):
print(adjacencyMatrix[i][j], end=' ')
print()
# 定义⼀个数组,⽤来保存每个结点的邻居数
neighborsNum = np.zeros(n)
# 计算每个结点的邻居数
for i in range(len(adjacencyMatrix)):
size =0
for j in range(len(adjacencyMatrix[0])):
if(adjacencyMatrix[i][j]==1):
size = size +1
neighborsNum[i]= size
print("打印每个结点的邻居数")
for i in range(len(neighborsNum)):
吉首大学怎么样print(neighborsNum[i])
# 计算每个结点与邻居结点的交集数量,⽤以计算初始化亲密度矩阵
for i in range(len(adjacencyMatrix)):
for j in range(len(adjacencyMatrix[0])):
# 如果找到了邻居,那么就对⽐当前结点所在的⾏与邻居结点所在的⾏有多少交集
commonNodes =0爱国英雄人物事迹
if(adjacencyMatrix[i][j]==1):
for k in range(len(adjacencyMatrix[0])):
if((adjacencyMatrix[i][k]==1)and(adjacencyMatrix[i][k]== adjacencyMatrix[j][k])):
commonNodes = commonNodes +1
惬意是什么意思# 求得共同邻居数之后,根据公式计算亲密度
intimacy[i][j]=1+((commonNodes)/(neighborsNum[i]+neighborsNum[j]-commonNodes-1)) print("共同邻居数与亲密度为", i, j, commonNodes, intimacy[i][j],end=' ')
print()
# 计算结点的影响⼒
for i in range(len(adjacencyMatrix)):
# 定义间接影响⼒变量
indirect =0
for j in range(len(adjacencyMatrix[0])):
儿童早餐简单营养早餐if(adjacencyMatrix[i][j]==1):
indirect = indirect + intimacy[i][j]*neighborsNum[j]
KI[i]= neighborsNum[i]+ indirect
print(i,"影响⼒的值为",KI[i])
KI.sort()
print(KI)
结点亲密度-USAir97
import networkx as nx
import numpy as np
filename =r"F:\PyCharm\PyCharmProjects\machine-learning\sns_"
n =334
# 初值邻接矩阵
adjacencyMatrix = np.zeros((n, n))
# 亲密度矩阵
intimacy = np.zeros((n, n))
个人优点怎么写# 结点影响⼒矩阵
KI = np.zeros(n)
with open(filename)as f:
for line adlines():
temp_list = line.split()
adjacencyMatrix[int(temp_list[0])-1][int(temp_list[1])-1]=1
adjacencyMatrix[int(temp_list[1])-1][int(temp_list[0])-1]=1
if __name__ =='__main__':
print(adjacencyMatrix)
for i in range(len(adjacencyMatrix)):
for j in range(len(adjacencyMatrix[0])):
print(adjacencyMatrix[i][j], end=' ')
print()
# 定义⼀个数组,⽤来保存每个结点的邻居数
neighborsNum = np.zeros(n)
# 计算每个结点的邻居数
for i in range(len(adjacencyMatrix)):
size =0
for j in range(len(adjacencyMatrix[0])):
if(adjacencyMatrix[i][j]==1):
size = size +1
neighborsNum[i]= size
print("打印每个结点的邻居数")
for i in range(len(neighborsNum)):
print(neighborsNum[i])
# 计算每个结点与邻居结点的交集数量,⽤以计算初始化亲密度矩阵
for i in range(len(adjacencyMatrix)):
for j in range(len(adjacencyMatrix[0])):
commonNodes =0;
# 如果找到了邻居,那么就对⽐当前结点所在的⾏与邻居结点所在的⾏有多少交集
if(adjacencyMatrix[i][j]==1):
for k in range(len(adjacencyMatrix[0])):
如何炒面
if((adjacencyMatrix[i][k]==1)and(adjacencyMatrix[i][k]== adjacencyMatrix[j][k])):
commonNodes = commonNodes +1
# 求得共同邻居数之后,根据公式计算亲密度
intimacy[i][j]=1+((commonNodes)/(neighborsNum[i]+neighborsNum[j]-commonNodes-1)) print("共同邻居数与亲密度为", i, j, commonNodes, intimacy[i][j],end=' ')
print()
# 计算结点的影响⼒
for i in range(len(adjacencyMatrix)):
# 定义间接影响⼒变量
indirect =0;
for j in range(len(adjacencyMatrix[0])):
if(adjacencyMatrix[i][j]==1):
indirect = indirect + intimacy[i][j]*neighborsNum[j]
KI[i]= neighborsNum[i]+ indirect
print(i,"结点的影响⼒",KI[i])