python - 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})
我究竟做错了什么?
解决方案
我将投影函数更改为: G = bipartite.generic_weighted_projected_graph(F, node_list_music, weight_function=userCompare) 并且它起作用了。
推荐阅读
- swift - 如何快速编写非/否定高阶函数?
- reactjs - 在 React js 中获取控制台中的下拉项
- php - 如何在 Laravel Controller 中创建 SQL Join 语句
- python - 在 Python 中发送 cookie
- google-cloud-platform - 如何在 Cloud Spanner 中为企业应用程序创建连接池
- python - 如何从mysql结果中只获取字符串?
- css - 反转 CSS 变量的像素值
- go - 用 Golang 简化字符串中的变量替换
- gradle - 使用自定义 gradle 插件的 Gradle 复合构建在 IntelliJ 中失败:“找不到参数的方法 api()”
- javascript - `Proxy` 将 `this[toString]` 与 `this[Symbol.toStringTag]` 混淆