import os
os.environ['OMP_NUM_THREADS'] = '1'
# 导包
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import calinski_harabasz_score
# 构建数据
x,y=make_blobs(n_samples=1000,n_features=2,centers=[[-1,-1],[0,0],[1,1],[2,2]],cluster_std=[0.4,0.2,0.2,0.3],random_state=22)
sse=[]
# 计算不同K值下的SSE,来获取K值
for k in range(1,51):
km=KMeans(n_clusters=k,max_iter=100,random_state=22)
km.fit(x)
sse.append(km.inertia_)
plt.plot(range(1,51),sse)
plt.grid()
plt.show()
3.2 SC聚类评估指标
python复制代码
# 计算SC系数
import os
os.environ['OMP_NUM_THREADS'] = '1'
# 导包
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import calinski_harabasz_score,silhouette_score
# 构建数据
x,y=make_blobs(n_samples=1000,n_features=2,centers=[[-1,-1],[0,0],[1,1],[2,2]],cluster_std=[0.4,0.2,0.2,0.3],random_state=22)
sc=[]
# 计算不同K值下的SSE,来获取K值
for k in range(2,51):
km=KMeans(n_clusters=k,max_iter=100,random_state=22)
y_pred=km.fit_predict(x)
sc_=silhouette_score(x,y_pred)
sc.append(sc_)
plt.plot(range(2,51),sc)
plt.grid()
plt.show()