Skip to content
Advertisement

How to import csv format Adjacency Matrix hashtag network in CSV file?

My hashtag co-occurrence network is stored as an adjacency matrix in CSV format like this.

,#A,#B,#C,#D,#E,#F,#G,#H,#I,#J,#K
#A,0,1,1,0,1,1,1,1,0,1,0
#B,1,0,0,0,1,1,1,1,0,1,0
#C,1,0,0,0,1,1,1,1,0,1,0
...

Then I use this page as a reference Plot NetworkX Graph from Adjacency Matrix in CSV file

I want to import this matrix into networkx and I tried this:

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from numpy import genfromtxt
import numpy as np

mydata = genfromtxt('data.csv', delimiter=',')

But I only got this:

print(mydata)
print(type(mydata))

[nan nan]
<class 'numpy.ndarray'>

How can I import my data correctly, I want to use the python pipeline to calculate degree centrality of each nodes in the network.And importing is the first step.

Advertisement

Answer

The numpy.genfromtxt function has an optional argument called comments that by default is set to "#".

comments : str, optional
    The character used to indicate the start of a comment.
    All the characters occurring on a line after a comment are discarded

In other words numpy thinks your entire file contains comments and empty rows. You will have to either rename your row and column identifiers or you have to set the comments argument to some other value such as "*"

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement