8 使用“肘部法则”选取k值
def selecte_K(X,iter_num): dist_arry=[] for k in range(1,10): centroids,idx=k_means(data.values,k,iter_num) dist_arry.append((k,metric_square(X,idx,centroids,k))) best_k=sorted(dist_arry,key=lambda item:item[1])[0][0] return dist_arry,best_k
dist_arry,best_k=selecte_K(data.values,20)
[294] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.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.44137276 0.97206476] [3.35444384 1.35441459] [1.95399466 5.02557006]]
9 画张图来可视化选择K
dist_arry
[(1, 1957.654720625167), (2, 913.319271474709), (3, 266.65851965491936), (4, 224.19062208578683), (5, 174.7886069689041), (6, 157.8269861031051), (7, 108.35021878232398), (8, 95.72271918958536), (9, 139.32120386214484)]
y=[item[1] for item in dist_arry] plt.plot(range(1,len(dist_arry)+1),y)
[<matplotlib.lines.Line2D at 0x1d875d6e1c0>]
10 对任意样本来预测其所属的聚类
### 10 对任意样本来预测其所属的聚类 def predict_cluster(x,centroids): lst_dist=[] for i in range(centroids.shape[0]): dist=np.sum(np.power(x-centroids[i],2)) lst_dist.append((i,dist)) # print(lst_dist) return sorted(lst_dist,key=lambda item:item[1])[0][0],lst_dist
centroids.shape[0]
3
x=np.array([[1.0,2.0],[1.5,2.5]]) for item in x: print(predict_cluster(item,centroids))
(2, [(0, 26.33885755045021), (1, 10.064180005080523), (2, 5.146008608810406)]) (2, [(0, 20.80466508259735), (1, 6.584615280794261), (2, 4.586927008121937)])
试试Sklearn
#导入包 from sklearn.cluster import KMeans #实例化一个聚类对象 estimator = KMeans(n_clusters=3) #用训练数据来拟合该模型 estimator.fit(data.values) #对任意一个点进行预测 print(estimator.labels_) estimator.predict(np.array(x))
[0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0] array([1, 1])
#查看每个聚类元素到中心点的距离和 estimator.inertia_
266.6585196549193
estimator.cluster_centers_
array([[1.95399466, 5.02557006], [3.04367119, 1.01541041], [6.03366736, 3.00052511]])
from sklearn.cluster import KMeans # '利用SSE选择k' SSE = [] # 存放每次结果的误差平方和 for k in range(1, 11): estimator = KMeans(n_clusters=k) # 构造聚类器 estimator.fit(data.values) SSE.append(estimator.inertia_) X = range(1, 11) plt.figure(figsize=(8, 6)) plt.xlabel('k') plt.ylabel('SSE') plt.plot(X, SSE, 'o-') plt.show()
d:\Users\DELL\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:1036: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn(
实验:K-means实现无监督聚类
重新利用鸢尾花数据集来实现无监督的聚类,只读取鸢尾花数据集的前两列数据
from sklearn.datasets import load_iris iris=load_iris() data=iris.data[:,:2] data
array([[5.1, 3.5], [4.9, 3. ], [4.7, 3.2], [4.6, 3.1], [5. , 3.6], [5.4, 3.9], [4.6, 3.4], [5. , 3.4], [4.4, 2.9], [4.9, 3.1], [5.4, 3.7], [4.8, 3.4], [4.8, 3. ], [4.3, 3. ], [5.8, 4. ], [5.7, 4.4], [5.4, 3.9], [5.1, 3.5], [5.7, 3.8], [5.1, 3.8], [5.4, 3.4], [5.1, 3.7], [4.6, 3.6], [5.1, 3.3], [4.8, 3.4], [5. , 3. ], [5. , 3.4], [5.2, 3.5], [5.2, 3.4], [4.7, 3.2], [4.8, 3.1], [5.4, 3.4], [5.2, 4.1], [5.5, 4.2], [4.9, 3.1], [5. , 3.2], [5.5, 3.5], [4.9, 3.6], [4.4, 3. ], [5.1, 3.4], [5. , 3.5], [4.5, 2.3], [4.4, 3.2], [5. , 3.5], [5.1, 3.8], [4.8, 3. ], [5.1, 3.8], [4.6, 3.2], [5.3, 3.7], [5. , 3.3], [7. , 3.2], [6.4, 3.2], [6.9, 3.1], [5.5, 2.3], [6.5, 2.8], [5.7, 2.8], [6.3, 3.3], [4.9, 2.4], [6.6, 2.9], [5.2, 2.7], [5. , 2. ], [5.9, 3. ], [6. , 2.2], [6.1, 2.9], [5.6, 2.9], [6.7, 3.1], [5.6, 3. ], [5.8, 2.7], [6.2, 2.2], [5.6, 2.5], [5.9, 3.2], [6.1, 2.8], [6.3, 2.5], [6.1, 2.8], [6.4, 2.9], [6.6, 3. ], [6.8, 2.8], [6.7, 3. ], [6. , 2.9], [5.7, 2.6], [5.5, 2.4], [5.5, 2.4], [5.8, 2.7], [6. , 2.7], [5.4, 3. ], [6. , 3.4], [6.7, 3.1], [6.3, 2.3], [5.6, 3. ], [5.5, 2.5], [5.5, 2.6], [6.1, 3. ], [5.8, 2.6], [5. , 2.3], [5.6, 2.7], [5.7, 3. ], [5.7, 2.9], [6.2, 2.9], [5.1, 2.5], [5.7, 2.8], [6.3, 3.3], [5.8, 2.7], [7.1, 3. ], [6.3, 2.9], [6.5, 3. ], [7.6, 3. ], [4.9, 2.5], [7.3, 2.9], [6.7, 2.5], [7.2, 3.6], [6.5, 3.2], [6.4, 2.7], [6.8, 3. ], [5.7, 2.5], [5.8, 2.8], [6.4, 3.2], [6.5, 3. ], [7.7, 3.8], [7.7, 2.6], [6. , 2.2], [6.9, 3.2], [5.6, 2.8], [7.7, 2.8], [6.3, 2.7], [6.7, 3.3], [7.2, 3.2], [6.2, 2.8], [6.1, 3. ], [6.4, 2.8], [7.2, 3. ], [7.4, 2.8], [7.9, 3.8], [6.4, 2.8], [6.3, 2.8], [6.1, 2.6], [7.7, 3. ], [6.3, 3.4], [6.4, 3.1], [6. , 3. ], [6.9, 3.1], [6.7, 3.1], [6.9, 3.1], [5.8, 2.7], [6.8, 3.2], [6.7, 3.3], [6.7, 3. ], [6.3, 2.5], [6.5, 3. ], [6.2, 3.4], [5.9, 3. ]])
iris.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
plt.scatter(data[:,0],data[:,1],color="blue") plt.show()
#真实的类别长成这样 plt.scatter(data[:,0],data[:,1],c=iris.target) plt.show()
1 定义和调用更新每个样本所属聚类,聚类中心更新,初始化聚类中心的参数
def update_centorids(X,idx,k): centorids=np.zeros((k,X.shape[1])) for i in range(k): index=np.where(idx==i)[0] centorids[i]=np.sum(X[index,:],axis=0)/len(index) return centorids def initialize_centroid(X,k): np.random.seed(30) index_initial=[np.random.randint(1,X.shape[0]) for i in range(k)] centorids=np.zeros((k,X.shape[1])) print(index_initial) for i,j in enumerate(index_initial): centorids[i]=X[j,:] return centorids
2 定义Kmeans算法获得最终的聚类中心和样本所属聚类索引
def k_means(X,k,iter_num): centroids=np.zeros((k,X.shape[1])) #初始化聚类中心 centroids=initialize_centroid(X,k) for i in range(iter_num): #每个样本找到所属聚类 idx=find_closest(X,centroids) print(idx) #更新新的聚类中心 centroids=update_centorids(X,idx,k) print(centroids) return centroids,idx
centroids,idx=k_means(data,3,30)
[38, 46, 141] [1. 0. 0. 0. 1. 1. 0. 1. 0. 0. 1. 1. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 0. 0. 1. 1. 1. 0. 1. 1. 1. 0. 1. 1. 0. 0. 1. 1. 0. 1. 0. 1. 1. 2. 2. 2. 0. 2. 1. 2. 0. 2. 0. 0. 2. 2. 2. 1. 2. 1. 2. 2. 0. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 0. 0. 2. 2. 1. 2. 2. 2. 1. 0. 0. 2. 2. 0. 1. 1. 1. 2. 0. 1. 2. 2. 2. 2. 2. 2. 0. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.] [[4.9137931 2.78965517] [5.29772727 3.44772727] [6.50519481 2.93506494]] [1. 0. 0. 0. 1. 1. 0. 1. 0. 0. 1. 1. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 0. 0. 1. 1. 1. 0. 1. 1. 1. 0. 1. 1. 0. 0. 1. 1. 0. 1. 0. 1. 1. 2. 2. 2. 0. 2. 1. 2. 0. 2. 0. 0. 2. 2. 2. 1. 2. 1. 2. 2. 0. 1. 2. 2. 2. 2. 2. 2. 2. 2. 0. 0. 0. 2. 2. 1. 2. 2. 2. 1. 0. 0. 2. 2. 0. 0. 1. 1. 2. 0. 1. 2. 2. 2. 2. 2. 2. 0. 2. 2. 2. 2. 2. 2. 0. 2. 2. 2. 2. 2. 2. 2. 0. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2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] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 2. 2. 2. 0. 2. 0. 2. 0. 2. 0. 0. 0. 0. 0. 0. 2. 0. 0. 0. 0. 0. 0. 0. 0. 2. 2. 2. 2. 0. 0. 0. 0. 0. 0. 0. 0. 2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 2. 0. 2. 2. 2. 2. 0. 2. 2. 2. 2. 2. 2. 0. 0. 2. 2. 2. 2. 0. 2. 0. 2. 0. 2. 2. 0. 0. 2. 2. 2. 2. 2. 0. 0. 2. 2. 2. 0. 2. 2. 2. 0. 2. 2. 2. 0. 2. 2. 0.] [[5.77358491 2.69245283] [5.006 3.428 ] [6.81276596 3.07446809]]
3 绘制各个聚类的图
#画图 import matplotlib.pyplot as plt cluster1=data[np.where(idx==0)[0],:] cluster2=data[np.where(idx==1)[0],:] cluster3=data[np.where(idx==2)[0],:] fig,axe=plt.subplots(figsize=(8,6)) axe.scatter(cluster1[:,0],cluster1[:,1],s=10,color="red",label="cluster1") axe.scatter(cluster2[:,0],cluster2[:,1],s=10,color="yellow",label="cluster2") axe.scatter(cluster3[:,0],cluster3[:,1],s=10,color="green",label="cluster2") axe.scatter(centroids[:,0],centroids[:,1],s=30,marker="+",c="k") plt.show()
4 定义评价函数–即任意一点所在聚类与聚类中心的距离平方和
定义一个评价函数
def metric_square(X,idx,centroids,k): lst_dist=[] for i in range(k): cluster=X[np.where(idx==i)[0],:] dist=np.sum(np.power(cluster-centroids[i,:],2)) lst_dist.append(dist) return sum(lst_dist)
5 使用“肘部法则”选取k值
def selecte_K(X,iter_num): dist_arry=[] for k in range(1,10): centroids,idx=k_means(data,k,iter_num) dist_arry.append((k,metric_square(X,idx,centroids,k))) best_k=sorted(dist_arry,key=lambda item:item[1])[0][0] return dist_arry,best_k dist_arry,best_k=selecte_K(data,10) y=[item[1] for item in dist_arry] plt.plot(range(1,len(dist_arry)+1),y)
[38] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] …… [1. 0. 0. 0. 1. 1. 0. 1. 0. 0. 1. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 0. 0. 1. 1. 1. 0. 0. 1. 1. 1. 0. 0. 1. 1. 0. 1. 1. 7. 0. 1. 1. 0. 1. 0. 1. 0. 8. 6. 8. 5. 4. 5. 6. 7. 4. 7. 7. 6. 5. 6. 5. 8. 5. 5. 4. 5. 6. 4. 4. 4. 4. 8. 8. 8. 6. 5. 5. 5. 5. 5. 5. 6. 8. 4. 5. 5. 5. 6. 5. 7. 5. 5. 5. 4. 7. 5. 6. 5. 8. 4. 4. 2. 7. 2. 4. 8. 8. 4. 8. 5. 5. 6. 4. 3. 2. 5. 8. 5. 2. 4. 8. 8. 4. 6. 4. 2. 2. 3. 4. 4. 4. 2. 6. 6. 6. 8. 8. 8. 5. 8. 8. 8. 4. 4. 6. 6.] [[4.71428571 3.16190476] [5.24285714 3.66785714] [7.51428571 2.87142857] [7.8 3.8 ] [6.34545455 2.73636364] [5.68518519 2.66666667] [6.14375 3.14375 ] [4.94285714 2.38571429] [6.825 3.13 ]]
6 对任意样本来预测其所属的聚类
def predict_cluster(x,centroids): lst_dist=[] for i in range(centroids.shape[0]): dist=np.sum(np.power(x-centroids[i],2)) lst_dist.append((i,dist)) return sorted(lst_dist,key=lambda item:item[1])[0][0],lst_dist
for item in np.array([[1.0,2.0],[2.4,1.6]]): print(predict_cluster(item,centroids))
(1, [(0, 23.266603773584908), (1, 18.08722), (2, 34.94272974196466)]) (1, [(0, 12.574528301886792), (1, 10.132819999999999), (2, 21.646559529198715)])
7 试试SKLERAN
#导入包 from sklearn.cluster import KMeans estimator = KMeans(n_clusters=3) estimator.fit(data) print(estimator.labels_) print(estimator.predict(np.array([[1.0,2.0],[2.4,1.6]]))) estimator.inertia_ estimator.cluster_centers_
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 0 2 0 2 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 2 2 2 0 2 2 2 2 2 2 0 0 2 2 2 2 0 2 0 2 0 2 2 0 0 2 2 2 2 2 0 0 2 2 2 0 2 2 2 0 2 2 2 0 2 2 0] [1 1] array([[5.77358491, 2.69245283], [5.006 , 3.428 ], [6.81276596, 3.07446809]])