我很恼火,因为我使用了(必须的)两个等价的方法。我的目标是将一个图聚类到不同的组中。为此,一方面我“手工”计算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')我得到了一个非常糟糕的结果,看起来一点都不一样。
所以我的问题是,为什么?有人能向我解释一下我到底是如何得到不同的结果的吗?
干杯。
发布于 2019-12-24 22:16:46
通过调用sklearn的谱聚类,您可以使用RBF内核将您的亲和度矩阵解释为坐标。我对此并不感到惊讶,因为它工作得不好。
错误的输入->错误的输出。
试试affinity='precomputed'。
https://stackoverflow.com/questions/59462726
复制相似问题