加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 23:18:49 所属栏目:Python 来源:网络整理
导读:我有一个 JSON Feed数据,其中包含许多用户关系,如: "subject_id = 1,object_id = 2,object = added subject_id = 1,object = likedsubject_id = 1,object_id = 3,object = addedsubject_id = 2,object_id = 1,object = added" 现在我使用以下代码将JSON转换
我有一个 JSON Feed数据,其中包含许多用户关系,如:
"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)

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

解决方法

您可以使用权重属性存储您的权重.您可以使用has_edge方法检查边缘是否存在.结合这些会给你:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读