3

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.

sharkzeeh
  • 59
  • 1
  • 4
  • 1
    Does `nx.read_gpickle(the_filename)` give you the graph if you run that in an interpreter... Or... are you asking how you install jupyter notebook for instance? – Jon Clements Aug 26 '18 at 14:48
  • Do not post text as images. See https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question. You can copy-paste text in here. – Nic3500 Aug 26 '18 at 16:45
  • @JonClements I've added the traceback. I don't really know how it's done in the wild. For now I've tried 2 different options and it didn't work out in any of the cases. Could you try the file on your machine? Or may it be something wrong with the file? – sharkzeeh Aug 26 '18 at 18:00
  • @Nic3500 I've edited the Q – sharkzeeh Aug 26 '18 at 18:01

1 Answers1

0

I also encountered the same problem of loading .p file used in datacamp course on my local system.

$pip freeze | grep networkx

Using above command in datacamp practice script,I noticed that the version of networks they are using in the course is networkx==1.1 . Reverting back to this version helped me to solve the following error that you are getting.

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

Hope this helps. Thanks

Pradeep
  • 25
  • 6