third_party/blink/renderer/build/scripts/cluster.py
def k_means(x_input, n_cluster=3, n_iter=100, n_tries=10):
    """Perform 1-D k-means clustering on a list of numbers x_input

    Args:
        x_input: list of numbers
        n_cluster: number of clusters
        n_iter: number of iterations

    Returns:
        centers: list of n_cluster elements containing the cluster centers
        min_dist_idx: list of len(x_input) elements containing the nearest cluster center's id
        error_value: sum of all distance from each point to its nearest cluster center
    """
    results = []
    for _ in range(n_tries):
        error_value = 0
        rand.seed(None)
        centers = sorted([rand.uniform(0.0, 100.0) for i in range(n_cluster)])
        min_dist_idx = [0] * len(x_input)
        i = 0
        while i < n_iter:
            failed = False
            dist_mat = l2_pairwise_distance(x_input, centers)
            error_value = calculate_error(dist_mat)
            min_dist_idx = [dist.index(min(dist)) for dist in dist_mat]
            centers = [0] * n_cluster
            count = [0] * n_cluster
            for j in range(len(x_input)):
                centers[min_dist_idx[j]] += x_input[j]
                count[min_dist_idx[j]] += 1

            for j in range(n_cluster):
                if count[j] == 0:
                    centers = sorted(
                        [rand.uniform(0.0, 100.0) for i in range(n_cluster)])
                    failed = True
                    break

            if failed:
                i = 0
                continue

            for j in range(n_cluster):
                centers[j] = centers[j] / count[j]
            i += 1

        results.append((centers, min_dist_idx, error_value))

    return min(results, key=lambda x: x[2])
Perform 1-D k-means clustering on a list of numbers x_input

    Args:
        x_input: list of numbers
        n_cluster: number of clusters
        n_iter: number of iterations

    Returns:
        centers: list of n_cluster elements containing the cluster centers
        min_dist_idx: list of len(x_input) elements containing the nearest cluster center's id
        error_value: sum of all distance from each point to its nearest cluster center
    
Perform 1-D k-means clustering on a list of numbers x_input
Perform 1 - D k - means clustering on a list of numbers x_input
def k_means(x_input, n_cluster=3, n_iter=100, n_tries=10):
    results = []
    for _ in range(n_tries):
        error_value = 0
        rand.seed(None)
        centers = sorted([rand.uniform(0.0, 100.0) for i in range(n_cluster)])
        min_dist_idx = [0] * len(x_input)
        i = 0
        while i < n_iter:
            failed = False
            dist_mat = l2_pairwise_distance(x_input, centers)
            error_value = calculate_error(dist_mat)
            min_dist_idx = [dist.index(min(dist)) for dist in dist_mat]
            centers = [0] * n_cluster
            count = [0] * n_cluster
            for j in range(len(x_input)):
                centers[min_dist_idx[j]] += x_input[j]
                count[min_dist_idx[j]] += 1
            for j in range(n_cluster):
                if count[j] == 0:
                    centers = sorted(
                        [rand.uniform(0.0, 100.0) for i in range(n_cluster)])
                    failed = True
                    break
            if failed:
                i = 0
                continue
            for j in range(n_cluster):
                centers[j] = centers[j] / count[j]
            i += 1
        results.append((centers, min_dist_idx, error_value))
    return min(results, key=lambda x: x[2])
def k_means ( x_input , n_cluster = 3 , n_iter = 100 , n_tries = 10 ) : results = [ ] for _ in range ( n_tries ) : error_value = 0 rand . seed ( None ) centers = sorted ( [ rand . uniform ( 0.0 , 100.0 ) for i in range ( n_cluster ) ] ) min_dist_idx = [ 0 ] * len ( x_input ) i = 0 while i < n_iter : failed = False dist_mat = l2_pairwise_distance ( x_input , centers ) error_value = calculate_error ( dist_mat ) min_dist_idx = [ dist . index ( min ( dist ) ) for dist in dist_mat ] centers = [ 0 ] * n_cluster count = [ 0 ] * n_cluster for j in range ( len ( x_input ) ) : centers [ min_dist_idx [ j ] ] += x_input [ j ] count [ min_dist_idx [ j ] ] += 1 for j in range ( n_cluster ) : if count [ j ] == 0 : centers = sorted ( [ rand . uniform ( 0.0 , 100.0 ) for i in range ( n_cluster ) ] ) failed = True break if failed : i = 0 continue for j in range ( n_cluster ) : centers [ j ] = centers [ j ] / count [ j ] i += 1 results . append ( ( centers , min_dist_idx , error_value ) ) return min ( results , key = lambda x : x [ 2 ] )
Perform 1-D k-means clustering on a list of numbers x_input
Perform 1 - D k - means clustering on a list of numbers x_input
"""Perform 1-D k-means clustering on a list of numbers x_input

    Args:
        x_input: list of numbers
        n_cluster: number of clusters
        n_iter: number of iterations

    Returns:
        centers: list of n_cluster elements containing the cluster centers
        min_dist_idx: list of len(x_input) elements containing the nearest cluster center's id
        error_value: sum of all distance from each point to its nearest cluster center
    """
list of n_cluster elements containing the cluster centers
min_dist_idx: list of len(x_input) elements containing the nearest cluster center's id
error_value: sum of all distance from each point to its nearest cluster center
list of n_cluster elements containing the cluster centers min_dist_idx : list of len ( x_input ) elements containing the nearest cluster center ' s id error_value : sum of all distance from each point to its nearest cluster center
centers
x_input
list of numbers
list of numbers
n_cluster
number of clusters
number of clusters
n_iter
number of iterations
number of iterations
n_tries