首页 > 解决方案 > Networkx 解码为 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) 部分。我已经尝试更改文档,但同样的错误不断弹出。我已经手动验证了所有节点或边都没有 NaN 或为空。

有人可以帮忙解决这个问题吗?

标签: pythongraphnlpnetworkx

解决方案


为了让未来的读者更容易,我将在这里发布这个部分答案(它并不能真正解决您的 Graphviz 布局不起作用的原因。)但是您的代码可以通过更改布局来工作,例如:

pos = nx.spring_layout(G)

推荐阅读