2016-11-20 46 views
1

我正尝试使用numpy/scipy的k-means算法之一为学校项目执行图像量化(减少图像的颜色数量)。 algirithm工作正常,但我也想计算算法e.i.的每次迭代的误差总和。样本距其最近的聚类中心的距离总和(这是项目任务之一)。 我无法找到任何kmeans方法的numpy或其他快速,优雅的方式执行此操作。 有没有这样一种方法或方法,如果没有,执行这项任务的最佳方法是什么?我的目标是尽量减少现有kmeans算法的重新实现。k表示使用numpy - 计算每次迭代的误差

下面我将我的代码到目前为止

import scipy.cluster.vq as vq 

def quantize_rgb(im_orig, n_quant, n_iter): 
    """ 
    A function that performs optimal quantization of a given RGB image. 
    :param im_orig: the input RGB image to be quantized (float32 image with values in [0, 1]) 
    :param n_quant: the number of intensities the output image should have 
    :param n_iter: the maximum number of iterations of the optimization procedure (may converge earlier.) 
    """ 
    reshaped_im = im_orig.reshape(im_orig.shape[0] * im_orig.shape[1], 3) 
    centroids, label = vq.kmeans2(reshaped_im, n_quant, n_iter) 
    reshaped_im = centroids[label] 
    im_quant = reshaped_im.reshape(im_orig.shape[0], im_orig.shape[1], 3) 

    return im_quant 
+0

看看这个:http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html –

+0

问题是,这个类不能告诉我每次迭代的错误,只有最终结果的错误... –

+0

Aaah OK。嗯...对不起,我从来没有这样做过。我相信'scikit'至少有一个起点。如果不这样做,我恐怕没有其他图书馆会这样做。 –

回答

1

只需使用

vq.kmeans2(k=previous_centers, iter=1, minit="matrix") 

只做一个迭代同时

+0

太棒了!起初我拒绝了这个解决方案,因为我认为kmeans总是使用随机启动点,但'minit =“matrix”'很好地解决了这个问题。 –