首页 > 解决方案 > 如何计算 scikit-learn 中输出值的出现次数?

问题描述

我有数据(d),一个片段如下所示:

[[2123, 324, 234, 324, 534, 6435], [43543, 45345, 353, 5345, 543, 5435],..., n]

from sklearn.cluster import KMeans

kmean=KMeans(n_clusters=3)
kmean.fit(d)

# Returns an array of either 0, 1 or 2, where each of these values is the cluster 
# that an element has been assigned to. Example found below.
kmean.labels_ 

出去:

array([0, 1,..., n])

即第一个列表[2123, 324, 234, 324, 534, 6435]对应于集群 0,第二个列表[43543, 45345, 353, 5345, 543, 5435]对应于集群 1。

问题:如何确定分配给每个集群值的元素(列表)的数量?

我试图len(kmean.labels_[0])找到例如 0 的元素的数量,但这没有用。有什么建议么?

标签: pythonlistk-means

解决方案


您可以使用np.bincount(kmean.labels_)


推荐阅读