如何使用Scikit-learn查找聚类质心 [英] How to find cluster centroid with Scikit-learn
本文介绍了如何使用Scikit-learn查找聚类质心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有(标记为)聚类的数据集.我试图找到每个群集的质心(一个矢量,它的距离从群集的所有数据点最小.)
I have a data set with (labeled) clusters. I'm trying to find the centroids of each cluster (a vector that his distance is the smallest from all data points of the cluster).
我找到了许多执行聚类的解决方案,然后才找到了质心,但是对于现有的质心我还没有找到.
I found many solutions to perform clustering and only then find the centroids, but I didn't find yet for existing ones.
Python schikit-learn是首选.谢谢.
Python schikit-learn is preferred. Thanks.
推荐答案
直接从文档:
from sklearn.neighbors.nearest_centroid import NearestCentroid
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
y = np.array([1, 1, 1, 2, 2, 2])
clf = NearestCentroid()
clf.fit(X, y)
print(clf.centroids_)
# [[-2. -1.33333333]
# [ 2. 1.33333333]]
这篇关于如何使用Scikit-learn查找聚类质心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文