Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import matrix from excel into numpy and then graph it? [closed]

I have a matrix in excel that I am trying to import and convert to a numpy matrix and then graph it with networkx how would I go about doing this? I do have some code but not sure if I am going about doing this correctly.

import networkx as nx
import pandas as pd
import numpy 
from numpy import genfromtxt

df=numpy.recfromcsv("Correlation_test.csv", delimiter=',', skiprows=1)

nx.Graph(df)

Thanks

This is what I have so far but, I keep getting an error saying "Input is not a correct numpy matrix or array".

like image 351
user3083785 Avatar asked Dec 29 '25 23:12

user3083785


1 Answers

If the data is a CSV file that looks like:

my_graph.csv

My graph data
0,1,1,0,0
1,0,0,1,0
1,0,0,1,1
0,1,1,0,1
0,0,1,1,0

It's simple to load and plot the resulting graph:

import numpy as np
import networkx as nx
import pylab as plt

A = np.genfromtxt("my_graph.csv",delimiter=',',skiprows=1)
G = nx.Graph(A)
nx.draw(G)
plt.show()

enter image description here

like image 181
Hooked Avatar answered Dec 31 '25 16:12

Hooked



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!