本文介绍了sklearn中的聚集聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些数据,还有这些数据点的成对距离矩阵.我想使用聚集聚类对它们进行聚类.我读到在sklearn中,我们可以将预先计算"为亲和力,我希望它是距离矩阵.但是我找不到任何使用预先计算的亲和力和自定义距离矩阵的示例.任何帮助将不胜感激.解决方案
我们将距离矩阵称为D
.
agg = AgglomerativeClustering(n_clusters=5, affinity='precomputed', linkage = 'average')
agg.fit_predict(D) # Returns class labels.
如果您有兴趣生成整个层次结构并生成树状图,则scikit-learn
的API会包装 scipy
分层群集代码.只需直接使用scipy
代码即可.
I have some data and also the pairwise distance matrix of these data points. I want to cluster them using Agglomerative clustering. I readthat in sklearn, we can have 'precomputed' as affinity and I expect it is the distance matrix. But I could not find any example which uses precomputed affinity and a custom distance matrix.Any help will be appreciated.
解决方案
Let's call your distance matrix D
.
agg = AgglomerativeClustering(n_clusters=5, affinity='precomputed', linkage = 'average')
agg.fit_predict(D) # Returns class labels.
If you're interested in generating the entire hierarchy and producing a dendrogram, scikit-learn
's API wraps the scipy
hierarchical clustering code. Just use the scipy
code directly.
这篇关于sklearn中的聚集聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!