Gworld
Gworld

Reputation: 103

How to perform clustering based on the edges of a graph?

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.

Upvotes: 2

Views: 1383

Answers (1)

Blair
Blair

Reputation: 6693

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)

Upvotes: 1

Related Questions