K-Center-Greedy算法讲解和python实现

前言

K-Center-Greedy 算法在主动学习和数据采样、最大覆盖等方面有广泛的应用。

最主要的思想是,可以用k个中心点来代表目前的数据分布。

算法流程

  1. 初始化:随机选择一个sample作为第一个中心。

  2. 计算数据集中每个sample到中心点的距离,并找出每个sample到中心点最小的距离,即计算数据集中每个点到其最近中心点的距离,我们称之为每个点的最近中心点距离。

  3. 选择目前所有样本点中的最近中心点距离最大的那个作为新的中心点。(即距离现有中心点最远的那个样本作为新的中心点)

  4. 重复2-3, 直到选择出k个中心点

其实可以简单一句话总结(上面的表述有点子绕 可能),这个算法的中心思想:就是找出目前样本中和目前所有中心点最远的那个点,作为新的中心点。

简单实现

import numpy as np
from scipy.spatial import distance
import matplotlib.pyplot as plt


def k_center_greedy(X, k):
    # 随机选择第一个中心点
    centers = [X[np.random.choice(range(X.shape[0]))]]

    while len(centers) < k:
        # 计算数据集中每个点到其最近中心点的最小距离
        dist_to_closest_center = np.array([min([distance.euclidean(x, c) for c in centers]) for x in X])

        # 选择距离现有中心点最远的点
        new_center = X[np.argmax(dist_to_closest_center)]
        centers.append(new_center)

    return np.array(centers)


def plot_k_center_greedy(points, centers):
    plt.scatter(points[:, 0], points[:, 1], c='blue', label='Data Points')
    plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='x', s=100, label='Center Points')
    plt.title('K-Center-Greedy Result')
    plt.xlabel('Dimension 1')
    plt.ylabel('Dimension 2')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    points = np.random.rand(200, 2) * 100
    k = 10
    centers = k_center_greedy(points, k)
    plot_k_center_greedy(points, centers)
677c8d34a713110b38aa26ae370be417.png

更节省空间,更结构化的实现

import numpy as np
from sklearn.metrics.pairwise import cosine_distances, euclidean_distances
import matplotlib.pyplot as plt


def cal_distance(x, y, mod="euc"):
    if mod == "cos":
        distance = cosine_distances(X=x.reshape(1, -1),
                                    Y=y.reshape(1, -1))  # 与簇的质心的最小距离
        distance = distance[0][0]
        return distance
    elif mod == "euc":
        distance = euclidean_distances(X=x.reshape(1, -1),
                                       Y=y.reshape(1, -1))
        distance = distance[0][0]
        return distance


class KCenterGreedy:
    def __init__(self, k):
        self.k = k
        self.centers = []
        self.select_index_list = []

    def fit(self, data):
        self.centers = []
        init_index = np.random.choice(range(data.shape[0]))
        self.centers.append(init_index)

        while len(self.centers) < self.k:
            # 计算数据集中每个点到其最近中心点的最小距离
            dist_to_closest_center = np.array(
                [min([cal_distance(x, data[c]) for c in self.centers]) for index, x in enumerate(data)]
            )

            # 选择距离现有中心点最远的点
            new_index = np.argmax(dist_to_closest_center)
            self.centers.append(new_index)

    def get_center(self):
        return list(set(self.centers))

def plot_k_center_greedy(points, centers):
    plt.scatter(points[:, 0], points[:, 1], c='blue', label='Data Points')
    plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='x', s=100, label='Center Points')
    plt.title('K-Center-Greedy Result')
    plt.xlabel('Dimension 1')
    plt.ylabel('Dimension 2')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    np.random.seed(1996)
    points = np.random.rand(200, 2) * 100
    k = 20

    kc_greedy = KCenterGreedy(k)
    kc_greedy.fit(points)

    centers_index = kc_greedy.centers
    centers = points[centers_index]
    print("Center points:")
    print(kc_greedy.get_center(), len(kc_greedy.get_center()))
    # 绘制结果
    plot_k_center_greedy(points, centers)
94ce382a0bee8a884023053aff3b0d48.png

算法的优缺点

优点

  1. 适用于大规模数据

  2. 最大化数据多样性

缺点

  1. 计算复杂度较高

  2. 对噪声敏感

推荐阅读:

我的2022届互联网校招分享

我的2021总结

浅谈算法岗和开发岗的区别

互联网校招研发薪资汇总

公众号:AI蜗牛车

保持谦逊、保持自律、保持进步

dd013b376feae1483a3c61134ec45f73.jpeg

发送【蜗牛】获取一份《手把手AI项目》(AI蜗牛车著)

发送【1222】获取一份不错的leetcode刷题笔记

发送【AI四大名著】获取四本经典AI电子书

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值