def k_means_reduced(all_projected_matrix, distance, d, n, k, weighted_distance): # Initialization of clusters - according to K parameter allocation_table = initial_allocation(k) print(allocation_table) results = ClassFile.Results() all_projected_clusters = create_projected_cluster(all_projected_matrix, allocation_table) # Iterations for i in range(0, n): for c in range(1, k + 1): cluster_results = ClassFile.ClusterResults() for s in range(1, d + 1): if distance == "euclidean": cur_dist = dist.euclidean_distance(all_projected_matrix, all_projected_clusters, s, c, weighted_distance) if distance == "correlation-pca": cur_dist = dist.correlation_distance(all_projected_matrix, all_projected_clusters, s, c, weighted_distance) if distance == "dwt": cur_dist = dist.dwt_distance(all_projected_matrix, all_projected_clusters, s, c, weighted_distance) cluster_results.add_commodity(s) cluster_results.add_distance(cur_dist) # this object contains all the desired distances results.add_cluster_result(cluster_results) # Update assignments allocation_table = update_allocation(results, allocation_table) # change prev alloc table based on curr results print(allocation_table) results = ClassFile.Results() # reset results all_projected_clusters = create_projected_cluster(all_projected_matrix, allocation_table) return allocation_table, all_projected_clusters;
def cost_of_clustering(all_projected_clusters, all_projected_matrix, allocation_table, weighted_distance): cost = np.zeros(4) for i in range(0, 4): for j in range(0, 4): if allocation_table[j][i] != 0: cost[i] += dist.dwt_distance(all_projected_matrix, all_projected_clusters, allocation_table[j][i]-1, i, weighted_distance) return cost;