Home > Enterprise >  Kmeans on more than 2 dimensions
Kmeans on more than 2 dimensions

Time:11-18

I have my own algorithm to classify kmeans

# parameter c is for how many cluster do you want
def kmeans(data, c, iter, state):
  np.random.seed(state)

  m=data.shape[0] #number of training examples 
  n=data.shape[1] #number of features 
  Centroids=np.array([]).reshape(n,0)

  for i in range(c):
    rand=np.random.randint(0,m-1)
    Centroids=np.c_[Centroids,data[rand]]
  
  result = {}

  for i in range(iter):      
      distance=np.array([]).reshape(m,0) #Euclidian distance
      for k in range(c):
          tempDist=np.sum((data-Centroids[:,k])**2,axis=1)
          distance=np.c_[distance,tempDist]
      C=np.argmin(distance,axis=1) 1
     
      Y={}
      for k in range(c):
          Y[k 1]=np.array([]).reshape(2,0)
      for i in range(m):          
          Y[C[i]]=np.c_[Y[C[i]],X[i]]     
      for k in range(c):
          Y[k 1]=Y[k 1].T
      for k in range(c):
          Centroids[:,k]=np.mean(Y[k 1],axis=0)
      result=Y

  return result

I've tested my code to classify kmeans of 2 dimensional data and it succeed

# This 2 dimensional data is just for example
Xd = [[7,0],[0,3],[3,4],[4,6],[7,1],[2,4]]
Z=kmeans(Xd,3,500,0)
print(Z)
>>>Z = {1: [[7,0],[7,1]], 
        2: [[3,4],[2,4]],
        3: [[0,3],[4,6]]} 

But when I replace Xd with variable that has 784 dimension, it shows error on this line:

Y[C[i]]=np.c_[Y[C[i]],X[i]]  
>>>all the input array dimensions for the concatenation axis must match exactly, 
but along dimension 0, the array at index 0 has size 2 and the array at index 1 has size 784

What should I do?

CodePudding user response:

Y[k 1]=np.array([]).reshape(2,0)

Instead of 2, this array should match the dimension of the one you're concatenating against.

  • Related