【问题标题】:How to get initialisation point after sklearn.cluster.KMeans如何在 sklearn.cluster.KMeans 之后获取初始化点
【发布时间】:2020-03-19 14:55:29
【问题描述】:

从 sklearn.cluster 执行 Means 后,我如何知道用于 Means 的初始化点?

对于我的每个集群,我需要返回使用的初始化点的每个特征(原始输入在 Pandas 数据帧中)

【问题讨论】:

  • 你需要属于每个簇的点吗?

标签: pandas scikit-learn k-means


【解决方案1】:
import numpy as np
from sklearn.cluster import KMeans
from sklearn import datasets
np.random.seed(0)

# Use Iris data
iris = datasets.load_iris()
X = iris.data
y = iris.target

# KMeans with 3 clusters
clf =  KMeans(n_clusters=3)
clf.fit(X,y)

#Coordinates of cluster centers with shape [n_clusters, n_features]
clf.cluster_centers_
#Labels of each point
clf.labels_

# Nice Pythonic way to get the indices of the points for each corresponding cluster
mydict = {i: np.where(clf.labels_ == i)[0] for i in range(clf.n_clusters)}

# Transform this dictionary into list (if you need a list as result)
dictlist = []
for key, value in mydict.iteritems():
    temp = [key,value]
    dictlist.append(temp)
print(dictlist)

[[0, array([ 50,  51,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
          64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,
          78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
          91,  92,  93,  94,  95,  96,  97,  98,  99, 101, 106, 113, 114,
         119, 121, 123, 126, 127, 133, 138, 142, 146, 149])],
 [1, array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
         17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
         34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])],
 [2, array([ 52,  77, 100, 102, 103, 104, 105, 107, 108, 109, 110, 111, 112,
         115, 116, 117, 118, 120, 122, 124, 125, 128, 129, 130, 131, 132,
         134, 135, 136, 137, 139, 140, 141, 143, 144, 145, 147, 148])]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    相关资源
    最近更新 更多