【发布时间】:2020-04-15 04:18:30
【问题描述】:
我很生气,因为我使用了(它必须是)两种等效的方法。 我的目标是将图表聚类到不同的组中。为此,一方面我是“手工”完成的,计算fiedler:
import networkx as nx
import numpy.linalg as la
g1 = nx.from_numpy_matrix(A.values )
A = nx.adjacency_matrix(g1)
D = np.diag(np.ravel(np.sum(A,axis=1)))
L=D-A
l, U = la.eigh(L)
# fiedler
f = U[:,1]
labels = np.ravel(np.sign(f))
coord = nx.spring_layout(g1, iterations=100,seed=42)
fig = plt.figure(figsize=(15, 8))
nx.draw_networkx_nodes(g1, coord, node_size=25, node_color=labels, cmap = 'cool')
coord = nx.spectral_layout(g1)
fig = plt.figure(figsize=(15, 8))
nx.draw_networkx_nodes(g1, coord, node_size=25, node_color=labels, cmap = 'cool')
现在,使用这段代码(以更系统的方式):
import networkx as nx
import sklearn
clustering = sklearn.cluster.SpectralClustering(n_clusters=2,
assign_labels="discretize",
random_state=0)
clustering = clustering.fit(A)
labels = clustering.labels_
coord = nx.spring_layout(g1, iterations=100,seed=42)
fig = plt.figure(figsize=(15, 8))
nx.draw_networkx_nodes(g1, coord,node_size=25,node_color=labels, cmap = 'cool')
我得到了一个非常糟糕的结果,看起来完全不一样。
所以我的问题是,为什么?谁能向我解释一下我到底是如何得到不同的结果的?
干杯。
【问题讨论】:
标签: python graph cluster-analysis networkx