编写聚类程序以将数据点放在最近的聚类中。我希望程序打印输出文件看起来像这样。代码的前半部分将点放在正确的集群中。
Point 1.8 in cluster 0
Point 4.5 in cluster 1
Point 1.1 in cluster 2
Point 2.1 in cluster 0
Point 9.8 in cluster 4
这是代码的主要部分
itr_times = 0
while True:
itr_times += 1
clusters = assign_to_clusters(input_points, centroids)
print("Iteration " + str(itr_times))
for index in range(total_clusters):
print(index, clusters[index])
print("")
centroids_updated = update_location(clusters, centroids)
if not centroids_updated:
break
with open(output_file, 'w') as output_file:
for p in input_points:
output_file.write(float("Point {} in cluster {}\n".format(
p, points_cluster_index_dict[p])))
output_file.close