获取离质心最近的点,scikit-learn? [英] Get nearest point to centroid, scikit-learn?

查看:129
本文介绍了获取离质心最近的点,scikit-learn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 K-means 来解决聚类问题.我试图找到最接近质心的数据点,我认为它被称为中心点.

有没有办法在 scikit-learn 中做到这一点?

解决方案

这不是 medoid,但您可以尝试以下方法:

<预><代码>>>>将 numpy 导入为 np>>>从 sklearn.cluster 导入 KMeans>>>从 sklearn.metrics 导入 pairwise_distances_argmin_min>>>X = np.random.randn(10, 4)>>>km = KMeans(n_clusters=2).fit(X)>>>最近,_ = pairwise_distances_argmin_min(km.cluster_centers_, X)>>>最近的数组([0, 8])

数组 closest 包含 X 中离每个质心最近的点的索引.所以X[0]X中离质心0最近的点,而X[8]是离质心1最近的点.>

I am using K-means for a clustering problem. I am trying to find the data point which is most close to the centroid, which I believe is called the medoid.

Is there a way to do this in scikit-learn?

解决方案

This is not the medoid, but here's something you can try:

>>> import numpy as np
>>> from sklearn.cluster import KMeans
>>> from sklearn.metrics import pairwise_distances_argmin_min
>>> X = np.random.randn(10, 4)
>>> km = KMeans(n_clusters=2).fit(X)
>>> closest, _ = pairwise_distances_argmin_min(km.cluster_centers_, X)
>>> closest
array([0, 8])

The array closest contains the index of the point in X that is closest to each centroid. So X[0] is the closest point in X to centroid 0, and X[8] is the closest to centroid 1.

这篇关于获取离质心最近的点,scikit-learn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆