聚类 西瓜

import numpy as np
import time


class KMeans:
    def __init__(self):
        return

    def kmeans(self, data, k):
        # input: data and k
        # output: clusters and nearest
        np.random.seed()
        n, m = len(data), len(data[0])  # n * m
        # initial clusters and nearest
        clusters = np.empty((k, m))
        nearest = np.empty((n))  # n
        # idxs = np.random.choice(n, k)
        idxs = [5, 11, 23]
        clusters = data[idxs]  # k * m
        # expand data and cluster to avoid for-loop
        data_expd = np.repeat(np.expand_dims(data, axis=1), k, axis=1)  # n * k * m

        while True:
            clusters_expd = np.repeat(np.expand_dims(clusters, axis=0), n, axis=0)  # n * k * m
            # cal dist between each sample and each cluster
            distances = np.sqrt(np.sum(np.power(data_expd - clusters_expd, 2), axis=2))  # n * k
            # cal cur nearest and cmp the last and the cur
            new_nearest = np.argmin(distances, axis=1)
            if (new_nearest == nearest).all():
                break
            else:
                # if need update, updata the clusters and the nearest
                nearest = new_nearest
                for i in range(k):
                    clusters[i] = np.mean(data[nearest == i], axis=0)
        return clusters, nearest


data = [[0.697, 0.460], [0.774, 0.376], [0.634, 0.264], [0.608, 0.318], [0.556, 0.215],
        [0.403, 0.237], [0.481, 0.149], [0.437, 0.211], [0.666, 0.091], [0.243, 0.267],
        [0.245, 0.057], [0.343, 0.099], [0.639, 0.161], [0.657, 0.198], [0.360, 0.370],
        [0.593, 0.042], [0.719, 0.103], [0.359, 0.188], [0.339, 0.241], [0.282, 0.257],
        [0.748, 0.232], [0.714, 0.346], [0.483, 0.312], [0.478, 0.437], [0.525, 0.369],
        [0.751, 0.489], [0.532, 0.472], [0.473, 0.376], [0.725, 0.445], [0.446, 0.459]]
data = np.array(data)


print("当K=3时:")
k = 3
KM = KMeans()
print("聚类结果为:")
print(KM.kmeans(data, 3))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值