def kinit(X, k):
 n = X.shape[0]
 'choose the 1st seed randomly, and store D(x)^2 in D[]'
 centers = [X[random.randint(n)]]
 D = [(norm((x - centers[0])) ** 2) for x in X]
 for _ in range((k - 1)):
  bestDsum = bestIdx = (-1)
  for i in range(n):
   'Dsum = sum_{x in X} min(D(x)^2,||x-xi||^2)'
   Dsum = reduce((lambda x, y: (x + y)), (min(D[j], (norm((X[j] - X[i])) ** 2)) for j in range(n)))
   if ((bestDsum < 0) or (Dsum < bestDsum)):
    (bestDsum, bestIdx) = (Dsum, i)
  centers.append(X[bestIdx])
  D = [min(D[i], (norm((X[i] - X[bestIdx])) ** 2)) for i in range(n)]
 return array(centers)

def kinit ( X , k ) : n = X . shape [ 0 ] centers = [ X [ random . randint ( n ) ] ] D = [ ( norm ( ( x - centers [ 0 ] ) ) ** 2 ) for x in X ] for _ in range ( ( k - 1 ) ) : bestDsum = bestIdx = ( - 1 ) for i in range ( n ) : Dsum = reduce ( ( lambda x , y : ( x + y ) ) , ( min ( D [ j ] , ( norm ( ( X [ j ] - X [ i ] ) ) ** 2 ) ) for j in range ( n ) ) ) if ( ( bestDsum < 0 ) or ( Dsum < bestDsum ) ) : ( bestDsum , bestIdx ) = ( Dsum , i ) centers . append ( X [ bestIdx ] ) D = [ min ( D [ i ] , ( norm ( ( X [ i ] - X [ bestIdx ] ) ) ** 2 ) ) for i in range ( n ) ] return array ( centers )
init k seeds according to kmeans++ .