我试图编写K-means算法,我以为我已经完成了,所以我开始测试并且它给了我错误:
File "kmeans.py", line 112, in update_clusters
if(data[i].get_cluster() is None or data[i].get_cluster() != currrentCluster):
NameError: global name 'currrentCluster' is not defined
这是我的update_clusters定义的代码:
def update_clusters():
isStillMoving = 0
for i in range(total_data):
bestmin = big_num
currentCluster = 0
for j in range(nclusters):
distance = get_dist(data[i].get_x(), data[i].get_y(), centroids[j].get_x(), centroids[j].get_y())
if(distance < bestmin):
bestmin = distance
currentCluster = j
data[i].set_cluster(currentCluster)
if(data[i].get_cluster() is None or data[i].get_cluster() != currrentCluster):
data[i].set_cluster(currentCluster)
isStillMoving = 1
return isStillMoving
我不确定为什么当我在开头定义它时无法识别currentCluster。任何帮助都会很棒。