How to remove float('nan') node from a graph in networkx?

◇◆丶佛笑我妖孽 提交于 2020-12-12 09:50:49

问题


I have a dataset(pickle format) containing float('nan'), and I need to remove it.

It is possible to add float('nan') to a graph as a node in networkx. However, I don't know how to remove it.

import networkx as nx
G = nx.Graph()
G.add_node(float('nan'))
print(G.nodes) # [nan], so there is float('nan') in the graph
G.remove_node(float('nan')) # this statement raise a NetworkxError showing nan not in the graph

Code and data in CoReRank-WSDM-2019 and BirdNest.

Could anyone help me with this problem? Thank you in advance.


回答1:


We can test this on a simple dictionary, which is the underlying data structure of a NetworkX graph. Say you have:

d = {'a':3, float('nan'):4}

If we try accessing the NaN key, as you're trying to do:

d[float('nan')]
> KeyError: nan

The core cause of this, is explained by the fact that a NaN does not equal to itself:

>>> float("nan") == float("nan")
False

The reason behind why this causes the lookup to fail, is nicely explained here.

A workaround, could be to loop over the graph keys, and identify the NaN node and then remove that key using the same reference of the object:

import math

G = nx.Graph()
G.add_node(float('nan'))
G.add_node(3)
print(G.nodes) 
# [nan, 3]

nan_nodes = []
for node in G.nodes():
    if math.isnan(node):
        nan_nodes.append(node)
G.remove_nodes_from(nan_nodes)

G.nodes()
# NodeView((3,))


来源:https://stackoverflow.com/questions/64276449/how-to-remove-floatnan-node-from-a-graph-in-networkx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!