I have been doing Datacamp course called Intro to network analysis (Pt1) and there is a test network in a form of Graph/DiGraph.
In an interactive python shell
on their website I can type T.edges()
, T.nodes()
etc. But I have no idea how to load the same network on my local machine.
The data is provided with .p extension. Click https://mega.nz/#!hs4RhbjC!ukDcb6pDiJSEoAGy-WiosfcMgP62qiQgAAAAAAAAAAA) to access the file.
import networkx as nx
dg = pickle.load(open('../data/tw.p'))
print (dg.edges())
It reads the error
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')
When I try via nx.read_gpickle I get this:
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'
What is shown below is how it should look and I don't know how to make this like so:
Directed Graph from the provided file.
The Twitter network has been loaded as `T`.
In [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'})]
I seem to not understand the basic idea of how to implement the transformation of .p file into Graph.