from sklearn.metrics import silhouette_score def silhouette_score(X, labels, metric='euclidean', sample_size=None, random_state=None, **kwds): ''' X:表示要聚类的样本数据,一般形如(samples,features)的格式 labels:即聚类之后得到的label标签,形如(samples,)的格式 metric:默认是欧氏距离 '''
# 生成随机样例,并可视化 import matplotlib.pyplot as plt import seaborn as sns sns.set() import numpy as np from sklearn.datasets import make_blobs X, y_true = make_blobs(n_samples=300, centers=3, cluster_std=0.60, random_state=0) plt.scatter(X[:, 0], X[:, 1], s=50) plt.show()
# 聚成三类,并可视化 from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=3) kmeans.fit(X) y_kmeans = kmeans.predict(X) plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis') centers = kmeans.cluster_centers_ plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5); plt.show()
from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=6) kmeans.fit(X) y_kmeans = kmeans.predict(X) plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis') centers = kmeans.cluster_centers_ plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5); plt.show()
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题
什么是聚类算法
聚类的一般步骤
常用聚类算法有哪些、对应的度量函数分别是
聚类跟分类的本质区别
聚类好坏的评价指标
代码示例
生成随机样例,并可视化
kmeans聚类成三类
同样的数据,聚成 6 类
延伸考点