何がコードを壊したのかわかりませんが、ネットワーク オブジェクトを Gephi オブジェクトにエクスポートしようとすると、キー エラー numpy.int64 が発生し続けます。Combnol リストを繰り返し処理し、ネットワーク オブジェクトを 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を追加することを確認するだけですか?int(combnol[2][1].edge['教育']['基礎']['違い'])
これは、値が追加される私の現在の実装です。下の 1 の周りに int() を追加する必要がありますか? g[ペア[0]][ペア[1]]['重量'] += int(1); int(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)