我想建立一个部分复制模型,这样我就可以从图G中的s>;=1个顶点开始,然后引入一个新的顶点“v”,并随机选择图G中的一个顶点“u”。用概率q,连接v和u。相互独立地,以概率p连接u到v的每个邻居。我想根据我的s来重复这一点。你知道吗
我有一个文本文件,它有三列:Protein1,Protein2和Combined score。这个文件有1000多个这样的条目。因此,每一行代表了图中从“protein1”到“protein2”的一条边,其权重为“综合评分”。我使用这个文件来实现上述算法。我只过滤掉那些我的综合分数大于990的行。你知道吗
4932.Q0010 4932.Q0017 951
4932.Q0010 4932.Q0032 951
4932.Q0010 4932.Q0045 313
4932.Q0010 4932.Q0085 263
4932.Q0010 4932.Q0105 410
4932.Q0010 4932.Q0143 930
代码:
import networkx as nx
import matplotlib.pyplot as plt
import random
def partial_duplication_model(G,p,q,s,max_score):
k=G.number_of_nodes()
list=[]
for i in range(s):
#random.randint(1,k)
node = random.choice(G.nodes())
if node not in list:
v = max_score + i
G.add_node(v)
list.append(node)
G.add_edge(v,node,weight = q)
#for j in range(k):
for j in G.neighbors(node):
if not j==v:
G.add_edge(j,node,weight = p)
print(G.number_of_nodes())
return(G)
if __name__ == '__main__':
f=open("4932.protein.links.v10.txt","r").readlines()
G=nx.Graph()
max_score=0
for x in f[1:]:
y=x.split(" ")
for node in y[:1]:
if int(y[2])>=990:
G.add_node(node)
if int(y[2])>=990:
G.add_edge(y[0], y[1], weight=int(y[2]))
temp=int(y[2])
#print(type(temp))
max_score=max(max_score,temp)
p = 0.3
q = 0.7
s = 2
res = partial_duplication_model(G,p,q,s,max_score)
print("making a plot")
stuff = nx.degree_histogram(res)
plt.loglog(stuff)
plt.show()
#print("Average shortest path length : " , nx.average_shortest_path_length(res))`
这段代码不起作用,因为当我试图计算平均最短路径长度时,它表示图形没有连接。你知道吗
不能将list用作变量。(好的,你可以,但不要期望好的结果)
相关问题 更多 >
编程相关推荐