networks algorithms python duplicates edge networkx weight

python - algorithms - Actualización de información de peso según repetición de bordes con networkx



networkx random (1)

Simplemente puede usar el atributo de weight almacenar sus pesos. Puede verificar si existe una ventaja con el método has_edge . La combinación de estos te daría:

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

Tengo datos de alimentación JSON con muchas relaciones de usuario, como:

"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, object = added"

Ahora he usado el siguiente código para convertir JSON a networkx Graph:

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

Y el resultado es algo así como:

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

Lo que quiero hacer es aumentar de peso si hay más de 1 relación entre estos usuarios. Entonces, como demostré en la relación JSON, subject_id 1 tiene 3 relaciones con subject_id 2, por lo tanto su peso debe ser 3, mientras que el usuario 3 tiene solo 1 relación con subject_id 1 y por lo tanto debe ser 1 como peso.

Actualizar:

Supongo que he resuelto mi problema con el uso de:

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''], value[''object_id''], key=''edge'', weight=data[''weight'']+1) else: G.add_edge(value[''subject_id''], value[''object_id''], key=''edge'', weight=1) print G.edges(data=True)

Pero aún así tu ayuda sería buena para mejorar.