从.p(精选)文件构造networkx图

时间:2018-08-26 14:38:56

标签: python-3.x graph pickle networkx digraphs

我一直在做名为网络分析入门(Pt1)的Datacamp课程,并且有一个以Graph / DiGraph形式出现的测试网络。

在他们网站上的交互式python shell中,我可以键入T.edges()T.nodes()等。但是我不知道如何在本地计算机上加载相同的网络。

数据以.p扩展名提供。点击https://mega.nz/#!hs4RhbjC!ukDcb6pDiJSEoAGy-WiosfcMgP62qiQgAAAAAAAAAAA)访问文件。

 import networkx as nx
 dg = pickle.load(open('../data/tw.p'))
 print (dg.edges())

它读取错误

 Traceback (most recent call last):
  File "C:\Code\DataCamp-master\21-network-analysis-in-python-(part-1)\01-introduction-to-networks\02-queries-on-a-graph.py", line 22, in <module>
    dg = pickle.load(open('../data/tw.p'))
  File "C:\ProgramData\Anaconda3\lib\encodings\cp1251.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 4933: character maps to <undefined>

@

   T = nx.read_gpickle('../folder/tw.p')

当我尝试通过nx.read_gpickle时,我得到了:

   Traceback (most recent call last):
  File "C:\Code\DataCamp-master\21-network-analysis-in-python-(part-1)\01-introduction-to-networks\02-queries-on-a-graph.py", line 21, in <module>
    print (T.nodes())
  File "C:\ProgramData\Anaconda3\lib\site-packages\networkx\classes\graph.py", line 719, in nodes
    nodes = NodeView(self)
  File "C:\ProgramData\Anaconda3\lib\site-packages\networkx\classes\reportviews.py", line 168, in __init__
    self._nodes = graph._node
AttributeError: 'DiGraph' object has no attribute '_node'

下面显示的是它的外观,我不知道如何使它像这样:

Directed Graph from the provided file.

The Twitter network has been loaded as `T`.

在[1] type(T)

networkx.classes.digraph.DiGraph

T.nodes(data=True)[:10]
[(1, {'category': 'I', 'occupation': 'scientist'}),
 (3, {'category': 'P', 'occupation': 'politician'}),
 (4, {'category': 'D', 'occupation': 'celebrity'}),
 (5, {'category': 'I', 'occupation': 'politician'}),
 (6, {'category': 'D', 'occupation': 'politician'}),
 (7, {'category': 'D', 'occupation': 'scientist'}),
 (8, {'category': 'I', 'occupation': 'celebrity'}),
 (9, {'category': 'D', 'occupation': 'celebrity'}),
 (10, {'category': 'I', 'occupation': 'celebrity'}),
 (11, {'category': 'I', 'occupation': 'celebrity'})]

我似乎不明白如何将.p文件转换为Graph的基本思想。

1 个答案:

答案 0 :(得分:0)

我也遇到了在本地系统上加载datacamp课程中使用的.p文件的相同问题。

$pip freeze | grep networkx

在datacamp练习脚本中使用以上命令,我注意到他们在课程中使用的网络版本为 networkx == 1.1 。恢复到此版本可帮助我解决您遇到的以下错误。

AttributeError: 'DiGraph' object has no attribute '_node'.

希望这会有所帮助。 谢谢