我在使用 NetworkX 时遇到了一个奇怪的问题。
鉴于 DS-1 dataset ,我的任务是每年创建一个在数据集中报告的图表。到目前为止,完全没有问题。对于 2013 年,这就是我得到的
我们可以说...有点拥挤。
现在这是我的奇怪问题。我的作业规定我应该通过某种逻辑选择每个图的顶部 k 节点。因此,由于我有一些节点少于 5 个的图(并且,根据要求,这个 k 将是 [0,5,10,50,200] 中的值),我想在迭代中排除那些 len(G ) 是 < k。
因此,给定一个字典 graphsPerYear (键:年份 - 值:图表)...
for x in graphsPerYear:
G = graphsPerYear[x]
if len(G) < k:
print(G.nodes)
print(G.number_of_nodes())
print("Skipping year " + str(x) + " since it has " + str(len(G)) + " nodes which is less than the prompted k")
continue
输出如下:
['linear matrix inequality', 'social inequality']
2
Skipping year 2013 since it has 2 nodes which is less than the prompted k
但图像却完全相反。我错过了什么?
编辑
添加图表的创建
def createGraphPerYear(dataset, year):
insertedWords = set()
listaAnni = set(dataset['anno'].values)
grafi = dict()
for anno in listaAnni:
datasetTemporale = dataset[dataset['anno'] == anno]
G=nx.DiGraph()
for index, row in datasetTemporale.iterrows():
#Reminder: ogni row è formato da anno, keyword1, keyword2, dizionario utilizzatore keywords - numero volte
#FASE 1: AGGIUNTA DEI DUE POSSIBILI NODI
if row.keyword1 not in G:
G.add_node(row.keyword1)
if row.keyword2 not in G:
G.add_node(row.keyword2)
if not __areNodesConnected(G,row.keyword1, row.keyword2):
G.add_edge(row.keyword1,row.keyword2)
grafi[anno] = G
return grafi
def __areNodesConnected(G, nodeToCheckOne,nodeToCheckTwo):
return nodeToCheckOne in G.neighbors(nodeToCheckTwo)
最佳答案
当您将节点添加到网络时,它会对其进行哈希
以确定唯一性。
具有相同哈希
的任何节点都被确定为相同。
By definition, a Graph is a collection of nodes (vertices)
along with identified pairs of nodes (called edges, links, etc).
In NetworkX, nodes can be any hashable object e.g.,
a text string, an image, an XML object, another Graph,
a customized node object, etc.
仔细检查这些项目是否不是相同的字符串,或者它们的散列性对于不同的节点来说不相同。
关于python - networkx 说我的节点比实际的少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872886/