Python實(shí)現(xiàn)的KMeans聚類算法實(shí)例分析
本文實(shí)例講述了Python實(shí)現(xiàn)的KMeans聚類算法。分享給大家供大家參考,具體如下:
菜鳥一枚,編程初學(xué)者,最近想使用Python3實(shí)現(xiàn)幾個(gè)簡單的機(jī)器學(xué)習(xí)分析方法,記錄一下自己的學(xué)習(xí)過程。
關(guān)于KMeans算法本身就不做介紹了,下面記錄一下自己遇到的問題。
一 、關(guān)于初始聚類中心的選取
初始聚類中心的選擇一般有:
(1)隨機(jī)選取
(2)隨機(jī)選取樣本中一個(gè)點(diǎn)作為中心點(diǎn),在通過這個(gè)點(diǎn)選取距離其較大的點(diǎn)作為第二個(gè)中心點(diǎn),以此類推。
(3)使用層次聚類等算法更新出初始聚類中心
我一開始是使用numpy隨機(jī)產(chǎn)生k個(gè)聚類中心
Center = np.random.randn(k,n)
但是發(fā)現(xiàn)聚類的時(shí)候迭代幾次以后聚類中心會(huì)出現(xiàn)nan,有點(diǎn)搞不清楚怎么回事
所以我分別嘗試了:
(1)選擇數(shù)據(jù)集的前K個(gè)樣本做初始中心點(diǎn)
(2)選擇隨機(jī)K個(gè)樣本點(diǎn)作為初始聚類中心
發(fā)現(xiàn)兩者都可以完成聚類,我是用的是iris.csv數(shù)據(jù)集,在選擇前K個(gè)樣本點(diǎn)做數(shù)據(jù)集時(shí),迭代次數(shù)是固定的,選擇隨機(jī)K個(gè)點(diǎn)時(shí),迭代次數(shù)和隨機(jī)種子的選取有關(guān),而且聚類效果也不同,有的隨機(jī)種子聚類快且好,有的慢且差。
def InitCenter(k,m,x_train): #Center = np.random.randn(k,n) #Center = np.array(x_train.iloc[0:k,:]) #取數(shù)據(jù)集中前k個(gè)點(diǎn)作為初始中心 Center = np.zeros([k,n]) #從樣本中隨機(jī)取k個(gè)點(diǎn)做初始聚類中心 np.random.seed(5) #設(shè)置隨機(jī)數(shù)種子 for i in range(k): x = np.random.randint(m) Center[i] = np.array(x_train.iloc[x]) return Center
二 、關(guān)于類間距離的選取
為了簡單,我直接采用了歐氏距離,目前還沒有嘗試其他的距離算法。
def GetDistense(x_train, k, m, Center): Distence=[] for j in range(k): for i in range(m): x = np.array(x_train.iloc[i, :]) a = x.T - Center[j] Dist = np.sqrt(np.sum(np.square(a))) # dist = np.linalg.norm(x.T - Center) Distence.append(Dist) Dis_array = np.array(Distence).reshape(k,m) return Dis_array
三 、關(guān)于終止聚類條件的選取
關(guān)于聚類的終止條件有很多選擇方法:
(1)迭代一定次數(shù)
(2)聚類中心的更新小于某個(gè)給定的閾值
(3)類中的樣本不再變化
我用的是前兩種方法,第一種很簡單,但是聚類效果不好控制,針對不同數(shù)據(jù)集,穩(wěn)健性也不夠。第二種比較合適,穩(wěn)健性也強(qiáng)。第三種方法我還沒有嘗試,以后可以試著用一下,可能聚類精度會(huì)更高一點(diǎn)。
def KMcluster(x_train,k,n,m,threshold): global axis_x, axis_y center = InitCenter(k,m,x_train) initcenter = center centerChanged = True t=0 while centerChanged: Dis_array = GetDistense(x_train, k, m, center) center ,axis_x,axis_y,axis_z= GetNewCenter(x_train,k,n,Dis_array) err = np.linalg.norm(initcenter[-k:] - center) print(err) t+=1 plt.figure(1) p=plt.subplot(3, 3, t) p1,p2,p3 = plt.scatter(axis_x[0], axis_y[0], c='r'),plt.scatter(axis_x[1], axis_y[1], c='g'),plt.scatter(axis_x[2], axis_y[2], c='b') plt.legend(handles=[p1, p2, p3], labels=['0', '1', '2'], loc='best') p.set_title('Iteration'+ str(t)) if err < threshold: centerChanged = False else: initcenter = np.concatenate((initcenter, center), axis=0) plt.show() return center, axis_x, axis_y,axis_z, initcenter
err是本次聚類中心點(diǎn)和上次聚類中心點(diǎn)之間的歐氏距離。
threshold是人為設(shè)定的終止聚類的閾值,我個(gè)人一般設(shè)置為0.1或者0.01。
為了將每次迭代產(chǎn)生的類別顯示出來我修改了上述代碼,使用matplotlib展示每次迭代的散點(diǎn)圖。
下面附上我測試數(shù)據(jù)時(shí)的圖,子圖設(shè)置的個(gè)數(shù)要根據(jù)迭代次數(shù)來定。
我測試了幾個(gè)數(shù)據(jù)集,聚類的精度還是可以的。
使用iris數(shù)據(jù)集分析的結(jié)果為:
err of Iteration 1 is 3.11443180281
err of Iteration 2 is 1.27568813621
err of Iteration 3 is 0.198909381512
err of Iteration 4 is 0.0
Final cluster center is [[ 6.85 3.07368421 5.74210526 2.07105263]
[ 5.9016129 2.7483871 4.39354839 1.43387097]
[ 5.006 3.428 1.462 0.246 ]]
最后附上全部代碼,錯(cuò)誤之處還請多多批評,謝謝。
#encoding:utf-8 """ Author: njulpy Version: 1.0 Data: 2018/04/11 Project: Using Python to Implement KMeans Clustering Algorithm """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn.cluster import KMeans def InitCenter(k,m,x_train): #Center = np.random.randn(k,n) #Center = np.array(x_train.iloc[0:k,:]) #取數(shù)據(jù)集中前k個(gè)點(diǎn)作為初始中心 Center = np.zeros([k,n]) #從樣本中隨機(jī)取k個(gè)點(diǎn)做初始聚類中心 np.random.seed(15) #設(shè)置隨機(jī)數(shù)種子 for i in range(k): x = np.random.randint(m) Center[i] = np.array(x_train.iloc[x]) return Center def GetDistense(x_train, k, m, Center): Distence=[] for j in range(k): for i in range(m): x = np.array(x_train.iloc[i, :]) a = x.T - Center[j] Dist = np.sqrt(np.sum(np.square(a))) # dist = np.linalg.norm(x.T - Center) Distence.append(Dist) Dis_array = np.array(Distence).reshape(k,m) return Dis_array def GetNewCenter(x_train,k,n, Dis_array): cen = [] axisx ,axisy,axisz= [],[],[] cls = np.argmin(Dis_array, axis=0) for i in range(k): train_i=x_train.loc[cls == i] xx,yy,zz = list(train_i.iloc[:,1]),list(train_i.iloc[:,2]),list(train_i.iloc[:,3]) axisx.append(xx) axisy.append(yy) axisz.append(zz) meanC = np.mean(train_i,axis=0) cen.append(meanC) newcent = np.array(cen).reshape(k,n) NewCent=np.nan_to_num(newcent) return NewCent,axisx,axisy,axisz def KMcluster(x_train,k,n,m,threshold): global axis_x, axis_y center = InitCenter(k,m,x_train) initcenter = center centerChanged = True t=0 while centerChanged: Dis_array = GetDistense(x_train, k, m, center) center ,axis_x,axis_y,axis_z= GetNewCenter(x_train,k,n,Dis_array) err = np.linalg.norm(initcenter[-k:] - center) t+=1 print('err of Iteration '+str(t),'is',err) plt.figure(1) p=plt.subplot(2, 3, t) p1,p2,p3 = plt.scatter(axis_x[0], axis_y[0], c='r'),plt.scatter(axis_x[1], axis_y[1], c='g'),plt.scatter(axis_x[2], axis_y[2], c='b') plt.legend(handles=[p1, p2, p3], labels=['0', '1', '2'], loc='best') p.set_title('Iteration'+ str(t)) if err < threshold: centerChanged = False else: initcenter = np.concatenate((initcenter, center), axis=0) plt.show() return center, axis_x, axis_y,axis_z, initcenter if __name__=="__main__": #x=pd.read_csv("8.Advertising.csv") # 兩組測試數(shù)據(jù) #x=pd.read_table("14.bipartition.txt") x=pd.read_csv("iris.csv") x_train=x.iloc[:,1:5] m,n = np.shape(x_train) k = 3 threshold = 0.1 km,ax,ay,az,ddd = KMcluster(x_train, k, n, m, threshold) print('Final cluster center is ', km) #2-Dplot plt.figure(2) plt.scatter(km[0,1],km[0,2],c = 'r',s = 550,marker='x') plt.scatter(km[1,1],km[1,2],c = 'g',s = 550,marker='x') plt.scatter(km[2,1],km[2,2],c = 'b',s = 550,marker='x') p1, p2, p3 = plt.scatter(axis_x[0], axis_y[0], c='r'), plt.scatter(axis_x[1], axis_y[1], c='g'), plt.scatter(axis_x[2], axis_y[2], c='b') plt.legend(handles=[p1, p2, p3], labels=['0', '1', '2'], loc='best') plt.title('2-D scatter') plt.show() #3-Dplot plt.figure(3) TreeD = plt.subplot(111, projection='3d') TreeD.scatter(ax[0],ay[0],az[0],c='r') TreeD.scatter(ax[1],ay[1],az[1],c='g') TreeD.scatter(ax[2],ay[2],az[2],c='b') TreeD.set_zlabel('Z') # 坐標(biāo)軸 TreeD.set_ylabel('Y') TreeD.set_xlabel('X') TreeD.set_title('3-D scatter') plt.show()
附:上述示例中的iris.csv文件點(diǎn)擊此處本站下載。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
pytorch中LN(LayerNorm)及Relu和其變相的輸出操作
這篇文章主要介紹了pytorch中LN(LayerNorm)及Relu和其變相的輸出操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python實(shí)現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法
這篇文章主要介紹了Python實(shí)現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法,可實(shí)現(xiàn)Python模擬登陸并抓取新鮮事的功能,需要的朋友可以參考下2015-05-05python使用opencv實(shí)現(xiàn)馬賽克效果示例
這篇文章主要介紹了python使用opencv實(shí)現(xiàn)馬賽克效果,結(jié)合實(shí)例形式分析了Python使用cv2模塊操作圖片實(shí)現(xiàn)馬賽克效果的相關(guān)技巧,需要的朋友可以參考下2019-09-09關(guān)于windos10環(huán)境下編譯python3版pjsua庫的問題
pjsua默認(rèn)綁定的python版本是python 2.4,使用起來有諸多限制,希望可以使用python3調(diào)用pjsua的庫實(shí)現(xiàn)軟電話的基礎(chǔ)功能。這篇文章主要介紹了windos10環(huán)境下編譯python3版pjsua庫,需要的朋友可以參考下2021-10-10Python日期時(shí)間模塊datetime詳解與Python 日期時(shí)間的比較,計(jì)算實(shí)例代碼
python中的datetime模塊提供了操作日期和時(shí)間功能,本文為大家講解了datetime模塊的使用方法及與其相關(guān)的日期比較,計(jì)算實(shí)例2018-09-09Anaconda環(huán)境克隆、遷移的詳細(xì)步驟
最近需要在多臺(tái)計(jì)算機(jī)上工作,每次重新部署環(huán)境比較麻煩,所以學(xué)習(xí)一下anaconda環(huán)境遷移的方法,下面這篇文章主要給大家介紹了關(guān)于Anaconda環(huán)境克隆、遷移的詳細(xì)步驟,需要的朋友可以參考下2022-08-08