所以我在下面的脚本时遇到了麻烦,代码可以运行和编译,但是有一些问题。我不确定我的问题是计算问题还是格式问题。我什至不确定自己是否步入正轨,我感觉自己像是我,但我是python的新手,我非常愿意提供任何建议或建议!坚持以下几点:
1)K_means类根本不将质心分配给群集
2)K_means类不执行任何重新计算以使重心居中
下面是我的代码,我只是粘贴了函数,因为我认为这是问题所在:
class K_means():
def __init__(self,k, lines, **kwargs):
self.k = k
self.lines = lines
self.index = []
self.distance = []
self.elements = []
self.points = []
for keys, values in kwargs.items():
setattr(self, keys, values)
def determineDistance(self, centroids, new_dict):
for keys, points in new_dict.items():
self.points.append(points)
for elements in range(len(centroids)):
self.elements.append(elements)
distance = abs(points - centroids[elements])
self.distance.append(distance)
def assign_to_clusters(self, lines, clusters, centroids,
point_assignments, distance, new_dict):
for index, points in new_dict.items():
closest_index = (self.k + 1)
for elements in range(len(centroids)):
distance = self.determineDistance(centroids, new_dict)
if distance < closest_index:
closest_index = distance
index = elements
clusters[index].append(points)
point_assignments[index] = closest_index
return point_assignments
def update_location(self, clusters, centroids):
for keys, values in clusters.items():
new_centroids ={keys:sum(values)/float(len(values))}
centroids.update(new_centroids)
return centroids