您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 根据网络重复边缘更新权重信息

5b51 2022/1/14 8:22:44 python 字数 3780 阅读 552 来源 www.jb51.cc/python

我有一个 JSON Feed数据,其中包含许多用户关系,如: "subject_id = 1, object_id = 2, object = added subject_id = 1, object_id = 2, object = liked subject_id = 1, object_id = 3, object = added subject_id = 2, object_id = 1

概述

"subject_id = 1,object_id = 2,object = added 
subject_id = 1,object = liked
subject_id = 1,object_id = 3,object = added
subject_id = 2,object_id = 1,object = added"

现在我使用以下代码将JSON转换为networkx图形:

def load(fname):
G = nx.DiGraph()
d = simplejson.load(open(fname))
for item in d:
    for attribute,value in item.iteritems():
        G.add_edge(value['subject_id'],value['object_id'])
return G

结果是这样的:

[('12820','80842'),('12820','81312'),'81311'),('13317','29'),('12144','81169'),('13140','16687'),'79092'),'78384'),'48715'),'54151'),'13718'),'4060'),'9914'),'32877'),'9918'),'4740'),'47847'),'29632'),'72395'),'48658'),'78394'),'4324'),'4776'),'78209'),'51624'),'66274'),'38009'),'80606'),'13762'),'28402'),'13720'),'9922'),('13303','81199'),('13130','70835'),'7936'),'30839'),'11558'),'32157'),'2785'),'73597'),'49879'),'62303'),'64275'),'48123'),'8722'),'43303'),'39316'),'78368'),'28328'),'57386'),'30739'),'71464'),'50296'),('12032','65338'),('13351','81316'),'16926'),'80435'),'79086'),('12107','16811'),'70310'),'10008'),'25466'),'36625'),'81320'),'48912'),'62209'),('12816','79526'),'79189'),('13180','39769'),'81319'),('12293','70918'),'59403'),'76348'),'12253'),'65078'),'61126'),'12243'),'12676'),'11693'),'78387'),'54788'),'26113'),'50472'),'50365'),'66431'),'29781'),'50435'),'48145'),'79170'),'76730'),'13260'),('12673',('12672',('13559','9327'),('12583','25462'),('12252','50754'),'11778'),'38306'),'48170'),'5488'),('12325','78635'),'4459'),'68699'),('12559','80285'),'78273'),('12020','48291'),'4498'),('12746','48916'),('13463','56785'),'47821'),('13461','80790'),'4425'),('12550','48353')]

我想做的是如果这些用户之间有一个以上的关系,我想增加体重.所以,正如我在JSON关系中演示的,subject_id 1与subject_id 2有3个关系,因此它们的权重应该是3,而user3与subject_id 1只有1个关系,所以它应该是1作为权重.

更新:

我想我已经解决了我的问题,使用:

def load(fname):
G = nx.MultiDiGraph()
d = simplejson.load(open(fname))
for item in d:
    for attribute,value in item.iteritems():
        if (value['subject_id'],value['object_id']) in G.edges():
            data = G.get_edge_data(value['subject_id'],value['object_id'],key='edge')
            G.add_edge(value['subject_id'],key='edge',weight=data['weight']+1)
        else:
            G.add_edge(value['subject_id'],weight=1)

print G.edges(data=True)

但是,您的帮助仍然会有所改善.

def load(fname):
    G = nx.DiGraph()
    d = simplejson.load(open(fname))
    for item in d:
        for attribute,value in item.iteritems():
            subject_id,object_id = value['subject_id'],value['object_id']
            if G.has_edge(subject_id,object_id):
                # we added this one before,just increase the weight by one
                G[subject_id][object_id]['weight'] += 1
            else:
                # new edge. add with weight=1
                G.add_edge(subject_id,object_id,weight=1)
    return G

总结

以上是编程之家为你收集整理的python – 根据网络重复边缘更新权重信息全部内容,希望文章能够帮你解决python – 根据网络重复边缘更新权重信息所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶