(data, k, t=0.0001, distfun=None, maxiter=50, centers=None)
return labels, c
One-dimensional K-means clustering function.

    :param data: list of data points.
    :param k: number of clusters.
    :param t: tolerance; stop if changes between iterations are smaller than
        this value.
    :param distfun: a distance function.
    :param centers: a list of centroids to start with.
    :param maxiter: maximum number of iterations to run.
One-dimensional K-means clustering function.
One - dimensional K - means clustering function .
def kmeans(data, k, t=0.0001, distfun=None, maxiter=50, centers=None):
    """
    One-dimensional K-means clustering function.

    :param data: list of data points.
    :param k: number of clusters.
    :param t: tolerance; stop if changes between iterations are smaller than
        this value.
    :param distfun: a distance function.
    :param centers: a list of centroids to start with.
    :param maxiter: maximum number of iterations to run.
    """

    # Adapted from a C version by Roger Zhang, <rogerz@cs.dal.ca>
    # http://cs.smu.ca/~r_zhang/code/kmeans.c

    DOUBLE_MAX = 1.797693e308
    n = len(data)

    error = DOUBLE_MAX  # sum of squared euclidean distance

    counts = [0] * k  # size of each cluster
    labels = [0] * n  # output cluster label for each data point

    # c1 is an array of len k of the temp centroids
    c1 = [0] * k

    # choose k initial centroids
    if centers:
        c = centers
    else:
        c = random.sample(data, k)

    niter = 0
    # main loop
    while True:
        # save error from last step
        old_error = error
        error = 0

        # clear old counts and temp centroids
        for i in xrange(k):
            counts[i] = 0
            c1[i] = 0

        for h in xrange(n):
            # identify the closest cluster
            min_distance = DOUBLE_MAX
            for i in xrange(k):
                distance = (data[h] - c[i]) ** 2
                if distance < min_distance:
                    labels[h] = i
                    min_distance = distance

            # update size and temp centroid of the destination cluster
            c1[labels[h]] += data[h]
            counts[labels[h]] += 1
            # update standard error
            error += min_distance

        for i in xrange(k):  # update all centroids
            c[i] = c1[i] / counts[i] if counts[i] else c1[i]

        niter += 1
        if (abs(error - old_error) < t) or (niter > maxiter):
            break

    return labels, c
def kmeans ( data , k , t = 0.0001 , distfun = None , maxiter = 50 , centers = None ) : # Adapted from a C version by Roger Zhang, <rogerz@cs.dal.ca> # http://cs.smu.ca/~r_zhang/code/kmeans.c DOUBLE_MAX = 1.797693e308 n = len ( data ) error = DOUBLE_MAX # sum of squared euclidean distance counts = [ 0 ] * k # size of each cluster labels = [ 0 ] * n # output cluster label for each data point # c1 is an array of len k of the temp centroids c1 = [ 0 ] * k # choose k initial centroids if centers : c = centers else : c = random . sample ( data , k ) niter = 0 # main loop while True : # save error from last step old_error = error error = 0 # clear old counts and temp centroids for i in xrange ( k ) : counts [ i ] = 0 c1 [ i ] = 0 for h in xrange ( n ) : # identify the closest cluster min_distance = DOUBLE_MAX for i in xrange ( k ) : distance = ( data [ h ] - c [ i ] ) ** 2 if distance < min_distance : labels [ h ] = i min_distance = distance # update size and temp centroid of the destination cluster c1 [ labels [ h ] ] += data [ h ] counts [ labels [ h ] ] += 1 # update standard error error += min_distance for i in xrange ( k ) : # update all centroids c [ i ] = c1 [ i ] / counts [ i ] if counts [ i ] else c1 [ i ] niter += 1 if ( abs ( error - old_error ) < t ) or ( niter > maxiter ) : break return labels , c