机器学习--kmeans的基本实现

# _*_ coding:utf-8 _*_
import numpy as np
def loadDataset():
    a = np.array([(3, 4), (3, 6), (7, 3), (4, 7), (3, 8), (8, 5), (4, 5), (4, 1), (7, 4), (5, 5)]).astype(np.float)
    return a

def initCenter(dataset,k):
    index = np.random.choice(len(dataset),k,replace=False)
    # print(dataset[index])
    return dataset[index]

def cal_dis(a,b):
    return np.sum((a-b)**2)**0.5

def kmeans(dataset,k):
    centers = initCenter(dataset,k)
    m = dataset.shape[0]
    clusters = np.full(m,np.nan)
    flag = True
    while(flag):
        flag=False
        for i in range(len(dataset)):
            mini_dst,index = 9999,-1
            for j in range(len(centers)):
                dst = cal_dis(dataset[i],centers[j])
                #判断最小距离是否发生变化
                if dst

 

你可能感兴趣的:(Machine,Learning)