Python 正在将networkx对象导入到gephi-KeyError:<;类型';numpy.int64'&燃气轮机;
我不知道是什么破坏了我的代码,但当我试图将我的网络对象导出到一个gephi对象中时,我不断得到密钥错误numpy.int64。我正在遍历comnol列表,并将网络对象放入write_gexf函数中Python 正在将networkx对象导入到gephi-KeyError:<;类型';numpy.int64'&燃气轮机;,python,numpy,networkx,gephi,Python,Numpy,Networkx,Gephi,我不知道是什么破坏了我的代码,但当我试图将我的网络对象导出到一个gephi对象中时,我不断得到密钥错误numpy.int64。我正在遍历comnol列表,并将网络对象放入write_gexf函数中 Combnol = [[1992, <networkx.classes.graph.Graph at 0x10dfc5b50>], [2000, <networkx.classes.graph.Graph at 0x10dc58650>], 回溯 KeyError
Combnol =
[[1992, <networkx.classes.graph.Graph at 0x10dfc5b50>],
[2000, <networkx.classes.graph.Graph at 0x10dc58650>],
回溯
KeyError Traceback (most recent call last)
<ipython-input-1536-80e0a534cec9> in <module>()
2 for i, no in enumerate(combnol):
3 print repr(no[1])
----> 4 nx.write_gexf(no[1], 'no')
5 break
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in write_gexf(G, path, encoding, prettyprint, version)
//anaconda/lib/python2.7/site-packages/networkx/utils/decorators.pyc in _open_file(func, *args, **kwargs)
261 # Finally, we call the original function, making sure to close the fobj.
262 try:
--> 263 result = func(*new_args, **kwargs)
264 finally:
265 if close_fobj:
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in write_gexf(G, path, encoding, prettyprint, version)
75 writer = GEXFWriter(encoding=encoding,prettyprint=prettyprint,
76 version=version)
---> 77 writer.add_graph(G)
78 writer.write(path)
79
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in add_graph(self, G)
286 self.graph_element=graph_element
287 self.add_nodes(G,graph_element)
--> 288 self.add_edges(G,graph_element)
289 self.xml.append(graph_element)
290
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in add_edges(self, G, graph_element)
361 edge_data=self.add_viz(edge_element,edge_data)
362 edge_data=self.add_attributes("edge", edge_element,
--> 363 edge_data, default)
364 edges_element.append(edge_element)
365 graph_element.append(edges_element)
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in add_attributes(self, node_or_edge, xml_obj, data, default)
379 if k == 'key':
380 k='networkx_key'
--> 381 attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)],
382 node_or_edge, default, mode)
383 if type(v)==list:
KeyError: <type 'numpy.int64'>
边缘中的值的数据类型是什么?该特定代码正在尝试序列化该值,它会按类型进行序列化。因此,字典中有一个“int”的词条,它基本上调用
str(int)
,但显然没有一个用于numpy int的词条。解决方案:在添加到networkx边缘时转换为“普通”整数,或者将numpy整数的转换添加到networkx源中。已更新以询问您的问题。。。让我试试看,我看不出您在哪里使用numpy.int64类型。但你一定在某个地方。如果要编写graphml,请使用常规Python整数。如何使用常规Python整数?这仅仅意味着写作吗1@user3314418-是的,这不是整数对浮点的事情。你在用numpy吗?如果没有,那么我也不明白为什么会出现这个错误。如果是,那么是的,在添加到图形时只需执行int(numpy\u int)
。
KeyError Traceback (most recent call last)
<ipython-input-1536-80e0a534cec9> in <module>()
2 for i, no in enumerate(combnol):
3 print repr(no[1])
----> 4 nx.write_gexf(no[1], 'no')
5 break
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in write_gexf(G, path, encoding, prettyprint, version)
//anaconda/lib/python2.7/site-packages/networkx/utils/decorators.pyc in _open_file(func, *args, **kwargs)
261 # Finally, we call the original function, making sure to close the fobj.
262 try:
--> 263 result = func(*new_args, **kwargs)
264 finally:
265 if close_fobj:
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in write_gexf(G, path, encoding, prettyprint, version)
75 writer = GEXFWriter(encoding=encoding,prettyprint=prettyprint,
76 version=version)
---> 77 writer.add_graph(G)
78 writer.write(path)
79
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in add_graph(self, G)
286 self.graph_element=graph_element
287 self.add_nodes(G,graph_element)
--> 288 self.add_edges(G,graph_element)
289 self.xml.append(graph_element)
290
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in add_edges(self, G, graph_element)
361 edge_data=self.add_viz(edge_element,edge_data)
362 edge_data=self.add_attributes("edge", edge_element,
--> 363 edge_data, default)
364 edges_element.append(edge_element)
365 graph_element.append(edges_element)
//anaconda/lib/python2.7/site-packages/networkx/readwrite/gexf.pyc in add_attributes(self, node_or_edge, xml_obj, data, default)
379 if k == 'key':
380 k='networkx_key'
--> 381 attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)],
382 node_or_edge, default, mode)
383 if type(v)==list:
KeyError: <type 'numpy.int64'>
print type(combnol[2][1].edge['Education']['Foundations']['difference'])
<type 'float'>
# adjusting weight and difference if edge already exists
#print pair[i][0], pair[i][1]
if g.has_edge(pair[0], pair[1]):
g[pair[0]][pair[1]]['weight'] += 1
# adding edge if non-existant with initial weight = 1
else:
weight = 1
g.add_edge(pair[0], pair[1], weight = weight, difference = 1. / weight, year= year)