Networkx:为什么我的带有jaccard函数的二部网络投影不起作用?

2024-04-25 02:09:39 发布

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

我正在尝试用networkx中的音乐数据进行二部网络投影。我在泛型加权投影图中使用jaccard函数,如本例所示:https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.bipartite.projection.generic_weighted_projected_graph.html

我的代码可以处理一个自制的图形,但不能处理我想处理的数据,即使数据的格式似乎相同。你知道吗

import networkx as nx
from networkx.algorithms import bipartite
import matplotlib.pyplot as plt
import pandas as pd

#CSV IMPORT
df = pd.read_csv('test.csv', delimiter=';')
node_list_user = df['source'].values.tolist()
node_list_music = df['target'].values.tolist()

F = nx.from_pandas_edgelist(df, source='source', target='target', edge_attr='weight')

#Check if CSV import is correct
print(bipartite.is_bipartite(F))

#Create graph to test if algorithm works with other data
B = nx.complete_bipartite_graph(2, 2)

for i,(u,v) in enumerate(B.edges()):
    B.edges[u, v]['weight'] = i + 1

#Print both graphs
for edge in F.edges(data=True):
    print(edge)

for edge in B.edges(data=True):
    print(edge)

#jaccard function
def userCompare(G, u, v):
    unbrs = set(G[u])
    vnbrs = set(G[v])
    return float(len(unbrs & vnbrs)) / len(unbrs | vnbrs)

#projection with jaccard function on (B/F)
G = bipartite.generic_weighted_projected_graph(F, [0, 1], weight_function=userCompare)
print(list(G.edges(data=True)))

nx.draw(G)
plt.show()

当我在自制的图形B上做投影时,一切正常:

G = bipartite.generic_weighted_projected_graph(B, [0, 1], weight_function=userCompare)

如果我用同样的方法处理带有外部数据的图F,我会得到以下错误:

G = bipartite.generic_weighted_projected_graph(F, [0, 1], weight_function=userCompare)

Traceback (most recent call last):
  File "/Users/studium/PycharmProjects/networktest/networktest.py", line 36, in <module>
    G = bipartite.generic_weighted_projected_graph(F, [0, 1], weight_function=userCompare)
  File "</Users/studium/PycharmProjects/networktest/venv/lib/python3.7/site-packages/decorator.py:decorator-gen-394>", line 2, in generic_weighted_projected_graph
  File "/Users/studium/PycharmProjects/networktest/venv/lib/python3.7/site-packages/networkx/utils/decorators.py", line 82, in _not_implemented_for
    return not_implement_for_func(*args, **kwargs)
  File "/Users/studium/PycharmProjects/networktest/venv/lib/python3.7/site-packages/networkx/algorithms/bipartite/projection.py", line 507, in generic_weighted_projected_graph
    G.add_nodes_from((n, B.nodes[n]) for n in nodes)
  File "/Users/studium/PycharmProjects/networktest/venv/lib/python3.7/site-packages/networkx/classes/graph.py", line 564, in add_nodes_from
    for n in nodes_for_adding:
  File "/Users/studium/PycharmProjects/networktest/venv/lib/python3.7/site-packages/networkx/algorithms/bipartite/projection.py", line 507, in <genexpr>
    G.add_nodes_from((n, B.nodes[n]) for n in nodes)
  File "/Users/studium/PycharmProjects/networktest/venv/lib/python3.7/site-packages/networkx/classes/reportviews.py", line 178, in __getitem__
    return self._nodes[n]
KeyError: 0

图F(部分)的打印:

('001656f03e1fae9a79239e6e2e9edd641977000a', 'the replacements', {'weight': 90})
('001656f03e1fae9a79239e6e2e9edd641977000a', 'sonic youth', {'weight': 87})
('001656f03e1fae9a79239e6e2e9edd641977000a', 'beastie boys', {'weight': 84})
('001656f03e1fae9a79239e6e2e9edd641977000a', 'creedence clearwater revival', {'weight': 84})

图B打印:

(0, 2, {'weight': 1})
(0, 3, {'weight': 2})
(1, 2, {'weight': 3})
(1, 3, {'weight': 4})

我做错什么了?你知道吗


Tags: innetworkxforusersgenericfilegraphnodes