Message Boards Message Boards

0
|
8785 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to create a CSV network in Mathematica?

Posted 10 years ago

Hi everybody. I have created a CSV file representing a network and now I want to import it in Mathematica and represent it. I import my file, but I can't create the graph . The function Graph doesn't work and GraphPlot gives me an error (is not a valid graph). With the same file I'm able to create the network in Gephi. Could you give me please an example of a CSV file and a network created with it in Mathematica? Thank you in advance.

Regards

POSTED BY: Phil R
4 Replies
Posted 10 years ago

Thank you David for having solved my problem, that example was really helpful for me. Now Mathematica creates the network graph.

Regards Filippo

POSTED BY: Phil R
Posted 10 years ago

Mathematica is extremely versatile. If you can understand the format of the csv file, then you can reformat it as needed. Attached is a new notebook. I did not load csv file -- that's easy with Import. But for the array of 7 rows and 5 columns, I assume the format means that for each row, there is a directed edge from the first element to each of the remaining four. The data is therefor transformed by a rule which maps that structure to a list of 4 edge expressions, when applied to a row. When that rule is applied to the data, we get 7 lists of edges. They are flattened because Graph wants a single level list. If you could attach a perhaps abbreviated csv file and some words about its format I could be of more help.

Kind regards,

David

Attachments:
POSTED BY: David Keith
Posted 10 years ago

Thank you David. My network is a bit more complex... So if I have more nodes with more connections between them, I have to write each connection in the csv file as a list of pairs, right? But is there a faster solution? For example I have a complex csv with more rows and columns, with another software like Gephi there are no problems: it recognizes the networks and creates it. In Mathematica this seems to not be possible. Do you have any suggestions? Thank you in advance

POSTED BY: Phil R
Posted 10 years ago

** Note: there was an error in the first post and attached notebook which have been corrected . **

The code will depend on how you've chosen to represent the graph in the csv file. The attached assumes the file is a list of pairs representing edges.

SetDirectory[NotebookDirectory[]];

importedEdges = Import["edges.csv"]

undirectedEdges = MapThread[UndirectedEdge, importedEdges // Transpose]

Graph[undirectedEdges, VertexLabels -> "Name"]

enter image description here

Attachments:
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract