【发布时间】:2019-12-31 09:02:54
【问题描述】:
我做了一个 Kmeans 算法并绘制了结果。一切进展顺利,但我想知道哪些人属于哪个组。有没有办法(以及它是什么)从特定群体中获得个人? 谢谢你的回答。
【问题讨论】:
标签: python machine-learning scikit-learn data-science
我做了一个 Kmeans 算法并绘制了结果。一切进展顺利,但我想知道哪些人属于哪个组。有没有办法(以及它是什么)从特定群体中获得个人? 谢谢你的回答。
【问题讨论】:
标签: python machine-learning scikit-learn data-science
你的问题不清楚。
以下是关于您的问题的假设,
- 您想要获取给定数据点的标签
from sklearn.cluster import KMeans
from sklearn import datasets
import numpy as np
centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data
y = iris.target
km = KMeans(n_clusters=3)
km.fit(X)
现在定义一个函数来提取标签,使用 numpy
def ClusterIndices(clustNum, labels_array): #numpy
return np.where(labels_array == clustNum)[0]
现在使用相同的函数检索标签
ClusterIndices(1, km.labels_)
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])
获取数据点
X[ClusterIndicesNumpy(1,km.labels_)]
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],...[4.8, 3. , 1.4, 0.3],
[5.1, 3.8, 1.6, 0.2],
[4.6, 3.2, 1.4, 0.2],
[5.3, 3.7, 1.5, 0.2],
[5. , 3.3, 1.4, 0.2]])
【讨论】:
这是我解决类似任务的方法:
import matplotlib.pyplot as plt
colors = ["g", "r", "m", "c", "y", "k"]
for i in range(len(feature_coords)):
plt.scatter(feature_coords[i][0], feature_coords[i][1], c=colors[kmeans.labels_[i]], s=10)
plt.title('Clusterization of the topics')
plt.show()
【讨论】: