def _k_modes_iter(X, centroids, cl_attr_freq, membship, dissim, random_state):
    """Single iteration of k-modes clustering algorithm"""
    moves = 0
    for ipoint, curpoint in enumerate(X):
        clust = np.argmin(dissim(centroids, curpoint, X=X, membship=membship))
        if membship[clust, ipoint]:
            # Point is already in its right place.
            continue

        # Move point, and update old/new cluster frequencies and centroids.
        moves += 1
        old_clust = np.argwhere(membship[:, ipoint])[0][0]

        cl_attr_freq, membship, centroids = move_point_cat(
            curpoint, ipoint, clust, old_clust, cl_attr_freq, membship, centroids
        )

        # In case of an empty cluster, reinitialize with a random point
        # from the largest cluster.
        if not membship[old_clust, :].any():
            from_clust = membship.sum(axis=1).argmax()
            choices = [ii for ii, ch in enumerate(membship[from_clust, :]) if ch]
            rindx = random_state.choice(choices)

            cl_attr_freq, membship, centroids = move_point_cat(
                X[rindx], rindx, old_clust, from_clust, cl_attr_freq, membship, centroids
            )

    return centroids, moves
def _k_modes_iter ( X , centroids , cl_attr_freq , membship , dissim , random_state ) : moves = 0 for ipoint , curpoint in enumerate ( X ) : clust = np . argmin ( dissim ( centroids , curpoint , X = X , membship = membship ) ) if membship [ clust , ipoint ] : continue moves += 1 old_clust = np . argwhere ( membship [ : , ipoint ] ) [ 0 ] [ 0 ] cl_attr_freq , membship , centroids = move_point_cat ( curpoint , ipoint , clust , old_clust , cl_attr_freq , membship , centroids ) if not membship [ old_clust , : ] . any ( ) : from_clust = membship . sum ( axis = 1 ) . argmax ( ) choices = [ ii for ii , ch in enumerate ( membship [ from_clust , : ] ) if ch ] rindx = random_state . choice ( choices ) cl_attr_freq , membship , centroids = move_point_cat ( X [ rindx ] , rindx , old_clust , from_clust , cl_attr_freq , membship , centroids ) return centroids , moves
Single iteration of k-modes clustering algorithm