{"id":18720425,"url":"https://github.com/yangjianxin1/pamae","last_synced_at":"2025-07-20T17:37:15.240Z","repository":{"id":102509229,"uuid":"231032677","full_name":"yangjianxin1/PAMAE","owner":"yangjianxin1","description":"使用Python复现SIGKDD2017的PAMAE算法(并行k-medoids算法)/The Python implementation of SIGKDD 2017's PAMAE algorithm (parallel k-medoids algorithm)","archived":false,"fork":false,"pushed_at":"2020-01-01T05:15:43.000Z","size":1955,"stargazers_count":34,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T14:15:30.891Z","etag":null,"topics":["clustering","k-medoids","kmedoids","pam","parallel","sigkdd"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yangjianxin1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-12-31T05:40:15.000Z","updated_at":"2025-01-17T09:49:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"af102fce-1de5-4d50-89f0-c587ac30b566","html_url":"https://github.com/yangjianxin1/PAMAE","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yangjianxin1/PAMAE","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FPAMAE","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FPAMAE/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FPAMAE/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FPAMAE/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yangjianxin1","download_url":"https://codeload.github.com/yangjianxin1/PAMAE/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FPAMAE/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266166989,"owners_count":23886904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clustering","k-medoids","kmedoids","pam","parallel","sigkdd"],"created_at":"2024-11-07T13:30:58.167Z","updated_at":"2025-07-20T17:37:15.212Z","avatar_url":"https://github.com/yangjianxin1.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PAMAE: Parallel k-Medoids Clustering with High Accuracy and Efficiency论文复现\n\n## 项目介绍\n[PAMAE: Parallel k-Medoids Clustering with High Accuracy and Efficiency](https://www.researchgate.net/publication/318918153_PAMAE_Parallel_k_-Medoids_Clustering_with_High_Accuracy_and_Efficiency)\n是SIGKDD2017一篇关于k-medoids并行聚类的论文，论文中作者使用Spark与Hadoop实现算法的并行化，而本项目使用python并行编程模拟MapReduce的并行，对该论文算法的思想进行复现。\n\n使用本项目复现的代码对中心数量分别为5、10、15、20的数据集进行聚类的效果图如下（数据集大小为1万）\n\n![5cluster](image/PHASE2-5cluster.png)\n![10cluster](image/PHASE2-10cluster.png)\n\n![15cluster](image/PHASE2-15cluster.png)\n![20cluster](image/PHASE2-20cluster.png)\n\n\n## 使用方法\n直接运行如下命令即可，程序会使用默认参数，生成一个数据集，并对该数据集执行聚类算法\n```\npython pamae.py\n```\n也可以指定如下参数：\n- n_points：生成的数据的个数，默认为10000\n- subset_size：phase 1中采样后子集的大小，默认为100\n- subset_num：phase 1中采样的子集的数量 ，默认为5\n- centroid_num：簇中心的数量 ，默认为10\n```\npython pamae.py --n_points 10000 --subset_size 100 --subset_num 5 --centroid_num 10\n```\n\n程序产生的Phase 1与Phase\n2的聚类结果图，会保存在根目录的results文件夹下，命名方式为\"phase[1|2]_数据集大小_采样子集大小_采样子集数量_中心数量\"，并且在控制台输出每个阶段的的中心集合、耗时、聚类误差等数据\n\n\n## 背景介绍\n聚类就是将数据集划分为多个簇(cluster)，每个簇由若干相似对象组成，使得同一个簇中对象间的相似度最大化，不同簇中对象间的相似度最小化。其中k-means与k-medoids是最基础的两种聚类算法\n\nk-means算法选择簇中所有对象的均值作为簇的中心，因此k-means算法实现简单、时间复杂度低，但其对噪声和离群点很敏感\n\nk-medoids算法则是选择离簇均值最近的对象作为簇中心。所以k-medoids算法对噪声和离群点更具鲁棒性，但其时间复杂度却很高。\n\n| | k-means |k-medoids|\n|---------|--------|--------|\n|优点|实现简单、时间复杂度低|对噪声和离群点更具鲁棒性|\n|缺点|对噪声和离群点很敏感|时间复杂度高|\n\n### k-means算法\n算法简介：k-means算法采用簇中所有对象的均值作为簇中心。给定划分的簇的数量k。随机选择k个对象作为k个簇中心，将剩余对象指派到距离最近的簇，然后重新计算每个簇的新均值，得到更新后的簇中心。不断重复上述步骤，直到簇中心不再发生变化。\n\nk-means算法伪代码如下： \n```\n输入：数据集D，划分簇的个数k\n输出：k个簇的集合\n从数据集D中任意选择k个对象作为初始簇中心\nrepeat\n    for 数据集D中每个对象P do\n        计算对象P到k个簇中心的距离\n        将对象P指派到与其最近(距离最短)的簇\n    end for\n    计算每个簇中对象的均值，以该均值点作为新的簇的中心\nuntil k个簇的簇中心不再发生变化\n```\n\nk-means算法示意图如下： \n\n![kmeans](image/kmeans.jpg)\n\n### PAM算法\n算法简介：PAM算法是k-medoids算法的变种，PAM算法并不是采用簇的均值作为簇中心，而是选择簇中距平均值最近的对象作为簇中心\n\n聚类误差S：所以对象到其簇中心的距离之和\n\nPAM算法伪代码如下： \n```\n输入：数据集D，划分簇的个数k\n输出：k个簇的集合\n从数据集D中任意选择k个对象作为初始簇中心\nrepeat\n    把剩余对象分配到距离最近的簇中心\n    对于所有(中心点C，非中心点P)的二元组。尝试将中心点与非中心点交换，并计算聚类误差\n    若交换之后聚类误差降低了，则使用该非中心点替换中心点\nuntil k个簇的簇中心不再发生变化\n```\n\nPAM算法示意图如下： \n\n![pam](image/pam.jpg)\n\n## 研究现状\n尽管k-medoids具有更好的鲁棒性，但是由于其计算复杂度过高，所以人们用的主要还是k-means算法。有许多研究者尝试解决k-medoids算法的效率问题，\n但基本都是以算法准确率作为代价，也就是说现有研究都没能很好地解决k-medoids算法的效率与准确率之间的矛盾。\n\n解决k-medoids算法效率问题的各种举措可以按照以下三个维度进行划分\n\n![kmedoids_category](image/kmedoids_category.jpg)\n \n通过上表，我们可以清楚看到 \n- global search与entire data的使用可以提高算法的准确率，但会降低算法的效率\n- sampled data与local search的使用可以提高算法效率，但会降低准确率\n\n## 论文贡献\n本文尝试在k-medoids的准确率与效率之间找到一个平衡点，于是作者提出了一个基于k-medoids的并行聚类算法(PAMAE)，算法分为两个阶段，每个阶段都使用Spark和Hadoop实现并行处理，提高算法的效率：\n- 在第一阶段采用sampled data与global search策略\n- 在第二阶段采用entire data与local search策略\n\n进行以上组合的原因： global search与entire\ndata策略的使用可以提高算法的准确率，但同时使用，必定会降低算法的效率。而entire\ndata与local\nsearch策略可以提高算法的效率，但同时使用，也必定会降低算法的准确率。因此作者在四者的组合中找到了一个平衡点，即sampled\ndata+global search与entire data+local search。\n\n***第一阶段的sampled data可以提高算法效率，global\nsearch可以提高算法准确率，这是一个高效率+高准确率的组合。***\n\n***第二阶段的entire data可以提高算法准确率，local\nsearch可以提高效率，也是一个高效率+高准确率的组合。***\n\n***可以发现每个阶段都是高效率+高准确率的组合，PAMAE算法就是通过这种“一快一准”的策略组合，再加上MapReduce的并行处理，以达到高效率和高准确率之间的平衡***\n\n![pamae](image/pamae.png)\n\n## PAMAE算法描述\nPAMAE算法分为如下两个阶段 \n### Phase 1\n算法思想：对整个数据集进行随机采样(sampled data\n)，生成m个规模为n的子集。然后对m个子集并行执行PAM算法(global\nsearch)，计算整个数据集的聚类误差，选择聚类误差最小的中心集合\n\n算法流程： \n```\n对整个数据集进行随机采样，生成m个规模为n的子集substes\nfor subset in subsets:\n    使用global search策略(这里使用PAM算法)找到子集subset的k个簇中心\n    将整个数据集Entire data的每个数据划分到距离最近的簇中心\n    计算聚类误差(每个数据点到其簇中心的距离之和)\n选择聚类误差最小的一组中心集合输入到Phase2    \n```\n***注：对于for循环里每个子集的聚类与计算聚类误差的操作，是并行的***\n\n第一阶段的算法示意图如下： \n\n![phase1](image/phase1.jpg)\n \n### Phase 2\n经过Phase 1的步骤，已经得到了一个近似的聚类中心集合，但是Phase\n1中的中心集合是从sampled\ndata中得到的，因此可能与真实的中心还存在一定的偏差，第二阶段就是为了对第一阶段生成的中心集合进行微调\n\n算法思想：对于Phase\n1获得的中心集合，将整个数据集的数据划分到距离最近的簇中心，然后并行更新每个簇的中心，以达到对簇中心进行微调的目的。得到的就是最终的k个簇中心\n\n第二阶段的算法示意图如下： \n\n![phase2](image/phase2.jpg)\n\n## 实验结果\n数据集大小为1万，不同中心数量的数据集，第一阶段和第二阶段聚类结果如下所示。可以看到，经过Phase\n1得到的聚类效果已经很不错，再经过Phase\n2微调之后，聚类效果也确实有所提高。\n \n簇中心数量为5：\n \n![PHASE1-5cluster](image/PHASE1-5cluster.png)\n![PHASE2-5cluster](image/PHASE2-5cluster.png)\n\n簇中心数量为10：\n\n![PHASE1-10cluster](image/PHASE1-10cluster.png)\n![PHASE2-10cluster](image/PHASE2-10cluster.png)\n \n簇中心数量为15：\n \n![PHASE1-15cluster](image/PHASE1-15cluster.png)\n![PHASE2-15cluster](image/PHASE2-15cluster.png)\n\n簇中心数量为20：\n\n![PHASE1-20cluster](image/PHASE1-20cluster.png)\n![PHASE2-20cluster](image/PHASE2-20cluster.png)\n\n## 不足\n本项目使用python的并行编程模拟Spark与Hadoop的MapReduce并行计算，运算速度不如MapReduce。由于Phase\n1采用的是Global\nsearch的策略，运算复杂度较高，当采样子集规模较大时，Phase \n1的耗时会增大。\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyangjianxin1%2Fpamae","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyangjianxin1%2Fpamae","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyangjianxin1%2Fpamae/lists"}