How to find cluster centroid with Scikit-learn [closed]

柔情痞子 提交于 2020-02-21 07:24:32

问题


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 is preferred. Thanks.


回答1:


Straight from the docs:

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]]


来源:https://stackoverflow.com/questions/50332786/how-to-find-cluster-centroid-with-scikit-learn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!