3

Intro to network analysis (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 ファイルからグラフへの変換を実装する方法の基本的な考え方を理解していないようです。

4

1 に答える 1