(G, seeds, maxiter=10)
return (distances, clusters, seeds)
Perform Lloyd clustering on graph with weighted edges.

    Parameters
    ----------
    G : csr_matrix, csc_matrix
        A sparse NxN matrix where each nonzero entry G[i,j] is the distance
        between nodes i and j.
    seeds : int array
        If seeds is an integer, then its value determines the number of
        clusters.  Otherwise, seeds is an array of unique integers between 0
        and N-1 that will be used as the initial seeds for clustering.
    maxiter : int
        The maximum number of iterations to perform.

    Returns
    -------
    distances : array
        final distances
    clusters : int array
        id of each cluster of points
    seeds : int array
        index of each seed

    Notes
    -----
    If G has complex values, abs(G) is used instead.
Perform Lloyd clustering on graph with weighted edges.
Perform Lloyd clustering on graph with weighted edges .
def lloyd_cluster(G, seeds, maxiter=10):
    """Perform Lloyd clustering on graph with weighted edges.

    Parameters
    ----------
    G : csr_matrix, csc_matrix
        A sparse NxN matrix where each nonzero entry G[i,j] is the distance
        between nodes i and j.
    seeds : int array
        If seeds is an integer, then its value determines the number of
        clusters.  Otherwise, seeds is an array of unique integers between 0
        and N-1 that will be used as the initial seeds for clustering.
    maxiter : int
        The maximum number of iterations to perform.

    Returns
    -------
    distances : array
        final distances
    clusters : int array
        id of each cluster of points
    seeds : int array
        index of each seed

    Notes
    -----
    If G has complex values, abs(G) is used instead.

    """
    G = asgraph(G)
    N = G.shape[0]

    if G.dtype.kind == 'c':
        # complex dtype
        G = np.abs(G)

    # interpret seeds argument
    if np.isscalar(seeds):
        seeds = np.random.permutation(N)[:seeds]
        seeds = seeds.astype('intc')
    else:
        seeds = np.array(seeds, dtype='intc')

    if len(seeds) < 1:
        raise ValueError('at least one seed is required')

    if seeds.min() < 0:
        raise ValueError(f'Invalid seed index ({seeds.min()})')
    if seeds.max() >= N:
        raise ValueError(f'Invalid seed index ({seeds.max()})')

    clusters = np.empty(N, dtype='intc')
    distances = np.empty(N, dtype=G.dtype)

    for _it in range(1, maxiter+1):
        last_seeds = seeds.copy()

        amg_core.lloyd_cluster(N, G.indptr, G.indices, G.data,
                               len(seeds), distances, clusters, seeds)

        if (seeds == last_seeds).all():
            break

    if _it == maxiter:
        warn('Lloyd clustering reached maxiter (did not converge)')

    return (distances, clusters, seeds)
def lloyd_cluster ( G , seeds , maxiter = 10 ) : G = asgraph ( G ) N = G . shape [ 0 ] if G . dtype . kind == 'c' : # complex dtype G = np . abs ( G ) # interpret seeds argument if np . isscalar ( seeds ) : seeds = np . random . permutation ( N ) [ : seeds ] seeds = seeds . astype ( 'intc' ) else : seeds = np . array ( seeds , dtype = 'intc' ) if len ( seeds ) < 1 : raise ValueError ( 'at least one seed is required' ) if seeds . min ( ) < 0 : raise ValueError ( f'Invalid seed index ({seeds.min()})' ) if seeds . max ( ) >= N : raise ValueError ( f'Invalid seed index ({seeds.max()})' ) clusters = np . empty ( N , dtype = 'intc' ) distances = np . empty ( N , dtype = G . dtype ) for _it in range ( 1 , maxiter + 1 ) : last_seeds = seeds . copy ( ) amg_core . lloyd_cluster ( N , G . indptr , G . indices , G . data , len ( seeds ) , distances , clusters , seeds ) if ( seeds == last_seeds ) . all ( ) : break if _it == maxiter : warn ( 'Lloyd clustering reached maxiter (did not converge)' ) return ( distances , clusters , seeds )