在Python中创建网络

2024-04-19 11:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想建立一个部分复制模型,这样我就可以从图G中的s>;=1个顶点开始,然后引入一个新的顶点“v”,并随机选择图G中的一个顶点“u”。用概率q,连接vu。相互独立地,以概率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))`

这段代码不起作用,因为当我试图计算平均最短路径长度时,它表示图形没有连接。你知道吗


Tags: inimportaddnodeforifpltrandom
1条回答
网友
1楼 · 发布于 2024-04-19 11:17:28
  • 我要做的第一件事就是去掉单字母变量名。你知道吗
  • 快捷方式:如果在不存在节点的位置添加边,它将添加该节点。你知道吗
  • 不能将list用作变量。(好的,你可以,但不要期望好的结果)

    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()
        my_list=[]
    
        for i in range(s):
            #random.randint(1,k)
            node = random.choice(G.nodes())
            if node not in my_list:
                v = max_score + i
                G.add_edge(v,node,weight = q)   # I don't understand what you are doing here.
                list.append(node)
    
            #are you sure your spacing is correct in the next 5 lines?    
            #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__':
        G= nx.Graph()
        max_score = 0
        with open('4932.protein.links.v10.txt','r') as f:
            for x in f.readlines()[1:]:
                node1, node2, val=x.split(" ")
                val = int(val)
                if val>=990:
                    G.add_edge(node1, node2, weight=val)
                max_score=max(max_score,val)
                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))
    

相关问题 更多 >