我正在实现K-means算法,我需要计算分配给质心的点数。
在类Point
中,我有一个名为centroid
的字段。
我能以某种方式使用Python的count来计算分配给当前质心的Point
对象的数量吗?你知道吗
我的代码:
def updateCentroids(centroids, pixelList):
k = len(centroids)
centoidsCount = [0]*k #couts how many pixels classified for each cent.
centroidsSum = np.zeros([k, 3])#sum value of centroids
for pixel in pixelList:
index = 0
#find whitch centroid equals
for centroid in centroids:
if np.array_equal(pixel.classification, centroid):
centoidsCount[index] += 1
centroidsSum[index] += pixel.point
break
index += 1
index = 0
for centroid in centroidsSum:
centroids[index] = centroid/centoidsCount[index]
index += 1
如果我对你的理解是正确的,你想做以下几点:
这个问题已经得到了回答。但我想补充一点:
您正在查询列表中的两件事:
pixel.point
在代码中)。你知道吗这将需要两次迭代而不是一次-只需使用一个for循环。你知道吗
相关问题 更多 >
编程相关推荐