Networkx 解码为 str TypeError
Networkx decoding to str TypeError
我正在尝试从一些文本创建图形表示,但一直收到 TypeError。这是一个示例代码:
document="Let us assume, as a running example, that my data is composed of word embeddings of the\
English language. I want to gain insights about the word distribution in the embedding space,\
specifically, if there are any clusters of very similar words, if there are words that are\
completely different from the rest, if there are words very similar to every other word, and so \
on. And I want to gain these insights visually, so that it is easier to understand and share with my collegues."
document = preprocess_document(document)
nodes = get_entities(document)
edges= get_relations(document)
G= nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
plt.figure(figsize=(10,10))
pos = nx.nx_agraph.graphviz_layout(G)
nx.draw(G, pos=pos, with_labels=True)
edge_labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.title(title)
plt.show()
我收到错误:
----> 1 plot_graph(G)
<ipython-input-174-287c83e8e288> in plot_graph(G, title)
4
5 # define position of nodes in figure
----> 6 pos = nx.nx_agraph.graphviz_layout(G)
7
8 # draw nodes and edges
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in graphviz_layout(G, prog, root, args)
235 This is a wrapper for pygraphviz_layout.
236 """
--> 237 return pygraphviz_layout(G, prog=prog, root=root, args=args)
238
239
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in pygraphviz_layout(G, prog, root, args)
282 if root is not None:
283 args += f"-Groot={root}"
--> 284 A = to_agraph(G)
285 A.layout(prog=prog, args=args)
286 node_pos = {}
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in to_agraph(N)
146 # add nodes
147 for n, nodedata in N.nodes(data=True):
--> 148 A.add_node(n)
149 # Add node data
150 a = A.get_node(n)
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/agraph.py in add_node(self, n, **attr)
311 except KeyError:
312 nh = gv.agnode(self.handle, n, _Action.create)
--> 313 node = Node(self, nh=nh)
314 node.attr.update(**attr)
315
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/agraph.py in __new__(self, graph, name, nh)
1562 def __new__(self, graph, name=None, nh=None):
1563 if nh is not None:
-> 1564 n = super(Node, self).__new__(self, gv.agnameof(nh), graph.encoding)
1565 else:
1566 n = super(Node, self).__new__(self, name)
TypeError: decoding to str: need a bytes-like object, NoneType found
错误似乎在代码的 nx.nx_agraph.graphviz_layout(G) 部分。我尝试更改文档,但不断弹出相同的错误。我已手动验证 none 个节点或边具有 NaN 或为空。
有人可以帮忙解决这个问题吗?
为了让未来的读者更容易阅读,我会 post 在这里给出这个部分答案(它并没有真正解决为什么你的 Graphviz 布局不起作用。)但是你的代码通过改变布局来工作,比如例如:
pos = nx.spring_layout(G)
我正在尝试从一些文本创建图形表示,但一直收到 TypeError。这是一个示例代码:
document="Let us assume, as a running example, that my data is composed of word embeddings of the\
English language. I want to gain insights about the word distribution in the embedding space,\
specifically, if there are any clusters of very similar words, if there are words that are\
completely different from the rest, if there are words very similar to every other word, and so \
on. And I want to gain these insights visually, so that it is easier to understand and share with my collegues."
document = preprocess_document(document)
nodes = get_entities(document)
edges= get_relations(document)
G= nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
plt.figure(figsize=(10,10))
pos = nx.nx_agraph.graphviz_layout(G)
nx.draw(G, pos=pos, with_labels=True)
edge_labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.title(title)
plt.show()
我收到错误:
----> 1 plot_graph(G)
<ipython-input-174-287c83e8e288> in plot_graph(G, title)
4
5 # define position of nodes in figure
----> 6 pos = nx.nx_agraph.graphviz_layout(G)
7
8 # draw nodes and edges
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in graphviz_layout(G, prog, root, args)
235 This is a wrapper for pygraphviz_layout.
236 """
--> 237 return pygraphviz_layout(G, prog=prog, root=root, args=args)
238
239
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in pygraphviz_layout(G, prog, root, args)
282 if root is not None:
283 args += f"-Groot={root}"
--> 284 A = to_agraph(G)
285 A.layout(prog=prog, args=args)
286 node_pos = {}
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in to_agraph(N)
146 # add nodes
147 for n, nodedata in N.nodes(data=True):
--> 148 A.add_node(n)
149 # Add node data
150 a = A.get_node(n)
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/agraph.py in add_node(self, n, **attr)
311 except KeyError:
312 nh = gv.agnode(self.handle, n, _Action.create)
--> 313 node = Node(self, nh=nh)
314 node.attr.update(**attr)
315
~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/agraph.py in __new__(self, graph, name, nh)
1562 def __new__(self, graph, name=None, nh=None):
1563 if nh is not None:
-> 1564 n = super(Node, self).__new__(self, gv.agnameof(nh), graph.encoding)
1565 else:
1566 n = super(Node, self).__new__(self, name)
TypeError: decoding to str: need a bytes-like object, NoneType found
错误似乎在代码的 nx.nx_agraph.graphviz_layout(G) 部分。我尝试更改文档,但不断弹出相同的错误。我已手动验证 none 个节点或边具有 NaN 或为空。
有人可以帮忙解决这个问题吗?
为了让未来的读者更容易阅读,我会 post 在这里给出这个部分答案(它并没有真正解决为什么你的 Graphviz 布局不起作用。)但是你的代码通过改变布局来工作,比如例如:
pos = nx.spring_layout(G)