我不知道是什么破坏了我的代码,但是当我尝试将网络对象导出到 gephi 对象时,我不断收到关键错误 numpy.int64。我正在遍历组合列表并将网络对象放入 write_gexf 函数中。
Combnol =
[[1992, <networkx.classes.graph.Graph at 0x10dfc5b50>],
[2000, <networkx.classes.graph.Graph at 0x10dc58650>],
这是网络对象的样子
combnol[1].edge
{'Alliances Partnerships': {'Education': {'difference': 1.0,
'weight': 1,
'year': 2001},
combnol[1].node
{'Alliances Partnerships': {'color': 'y'},
'Education': {'color': 'y'},
'Entrepreneurship': {'color': 'y'},
#save file
for i, no in enumerate(combnol):
print repr(no[1])
nx.write_gexf(no[1], 'no{0}'.format(i))
break
追溯
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'>
那么是否只是确保我添加整数而不是浮点数?int(combnol[2][1].edge['Education']['Foundations']['difference'])
这是我当前的实现,其中添加了值。我应该在下面的 1 周围添加一个 int() 吗?g[pair[0]][pair[1]]['weight'] += int(1); 整数(1 / 重量)
# 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)