Python k-均值聚類演算法二維例項

qq_24311495發表於2018-02-10

k-均值聚類演算法二維例項,不多解釋,解釋就是掩飾,複製貼上即可執行。

import time
import numpy as np
import random
import matplotlib.pyplot as plt
import operator

def func01():    #生成二維隨機點

    #random.seed(1)
    kjz1=[[random.randint(50,100),random.randint(50,100)] for j in range(0,200)]
    kjz1.extend([[random.randint(150,200),random.randint(150,200)] for j in range(0,200)])
    kjz1.extend([[random.randint(60,110),random.randint(150,200)] for j in range(0,200)])
    kjz1.extend([[random.randint(130,180),random.randint(50,100)] for j in range(0,200)])
    kjz1.extend([[random.randint(150,200),random.randint(250,300)] for j in range(0,200)])
    kjz1.extend([[random.randint(60,110),random.randint(250,300)] for j in range(0,200)])
    return kjz1

def func02(kjz2w): #繪圖

    if kjz2w!=[]:
        colors=['b','g','r','c','m','y','k'];s=0;
        for j in kjz2w:
            for i in j:
                plt.plot(i[0], i[1], color=colors[s%len(colors)], marker='.')
            s=s+1;
        plt.ion();plt.show();plt.pause(5);plt.close();

def func03(kjz1,k):    #計算初始均值,並返回初始分組

    minxy=np.min(kjz1,axis=0).tolist();maxxy=np.max(kjz1,axis=0).tolist();
    xjg=(maxxy[0]-minxy[0])/k;yjg=(maxxy[1]-minxy[1])/k;
    meanxy=[];meanxy.append([minxy[0],minxy[1]]);meanxy.append([maxxy[0],maxxy[1]]);
    for j in range(1,k-1):
        meanxy.append([minxy[0]+xjg*j,minxy[1]+yjg*j])
    kjz2wxy2=[[] for j in range(0,len(meanxy))];
    for j in kjz1:
        s=0;lslb=[];
        for k in meanxy:
            lslb.append([s,(j[0]-k[0])**2+(j[1]-k[1])**2]);s=s+1; #一個座標一組
        lslb.sort(reverse=False,key=operator.itemgetter(1))  #正序
        kjz2wxy2[lslb[0][0]].append(j)
    return kjz2wxy2

def func05(lb2): #剔除空列表

    j=0;
    while(True):
        if len(lb2[j])<=0:
            lb2.pop(j)
        else:
            j=j+1;
        if j>=len(lb2):
            break
    return lb2

def func06(kjz2wxy): #求組合中心(均值)

    meanxy=[];
    for j in kjz2wxy:
        meanxy.append(np.mean(j,axis=0).tolist())
    kjz2wxy2=[[] for j in range(0,len(meanxy))];
    for j in kjz2wxy:
        for i in j: #點
            s=0;lslb=[];
            for k in meanxy:
                lslb.append([s,(i[0]-k[0])**2+(i[1]-k[1])**2]);s=s+1;
            lslb.sort(reverse=False,key=operator.itemgetter(1))  #正序
            kjz2wxy2[lslb[0][0]].append(i)
    kjz2wxy2=func05(kjz2wxy2)
    return kjz2wxy2,meanxy

def func07(kjz2w,fz):

    kjz2wxy=func03(kjz2w,fz) #座標列表,分組,0-按照x軸均分
    j=0;
    while(True):
        kjz2wxy,meanxy=func06(kjz2wxy)
        if j>0 and meanxy==meanxy2:
            break
        meanxy2=meanxy.copy();
        j=j+1;
    print('迭代%d次' % (j))
    func02(kjz2wxy) #繪圖

if __name__=='__main__':

    start=time.time();
    for j in range(0,10):
        kjz2w=func01()
        func07(kjz2w,6) #分6組

    print('Time used:',int((time.time()-start)/60*10)/10,'分鐘')

這裡寫圖片描述
上圖是初始均值選取比較好的時候的情況,還有些情況是下面這樣的。
這裡寫圖片描述
今天還寫了一種畫蛇添足的演算法,從一維演算法過度來的,就像下面這樣的。

import time
import numpy as np
import random
import matplotlib.pyplot as plt
import operator


def func01():    #生成二維隨機點

    random.seed(1)
    kjz1=[[random.randint(50,100),random.randint(50,100)] for j in range(0,200)]
    kjz1.extend([[random.randint(150,200),random.randint(150,200)] for j in range(0,200)])
    kjz1.extend([[random.randint(60,110),random.randint(150,200)] for j in range(0,200)])
    kjz1.extend([[random.randint(130,180),random.randint(50,100)] for j in range(0,200)])
    kjz1.extend([[random.randint(150,200),random.randint(250,300)] for j in range(0,200)])
    kjz1.extend([[random.randint(60,110),random.randint(250,300)] for j in range(0,200)])
    #繪圖
    plt.xlabel('x-axis')
    plt.ylabel('y-axis')  
    for j in kjz1:
        plt.plot(j[0], j[1], color='b', marker='.', label='y1 data')
    plt.ion();plt.show();plt.pause(1);plt.close();

    return kjz1

def func02(kjz1,k,axis):    #k個均值分k份

    kjz1.sort(reverse=False,key=operator.itemgetter(1-axis))  #正序
    kjz1.sort(reverse=False,key=operator.itemgetter(axis))  #正序
    wb2=kjz1.copy();
    #初始均勻分組
    xlb=[];a=round(len(wb2)/k);b=len(wb2)%k;
    for j in range(1,k+1):
        xlb.append(j*a)
        if j==k:
            xlb[j-1]=xlb[j-1]+b;
    j=0;wb1=[];
    for j in range(0,k):
        wb1.append([])
    i=0;j=0;
    while(i<=len(wb2)-1):
        wb1[j].append(wb2[i]);
        if i>=xlb[j]-1:
            j=j+1;
        i=i+1;
    kj1=means(wb1,axis);#初始分組均值

    bj=1;
    while(True):
        wb2=kjz1.copy();
        if bj!=1:
            kj1=kj2.copy();
        wb3=[];
        for j in range(0,k-1):
            wb3.append([])
        for j in range(0,k-1):
            i=0;
            while(True):
                if wb2[i][axis]<=kj1[j]:
                    wb3[j].append(wb2.pop(i));
                else:
                    i=i+1;
                if i>=len(wb2):
                    break
        wb3.append(wb2)
        for j in wb3:
            if len(j)<=0:
                print('分組出現空組,返回[]');return []
        kj2=means(wb3,axis);#過程均值
        if bj==2:
            if kj1==kj2:
                break
        bj=2;
    return wb3

def means(lb1,axis):    #計算均值

    mean1=[];mean2=[];std1=[];
    for j in lb1:
        mean1.append(np.mean(j,axis=0).tolist())
    for j in range(1,len(mean1)):
        mean2.append(np.mean([mean1[j-1][axis],mean1[j][axis]])) #分組均值使用各組的均值
    return mean2

def func03(kjz2w): #繪圖

    if kjz2w!=[]:
        colors=['b','g','r','c','m','y','k'];s=0;
        for j in kjz2w:
            for i in j:
                plt.plot(i[0], i[1], color=colors[s%len(colors)], marker='.', label='y1 data')
            s=s+1;
        plt.ion();plt.show();plt.pause(3);plt.close();

def func04(kjz2wx,kjz2wy): #x,y 組整合

    kjz2wxy=[[] for j in range(0,len(kjz2wx)*len(kjz2wy))];k=0;
    for j in kjz2wx:
        for i in kjz2wy:
            for a in j:
                if a in i:
                    kjz2wxy[k].append(a);
            k=k+1;
    kjz2wxy=func05(kjz2wxy)
    return kjz2wxy


def func05(lb2): #組合迭代

    j=0;
    while(True):
        if len(lb2[j])<=0:
            lb2.pop(j)
        else:
            j=j+1;
        if j>=len(lb2):
            break
    return lb2


def func06(kjz2wxy): #組合迭代

    #求組合中心(均值)
    meanxy=[];
    for j in kjz2wxy:
        meanxy.append(np.mean(j,axis=0).tolist())
    kjz2wxy2=[[] for j in range(0,len(meanxy))];
    for j in kjz2wxy:
        for i in j: #點
            s=0;lslb=[];
            for k in meanxy:
                lslb.append([s,(i[0]-k[0])**2+(i[1]-k[1])**2]);s=s+1;
            lslb.sort(reverse=False,key=operator.itemgetter(1))  #正序
            kjz2wxy2[lslb[0][0]].append(i)
    kjz2wxy2=func05(kjz2wxy2)
    return kjz2wxy2,meanxy

def func07(kjz2w):

    kjz2wx=func02(kjz2w,2,0) #列表,分組數,x軸0,y軸1
    func03(kjz2wx) #繪圖

    kjz2wy=func02(kjz2w,3,1)
    func03(kjz2wy) #繪圖

    kjz2wxy=func04(kjz2wx,kjz2wy)
    func03(kjz2wxy) #繪圖

    #kjz2wxy=func06(kjz2wxy)
    j=0;
    while(True):
        kjz2wxy,meanxy=func06(kjz2wxy)
        if j>0 and meanxy==meanxy2:
            break
        meanxy2=meanxy.copy();
        j=j+1;
    print('迭代%d次' % (j))
    func03(kjz2wxy) #繪圖

if __name__=='__main__':

    start=time.time();
    kjz2w=func01()

    func07(kjz2w)

    print('Time used:',int((time.time()-start)/60*10)/10,'分鐘')

相關文章