def init_huang(X, n_clusters, dissim, random_state):
    """Initialize centroids according to method by Huang [1997]."""
    n_attrs = X.shape[1]
    centroids = np.empty((n_clusters, n_attrs), dtype='object')
    # determine frequencies of attributes
    for iattr in range(n_attrs):
        freq = defaultdict(int)
        for curattr in X[:, iattr]:
            freq[curattr] += 1
        # Sample centroids using the probabilities of attributes.
        # (I assume that's what's meant in the Huang [1998] paper; it works,
        # at least)
        # Note: sampling using population in static list with as many choices
        # as frequency counts. Since the counts are small integers,
        # memory consumption is low.
        choices = [chc for chc, wght in freq.items() for _ in range(wght)]
        # So that we are consistent between Python versions,
        # each with different dict ordering.
        choices = sorted(choices)
        centroids[:, iattr] = random_state.choice(choices, n_clusters)
    # The previously chosen centroids could result in empty clusters,
    # so set centroid to closest point in X.
    for ik in range(n_clusters):
        ndx = np.argsort(dissim(X, centroids[ik]))
        # We want the centroid to be unique, if possible.
        while np.all(X[ndx[0]] == centroids, axis=1).any() and ndx.shape[0] > 1:
            ndx = np.delete(ndx, 0)
        centroids[ik] = X[ndx[0]]

    return centroids
def init_huang ( X , n_clusters , dissim , random_state ) : n_attrs = X . shape [ 1 ] centroids = np . empty ( ( n_clusters , n_attrs ) , dtype = 'object' ) for iattr in range ( n_attrs ) : freq = defaultdict ( int ) for curattr in X [ : , iattr ] : freq [ curattr ] += 1 choices = [ chc for chc , wght in freq . items ( ) for _ in range ( wght ) ] choices = sorted ( choices ) centroids [ : , iattr ] = random_state . choice ( choices , n_clusters ) for ik in range ( n_clusters ) : ndx = np . argsort ( dissim ( X , centroids [ ik ] ) ) while np . all ( X [ ndx [ 0 ] ] == centroids , axis = 1 ) . any ( ) and ndx . shape [ 0 ] > 1 : ndx = np . delete ( ndx , 0 ) centroids [ ik ] = X [ ndx [ 0 ] ] return centroids
Initialize centroids according to method by Huang [1997].