【发布时间】:2020-07-06 14:03:06
【问题描述】:
我正在寻找解决问题的方法。
我使用 sklearn 的 Kmeans,我想要一本带有 { cluster : list of partition} 的字典
kmeans = KMeans(n_clusters=n)
kmeans.fit(data)
result = zip(data,kmeans.labels_)
sortedR = sorted(result,key=lambda x: x[1])
cluster_nb = {}
for k,v in sortedR:
if v in cluster_nb:
cluster_nb[v].append(k)
else:
cluster_nb[v] = [k]
我将 kmoyen.labels 集群的位置作为键,但我需要 kmoyen.cluster_centers_ 的相应元素
例如:
{'[1,2]' : [array([1, 3]), array([2,4])], '[5,5]' : [array([7, 8]), array([10,12])]}
我尝试了一个新循环:
for x in cluster_nb:
cluster_nb[str(kmeans.cluster_centers_[x])] = cluster_nb.pop(x)
return cluster_nb
但我有这个错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
我在哪里犯错了?
有没有更简单的解决方案?
【问题讨论】:
-
确定:您正在尝试为每个集群检索属于该集群的输入数据的分区?
-
可以,以分区簇坐标为key
标签: python arrays numpy dictionary k-means