我正在尝试使用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
('001656f03e1fae9a79239e6e2e9edd641977000a', 'the replacements', {'weight': 90})
('001656f03e1fae9a79239e6e2e9edd641977000a', 'sonic youth', {'weight': 87})
('001656f03e1fae9a79239e6e2e9edd641977000a', 'beastie boys', {'weight': 84})
('001656f03e1fae9a79239e6e2e9edd641977000a', 'creedence clearwater revival', {'weight': 84})
(0, 2, {'weight': 1})
(0, 3, {'weight': 2})
(1, 2, {'weight': 3})
(1, 3, {'weight': 4})
我在做什么错了?
答案 0 :(得分:0)
我将投影功能更改为: G = bipartite.generic_weighted_projected_graph(F,node_list_music,weight_function = userCompare) 而且有效。