python_data_scien/algorithmic_thinking2/project3/alg_project3_solution.py
def kmeans_clustering(cluster_list, len_k, iter_times_q):
    """
    Compute the len_k-means clustering of a set of clusters
    Note: the function may not mutate cluster_list
    
    Input: List of clusters, integers number of clusters and number of iterations
    Output: List of clusters whose length is num_clusters
    """

    # position initial clusters at the location of clusters with largest populations
            
    cluster_list_copy = list(cluster_list)
    cluster_list_copy.sort(key = lambda cluster: cluster.total_population(),reverse=True)
    miu = []
    for idx_f in range(len_k):
        miu.append((cluster_list_copy[idx_f].horiz_center(),cluster_list_copy[idx_f].vert_center()))

    for dummy_i in range(iter_times_q):
        k_cluster_list = []
        nk_cluster_list = []
        for idx_x in range(len_k):
            temp_cluster = alg_cluster.Cluster(set(),float(miu[idx_x][0]),float(miu[idx_x][1]),int(0),float(0))
            k_cluster_list.append(temp_cluster)
            temp_cluster = alg_cluster.Cluster(set(),float(0),float(0),int(0),float(0))
            nk_cluster_list.append(temp_cluster)

        for idx_j in range(len(cluster_list)):
            min_distance = float('inf')
            idx_l = -1
            for idx_f in range(len_k):
                temp_distance = cluster_list[idx_j].distance(k_cluster_list[idx_f])
                if temp_distance <= min_distance:
                    min_distance = temp_distance
                    idx_l = idx_f
            nk_cluster_list[idx_l].merge_clusters(cluster_list[idx_j])

        miu = []
        for idx_f in range(len_k):
            miu.append((nk_cluster_list[idx_f].horiz_center(),nk_cluster_list[idx_f].vert_center()))
    
    return nk_cluster_list

    Compute the len_k-means clustering of a set of clusters
    Note: the function may not mutate cluster_list
    
    Input: List of clusters, integers number of clusters and number of iterations
    Output: List of clusters whose length is num_clusters
    
Compute the len_k-means clustering of a set of clusters
Note: the function may not mutate cluster_list

List of clusters, integers number of clusters and number of iterations
Output: List of clusters whose length is num_clusters
Compute the len_k - means clustering of a set of clusters Note : the function may not mutate cluster_list List of clusters integers number of clusters and number of iterations Output : List of clusters whose length is num_clusters
def kmeans_clustering(cluster_list, len_k, iter_times_q):
    cluster_list_copy = list(cluster_list)
    cluster_list_copy.sort(key = lambda cluster: cluster.total_population(),reverse=True)
    miu = []
    for idx_f in range(len_k):
        miu.append((cluster_list_copy[idx_f].horiz_center(),cluster_list_copy[idx_f].vert_center()))
    for dummy_i in range(iter_times_q):
        k_cluster_list = []
        nk_cluster_list = []
        for idx_x in range(len_k):
            temp_cluster = alg_cluster.Cluster(set(),float(miu[idx_x][0]),float(miu[idx_x][1]),int(0),float(0))
            k_cluster_list.append(temp_cluster)
            temp_cluster = alg_cluster.Cluster(set(),float(0),float(0),int(0),float(0))
            nk_cluster_list.append(temp_cluster)
        for idx_j in range(len(cluster_list)):
            min_distance = float('inf')
            idx_l = -1
            for idx_f in range(len_k):
                temp_distance = cluster_list[idx_j].distance(k_cluster_list[idx_f])
                if temp_distance <= min_distance:
                    min_distance = temp_distance
                    idx_l = idx_f
            nk_cluster_list[idx_l].merge_clusters(cluster_list[idx_j])
        miu = []
        for idx_f in range(len_k):
            miu.append((nk_cluster_list[idx_f].horiz_center(),nk_cluster_list[idx_f].vert_center()))
    return nk_cluster_list
def kmeans_clustering ( cluster_list , len_k , iter_times_q ) : cluster_list_copy = list ( cluster_list ) cluster_list_copy . sort ( key = lambda cluster : cluster . total_population ( ) , reverse = True ) miu = [ ] for idx_f in range ( len_k ) : miu . append ( ( cluster_list_copy [ idx_f ] . horiz_center ( ) , cluster_list_copy [ idx_f ] . vert_center ( ) ) ) for dummy_i in range ( iter_times_q ) : k_cluster_list = [ ] nk_cluster_list = [ ] for idx_x in range ( len_k ) : temp_cluster = alg_cluster . Cluster ( set ( ) , float ( miu [ idx_x ] [ 0 ] ) , float ( miu [ idx_x ] [ 1 ] ) , int ( 0 ) , float ( 0 ) ) k_cluster_list . append ( temp_cluster ) temp_cluster = alg_cluster . Cluster ( set ( ) , float ( 0 ) , float ( 0 ) , int ( 0 ) , float ( 0 ) ) nk_cluster_list . append ( temp_cluster ) for idx_j in range ( len ( cluster_list ) ) : min_distance = float ( 'inf' ) idx_l = - 1 for idx_f in range ( len_k ) : temp_distance = cluster_list [ idx_j ] . distance ( k_cluster_list [ idx_f ] ) if temp_distance <= min_distance : min_distance = temp_distance idx_l = idx_f nk_cluster_list [ idx_l ] . merge_clusters ( cluster_list [ idx_j ] ) miu = [ ] for idx_f in range ( len_k ) : miu . append ( ( nk_cluster_list [ idx_f ] . horiz_center ( ) , nk_cluster_list [ idx_f ] . vert_center ( ) ) ) return nk_cluster_list
Compute the len_k-means clustering of a set of clusters
Note: the function may not mutate cluster_list
Compute the len_k - means clustering of a set of clusters Note : the function may not mutate cluster_list
"""
    Compute the len_k-means clustering of a set of clusters
    Note: the function may not mutate cluster_list
    
    Input: List of clusters, integers number of clusters and number of iterations
    Output: List of clusters whose length is num_clusters
    """ # position initial clusters at the location of clusters with largest populations

cluster_list
len_k
iter_times_q