首页 > 解决方案 > Networkx:为什么我的带有 jaccard 功能的二分网络投影不起作用?

问题描述

我正在尝试使用 networkx 中的音乐数据进行双向网络投影。我在 generic_weighted_projected_graph 中使用 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})

我究竟做错了什么?

标签: pythonnetworkingnetworkxbipartite

解决方案


我将投影函数更改为: G = bipartite.generic_weighted_projected_graph(F, node_list_music, weight_function=userCompare) 并且它起作用了。


推荐阅读