def run_kmeans(transformed_pca_matrix, n_clusters, random_state=None):
if random_state is None:
random_state=cr_constants.RANDOM_STATE
kmeans = sk_cluster.KMeans(n_clusters=n_clusters, random_state=random_state)
clusters = kmeans.fit_predict(transformed_pca_matrix) + 1
cluster_score = compute_db_index(transformed_pca_matrix, kmeans)
clusters = cr_clustering.relabel_by_size(clusters)
clustering_key = cr_clustering.format_clustering_key(cr_clustering.CLUSTER_TYPE_KMEANS, n_clusters)
return cr_clustering.create_clustering(clusters=clusters,
num_clusters=n_clusters,
cluster_score=cluster_score,
clustering_type=cr_clustering.CLUSTER_TYPE_KMEANS,
global_sort_key=n_clusters,
description=cr_clustering.humanify_clustering_key(clustering_key))
评论列表
文章目录