运行代码时,我得到:
TypeError: 'int' object is not iterable'
:
centroids[key] = [m / len(assignments[key]) for m in clust_sum]
这是我的python代码:
def k_means_clust(data,num_clust,num_iter,w=5):
centroids=random.sample(data,num_clust)
counter=0
for n in range(num_iter):
counter+=1
print counter
assignments={}
#assign data points to clusters
for ind,i in enumerate(data):
min_dist=float('inf')
closest_clust=None
for c_ind,j in enumerate(centroids):
if LB_Keogh(i,j,5)<min_dist:
cur_dist=DTWDistance(i,j,w)
if cur_dist<min_dist:
min_dist=cur_dist
closest_clust=c_ind
if closest_clust in assignments:
assignments[closest_clust].append(ind)
else:
assignments[closest_clust]=[]
#recalculate centroids of clusters
for key in assignments:
clust_sum=0
for k in assignments[key]:
clust_sum=clust_sum+data[k]
centroids[key]=[m/len(assignments[key]) for m in clust_sum] #<-HERE ERROR
return centroids
我不明白出现此错误的原因。
变量clust_sum
是numpy.ndarray
谢谢你。
答案 0 :(得分:2)
您在循环之前重新定义了clust_sum
clust_sum=0
所以clust_sum现在是一个int,您不能对其进行迭代
centroids[key]=[m/len(assignments[key]) for m in clust_sum]
答案 1 :(得分:1)
您只需要在迭代中放入“ clust_sum = 0”:
clust_sum=0
#recalculate centroids of clusters
for key in assignments:
for k in assignments[key]:
clust_sum=clust_sum+data[k]
centroids[key]=[m/len(assignments[key]) for m in clust_sum]
return centroids