字节跳动面试题.实现kmeans「numpy」

import numpy as np

'''欧式距离'''
def ecludDist(x, y):
    return np.sqrt(sum(np.square(np.array(x) - np.array(y))))
'''计算簇的均值点'''
def clusterMean(dataset):
    return sum(np.array(dataset)) / len(dataset)

'''生成随机均值点'''
def randCenter(dataset, k):
    temp = []
    while len(temp) < k:
        index = np.random.randint(0, len(dataset)-1)
        if  index not in temp:
            temp.append(index)
    return np.array([dataset[i] for i in temp])

def kMeans(dataset, dist, center, k):
    #all_kinds用于存放中间计算结果
    all_kinds = []
    for _ in range(k):
        temp = []
        all_kinds.append(temp)
    #计算每个点到各均值点的距离
    for i in dataset:
        temp = []
        for j in center:
            temp.append(dist(i, j))
        all_kinds[temp.index(min(temp))].append(i)
    #更新均值点
    center_ = np.array([clusterMean(i) for i in all_kinds])
    #打印中间结果
    for i in range(k):
        print('第' + str(i) + '组:', all_kinds[i], end='\n')
    print("------------------------------------------------------------------------------------")

    if (center_ == center).all():
        for i in range(k):
            print('第'+str(i)+'组均值点:', center_[i], end='\n')
    else:
        #递归调用kMeans函数
        center = center_
        kMeans(dataset, dist, center, k)

def main(k):
    '''生成随机点'''
    x = [np.random.randint(0, 50) for _ in range(50)]
    y = [np.random.randint(0, 50) for _ in range(50)]
    points = [[i,j] for i, j in zip(x, y)]
    initial_center = randCenter(dataset=points, k=k)
    kMeans(dataset=points, dist=ecludDist, center=initial_center, k=k)

if __name__ == '__main__':
    main(3)

注:

参考链接:https://blog.csdn.net/iphilo/article/details/80735944

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值