【问题标题】:sklearn agglomerative clustering input datasklearn 凝聚聚类输入数据
【发布时间】:2023-03-12 20:53:02
【问题描述】:

我有四个用户之间的相似度矩阵。我想做一个凝聚聚类。代码是这样的:

lena = np.matrix('1 1 0 0;1 1 0 0;0 0 1 0.2;0 0 0.2 1')
X = np.reshape(lena, (-1, 1))

print("Compute structured hierarchical clustering...")
st = time.time()
n_clusters = 3 # number of regionsle


ward = AgglomerativeClustering(n_clusters=n_clusters,
        linkage='complete').fit(X)
print ward
label = np.reshape(ward.labels_, lena.shape)
print("Elapsed time: ", time.time() - st)
print("Number of pixels: ", label.size)
print("Number of clusters: ", np.unique(label).size)
print label

标签的打印结果如下:

[[1 1 0 0]
 [1 1 0 0]
 [0 0 1 2]
 [0 0 2 1]]

这是否意味着它给出了可能的聚类结果列表,我们可以从中选择一个?喜欢选择:[0,0,2,1]。如果错了,你能告诉我如何做基于相似度的凝聚算法吗?如果是对的,相似度矩阵很大,我如何从一个巨大的列表中选择最优的聚类结果?谢谢

【问题讨论】:

    标签: python scikit-learn hierarchical-clustering


    【解决方案1】:

    我认为这里的问题是你用错误的数据拟合你的模型

    # This will return a 4x4 matrix (similarity matrix)
    lena = np.matrix('1 1 0 0;1 1 0 0;0 0 1 0.2;0 0 0.2 1')
    
    # However this will return 16x1 matrix
    X = np.reshape(lena, (-1, 1))
    

    你得到的真实结果是这样的:

     ward.labels_
     >> array([1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 2, 0, 0, 2, 1])
    

    X 向量中每个元素的标签是什么,它没有意义

    如果我很好地理解了您的问题,您需要根据用户之间的距离(相似度)对用户进行分类。好吧,在这种情况下,我建议以这种方式使用谱聚类:

    import numpy as np
    from sklearn.cluster import SpectralClustering
    
    lena = np.matrix('1 1 0 0;1 1 0 0;0 0 1 0.2;0 0 0.2 1')
    
    n_clusters = 3
    SpectralClustering(n_clusters).fit_predict(lena)
    
    >> array([1, 1, 0, 2], dtype=int32)
    

    【讨论】:

    • 但是如果他需要层次聚类呢?
    猜你喜欢
    • 2017-12-03
    • 2016-07-29
    • 2015-01-07
    • 2019-05-19
    • 2018-04-29
    • 1970-01-01
    • 2016-11-18
    • 2014-06-28
    相关资源
    最近更新 更多