尝试运行K-Means时出现Python错误
我正在尝试编写K-means算法,我认为我已经完成了,所以我开始测试,它给了我一个错误:尝试运行K-Means时出现Python错误,python,k-means,Python,K Means,我正在尝试编写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 以下是我的更新集群定义代码: def
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
以下是我的更新集群定义代码:
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时它无法识别它。任何帮助都会很好。CurrentCluster有3个R感谢您。我知道这很简单