kmeans 集群中节点与质心之间的距离?
Distance between nodes and the centroid in a kmeans cluster?
提取 kmeans 集群中节点与质心之间距离的任何选项。
我已经对文本嵌入数据集进行了 Kmeans 聚类,我想知道在每个聚类中哪些节点远离质心,以便我可以检查相应节点的特征有区别。
提前致谢!
如果您正在使用 Python 和 sklearn。
从这里开始:
https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans
你可以获得 labels_
和 cluster_centers_
。
现在,您确定采用每个节点及其聚类中心的向量的距离函数。按 labels_
过滤并计算每个标签内每个点的距离。
KMeans.transform()
returns每个样本到聚类中心的距离数组。
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import seaborn as sns
# Generate some random clusters
X, y = make_blobs()
kmeans = KMeans(n_clusters=3).fit(X)
# plot the cluster centers and samples
sns.scatterplot(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1],
marker='+',
color='black',
s=200);
sns.scatterplot(X[:,0], X[:,1], hue=y,
palette=sns.color_palette("Set1", n_colors=3));
transform
X 并取每行的总和 (axis=1
) 以识别离中心最远的样本。
# squared distance to cluster center
X_dist = kmeans.transform(X)**2
# do something useful...
import pandas as pd
df = pd.DataFrame(X_dist.sum(axis=1).round(2), columns=['sqdist'])
df['label'] = y
df.head()
sqdist label
0 211.12 0
1 257.58 0
2 347.08 1
3 209.69 0
4 244.54 0
视觉检查 -- 相同的图,只是这次突出显示了距每个聚类中心最远的点:
# for each cluster, find the furthest point
max_indices = []
for label in np.unique(kmeans.labels_):
X_label_indices = np.where(y==label)[0]
max_label_idx = X_label_indices[np.argmax(X_dist[y==label].sum(axis=1))]
max_indices.append(max_label_idx)
# replot, but highlight the furthest point
sns.scatterplot(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1],
marker='+',
color='black',
s=200);
sns.scatterplot(X[:,0], X[:,1], hue=y,
palette=sns.color_palette("Set1", n_colors=3));
# highlight the furthest point in black
sns.scatterplot(X[max_indices, 0], X[max_indices, 1], color='black');
凯文在上面有一个很好的答案,但我觉得它没有回答所问的问题(也许我读的完全错了)。如果您试图查看每个单独的聚类中心并获取该聚类中距离中心最远的点,您将需要使用聚类标签来获取每个点到该聚类质心的距离。上面的代码只是在每个集群中找到距离所有其他集群中心最远的点(你可以在图片中看到,这些点总是在远离其他 2 个集群的集群的远侧)。为了查看各个集群,您需要如下内容:
center_dists = np.array([X_dist[i][x] for i,x in enumerate(y)])
这将为您提供每个点与其簇的质心的距离。然后通过 运行 与 Kevin 上面几乎相同的代码,它会给你每个集群中最远的点。
max_indices = []
for label in np.unique(kmeans.labels_):
X_label_indices = np.where(y==label)[0]
max_label_idx = X_label_indices[np.argmax(center_dists[y==label])]
max_indices.append(max_label_idx)
提取 kmeans 集群中节点与质心之间距离的任何选项。
我已经对文本嵌入数据集进行了 Kmeans 聚类,我想知道在每个聚类中哪些节点远离质心,以便我可以检查相应节点的特征有区别。
提前致谢!
如果您正在使用 Python 和 sklearn。
从这里开始: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans
你可以获得 labels_
和 cluster_centers_
。
现在,您确定采用每个节点及其聚类中心的向量的距离函数。按 labels_
过滤并计算每个标签内每个点的距离。
KMeans.transform()
returns每个样本到聚类中心的距离数组。
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import seaborn as sns
# Generate some random clusters
X, y = make_blobs()
kmeans = KMeans(n_clusters=3).fit(X)
# plot the cluster centers and samples
sns.scatterplot(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1],
marker='+',
color='black',
s=200);
sns.scatterplot(X[:,0], X[:,1], hue=y,
palette=sns.color_palette("Set1", n_colors=3));
transform
X 并取每行的总和 (axis=1
) 以识别离中心最远的样本。
# squared distance to cluster center
X_dist = kmeans.transform(X)**2
# do something useful...
import pandas as pd
df = pd.DataFrame(X_dist.sum(axis=1).round(2), columns=['sqdist'])
df['label'] = y
df.head()
sqdist label
0 211.12 0
1 257.58 0
2 347.08 1
3 209.69 0
4 244.54 0
视觉检查 -- 相同的图,只是这次突出显示了距每个聚类中心最远的点:
# for each cluster, find the furthest point
max_indices = []
for label in np.unique(kmeans.labels_):
X_label_indices = np.where(y==label)[0]
max_label_idx = X_label_indices[np.argmax(X_dist[y==label].sum(axis=1))]
max_indices.append(max_label_idx)
# replot, but highlight the furthest point
sns.scatterplot(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1],
marker='+',
color='black',
s=200);
sns.scatterplot(X[:,0], X[:,1], hue=y,
palette=sns.color_palette("Set1", n_colors=3));
# highlight the furthest point in black
sns.scatterplot(X[max_indices, 0], X[max_indices, 1], color='black');
凯文在上面有一个很好的答案,但我觉得它没有回答所问的问题(也许我读的完全错了)。如果您试图查看每个单独的聚类中心并获取该聚类中距离中心最远的点,您将需要使用聚类标签来获取每个点到该聚类质心的距离。上面的代码只是在每个集群中找到距离所有其他集群中心最远的点(你可以在图片中看到,这些点总是在远离其他 2 个集群的集群的远侧)。为了查看各个集群,您需要如下内容:
center_dists = np.array([X_dist[i][x] for i,x in enumerate(y)])
这将为您提供每个点与其簇的质心的距离。然后通过 运行 与 Kevin 上面几乎相同的代码,它会给你每个集群中最远的点。
max_indices = []
for label in np.unique(kmeans.labels_):
X_label_indices = np.where(y==label)[0]
max_label_idx = X_label_indices[np.argmax(center_dists[y==label])]
max_indices.append(max_label_idx)