我在pycharm中运行Lloyd's
算法,并且发生了错误-函数def find_centers(x, k)
中的语法无效:
import numpy as np
import random
def cluster_points(X, mu):
clusters = {}
for x in X:
bestmukey = min([(i[0], np.linalg.norm(x - mu[i[0]])) \
for i in enumerate(mu)], key=lambda t: t[1])[0]
try:
clusters[bestmukey].append(x)
except KeyError:
clusters[bestmukey] = [x]
return clusters
def reevaluate_centers(mu, clusters):
newmu = []
keys = sorted(clusters.keys())
for k in keys:
newmu.append(np.mean(clusters[k], axis=0))
return newmu
def has_converged(mu, oldmu):
return (set([tuple(a) for a in mu]) == set([tuple(a) for a in oldmu])
def find_centers(x, k):
# Initialize to K random centers
oldmu = random.sample(x, k)
mu = random.sample(x, k)
while not has_converged(mu, oldmu):
oldmu = mu
# Assign all points in X to clusters
clusters = cluster_points(x, mu)
# Reevaluate centers
mu = reevaluate_centers(oldmu, clusters)
return (mu, clusters)