如何根据图的边缘进行聚类?
我有一个CSV文件,内容如下:
x,y,6
y,z,9
y,p,5
x,p,3
在这里,字母代表节点,数字代表边。我画了这个图:
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()
我想知道如何根据这些边来把节点分成不同的组?这样一来,成对的节点就会成为一个组的元素。比如,x和y就是这个组里的元素。
1 个回答
1
如果你不想使用 nx.random_layout(Gph)
,你可以试试 spring_layout
这个算法,它是 networkx
提供的。使用这个算法之前,你需要先安装 numpy
。这个基于力导向的算法会根据边的权重来聚集节点。
下面是一个调用这个算法的例子,你可以根据需要调整参数。把随机布局的那一行替换成这个就可以了。
pos = nx.spring_layout(Gph,scale=20)