K-表示图像压缩中的numpy错误

时间:2017-03-17 18:23:39

标签: python numpy image-processing k-means

我一直在尝试使用像素作为数据来实现用于图像压缩的k均值,并使用k作为质心数。我一直收到错误:IndexError: invalid index to scalar variable.;在if assignment[i] == j:的比较语句中。我决定先计算初始质心,然后用它将数据点聚类到正确的质心。然后使用赋值变量重新计算质心到argmin。

我的代码:

def mykmeans(pixels, K):
    k=int(K)
    #number of pixels dimension
    pixel_num=pixels.shape[0]
    print('Pixel num',pixel_num)
    pixel_rand1=np.random.randint(pixel_num,size=(1,k))
    int_cent=pixels[pixel_rand1[0],:] #new centroids
    #for initial centroids
    dis_temp1=np.array((pixel_num,k))
    assignment= [None]*pixel_num
    for i in range(pixel_num):
        for j in range(k):
            dis_temp1[j]=linalg.norm(pixels[i]-int_cent[j])**2
    assignment=np.argmin(dis_temp1)
    print('Assignemnt',int(assignment))
    x_new=int_cent
    new_assign=np.array([])
    for i in range(pixel_num):
        for j in range(k):
            if assignment[i] is j:
                new_assign += assignment[i]
    #for after initial centroid
    while (linalg.norm(x_new-x_old) <1e-4):
        #part 1
        for i in range(pixel_num):
            for j in range(k):
                dis_temp[j]=linalg.norm(pixels[i]-x_old[j])**2
        assignment[i]=np.argmin(dis_temp)
        #        
        #        #part 2
        new_assign=[]
        for i in range(pixel_num):
            for j in range(k):
                if assignment[i] == j:
                    new_assign += assignment[i]
            x_new[j]=np.mean(new_assign,axis=0)
            centroid=x_new

    print(assignment)
    print(centroid)
    return assignment, centroid

1 个答案:

答案 0 :(得分:0)

错误告诉您assignment是标量变量。

因此,您无法通过索引访问assignment,因此assignment[i] = j会给您错误。

assignment设置在assignment=np.argmin(dis_temp1),其中dist_temp1是一维数组。

当您将一维数组传递给np.argmin时,它会返回一个标量:请参阅文档here

最初设置assignment= [None]*pixel_num,因此您需要小心重新分配变量。

您还应该看看如何定义dis_temp,因为以下内容会为k>2生成错误

for i in range(pixel_num):
  for j in range(k):
      dis_temp1[j]=linalg.norm(pixels[i]-int_cent[j])**2

也许您打算将dis_temp1 = np.zeros((pixel_num,k))和循环定义为

for i in range(pixel_num):
  for j in range(k):
      dis_temp1[i][j]=linalg.norm(pixels[i]-int_cent[j])**2
  assignment[i]=np.argmin(dis_temp1)

这会将代码转发到尚未定义x_old的新错误。