【发布时间】:2017-08-06 21:24:40
【问题描述】:
我正在尝试使用 sklearn 的凝聚聚类命令执行约束聚类。为了使算法受到约束,它需要一个“连接矩阵”。这被描述为:
连接性约束是通过连接性矩阵施加的:一个 scipy 稀疏矩阵,其元素仅位于行和列的交叉点,其中包含应连接的数据集的索引。这个矩阵可以从先验信息构建:例如,您可能希望通过仅合并具有从一个指向另一个指向的链接的页面来对网页进行聚类。
我有一个观察对列表,我希望算法强制它们保持在同一个集群中。我可以将其转换为稀疏的scipy 矩阵(coo 或csr),但生成的集群无法强制约束。
一些数据:
import numpy as np
import scipy as sp
import pandas as pd
import scipy.sparse as ss
from sklearn.cluster import AgglomerativeClustering
# unique ids
ids = np.arange(10)
# Pairs that should belong to the same cluster
mustLink = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=['A', 'B'])
# Features for training the model
data = pd.DataFrame([
[.0873,-1.619,-1.343],
[0.697456, 0.410943, 0.804333],
[-1.295829, -0.709441, -0.376771],
[-0.404985, -0.107366, 0.875791],
[-0.404985, -0.107366, 0.875791],
[-0.515996, 0.731980, -1.569586],
[1.024580, 0.409148, 0.149408],
[-0.074604, 1.269414, 0.115744],
[-0.006706, 2.097276, 0.681819],
[-0.432196, 1.249149,-1.159271]])
将这些对转换为“连接矩阵”:
# Blank coo matrix to csr
sm = ss.coo_matrix((len(ids), len(ids)), np.int32).tocsr()
# Insert 1 for connected pairs and diagonals
for i in np.arange(len(mustLink)): # add links to both sides of the matrix
sm[mustLink.loc[i, 'A'], mustLink.loc[i, 'B']] = 1
sm[mustLink.loc[i, 'B'], mustLink.loc[i, 'A']] = 1
for i in np.arange(sm.tocsr()[1].shape[1]): # add diagonals
sm[i,i] = 1
sm = sm.tocoo() # convert back to coo format
训练和拟合凝聚聚类模型:
m = AgglomerativeClustering(n_clusters=6, connectivity=sm)
out = m.fit_predict(X=data)
我收到的警告:
UserWarning:连通矩阵的连通分量数为 7 > 1。完成它以避免过早停止树。 连通性,n_components = _fix_connectivity(X, 连通性)
除了不祥的警告之外,我希望的对属于同一个集群,但事实并非如此。
这是因为 sklearn 算法不是为处理 mustlink 约束而设计的,而是只能使用distance 矩阵(区分here)?
【问题讨论】:
-
你的代码不是这样运行的 :) 也许值得在新的 python 会话中自己测试一下。
-
更新了!感谢您的提醒
-
您的连接矩阵无效。所有点必须连接在一起,因为凝聚聚类将层次结构中的所有点聚集在一起。 (您可以将其视为必须形成一个不能不相交的图形的所有点。可以关闭节点之间的边)。这就是您收到警告的原因。没有很好的方法来做你正在尝试用凝聚集群做的事情。
-
谢谢,@y300。如果您想将该评论放大为完整的回复,我将奖励您赏金。这就是我一直在寻找的。span>
标签: python scikit-learn hierarchical-clustering