2

I have a CSV with the following,

x,y,6
y,z,9
y,p,5
x,p,3

here, letters are nodes and numbers are the edges. I drew the graph :

Gph = nx.read_edgelist(filepath, delimiter=',', data=[('weight', int)]) 
G.edges(data=True)

edge_labels = dict(((u, v), d['weight']) for u, v, d in Gph.edges(data=True))
pos = nx.random_layout(Gph)
#pos = nx.pydot_layout(Gph)
nx.draw(Gph, pos, with_labels=True) 
nx.draw_networkx_edges(Gph, pos, edge_color='b')

plt.show() 

how to cluster the nodes based on the edges? so the pairs become element of a cluster. x, y is an element of a the cluster.

Gworld
  • 103
  • 10
  • You seem to be interchanging `Gph` and `G`. Is there more code for this that you aren't showing us or is this just something that came up in transcription? – Blair Jul 18 '14 at 18:17
  • @Brien its just Gph... typo.. i updated it now – Gworld Jul 18 '14 at 18:19

1 Answers1

1

Instead of using nx.random_layout(Gph), one option available to you is to try the spring_layout algorithm provided by networkx. You'll need numpy installed but this force-directed algorithm will cluster the nodes based on the edge weights.

Here is one example of a call to that algorithm, but you can tweak the parameters as needed. Replace the random layout line with this one.

pos = nx.spring_layout(Gph,scale=20)
Blair
  • 6,623
  • 1
  • 36
  • 42
  • I did it with spring_layout. but How can I see the clusters? I want the clusters to be printed by not displayed – Gworld Jul 18 '14 at 18:21
  • Sorry, I don't know of any way to do this. The only thing I can think of would be to run this algorithm then look and see which nodes were close together and which ones were far away from each other. – Blair Jul 18 '14 at 18:35