import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.metrics import silhouette_score
from sklearn.cluster import KMeans
# 生成一些示例数据,你可以替换成你的数据
iris = datasets.load_iris()
x = iris.data
print( type(x))
print(x.shape)
from sklearn.manifold import TSNE
tsne=TSNE()
X=tsne.fit_transform(x)
print(X.shape)
plt.scatter(X[:,0], X[:,1])
# 计算不同聚类数下的簇内平方和
inertia = []
silhouette_scores = []
k_range = range(2, 11)
for k in k_range:
kmeans = KMeans(n_clusters=k, random_state=10).fit(X)
inertia.append(kmeans.inertia_)
silhouette_scores.append(silhouette_score(X, kmeans.labels_))
# 画出肘部法则图
plt.plot(k_range, silhouette_scores, marker='o')
plt.xlabel('Number of clusters')
plt.ylabel('Silhouette Score')
plt.title('Silhouette Score For Each k')
plt.tight_layout()
# 官方给出的分类。[0 1 2]
print( np.unique( datasets.load_iris().target))
plt.show()
<class 'numpy.ndarray'>
(150, 4)
(150, 2)
[0 1 2]
- 输出