【问题标题】:How To Plot n Furthest Points From Each Centroid KMeans如何从每个质心 KMeans 绘制 n 个最远点
【发布时间】:2020-09-17 14:20:55
【问题描述】:

我正在尝试用 Python 在 iris 数据集上训练一个 kmeans 模型。

有没有办法在 Python 中使用 kmeans 绘制离每个质心最远的 n 个点?

这是一个完整的工作代码:

from sklearn import datasets
from sklearn.cluster import KMeans
import numpy as np

# import iris dataset
iris = datasets.load_iris()
X = iris.data[:, 2:5] # use two variables

# plot the two variables to check number of clusters
import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1]) 
 
# kmeans
km = KMeans(n_clusters = 2, random_state = 0) # Chose two clusters
y_pred = km.fit_predict(X)

X_dist = kmeans.transform(X) # get distances to each centroid

## Stuck at this point: How to make a function that extracts three points that are furthest from the two centroids
max3IdxArr = []
for label in np.unique(km.labels_):
    X_label_indices = np.where(y_pred == label)[0]
    # max3Idx = X_label_indices[np.argsort(X_dist[:3])] # This part is wrong
    max3Idx = X_label_indices[np.argsort(X_dist[:3])] # This part is wrong
    max3IdxArr.append(max3Idx)
  
max3IdxArr

# plot
plt.scatter(X[:, 0].iloc[max3IdxArr], X[:, 1].iloc[max3IdxArr])

【问题讨论】:

    标签: python-3.x k-means


    【解决方案1】:

    你所做的是np.argsort(X_dist[:3])

    它已经从未排序的X_dist 中获取前三个值,因此您可以

    尝试使用x=np.argsort(x_dist)

    排序完成后可以尝试

    x[:3]

    欢迎提问, 如果这不起作用

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-05
      • 2019-11-20
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 2020-12-13
      • 2021-03-03
      • 2020-03-09
      相关资源
      最近更新 更多