Desc: 层次聚类算法的实现 Args: self : self points : 所有的点 Return: list of clusters Raise: None
(self, points)
| 460 | self.grids = {} |
| 461 | |
| 462 | def run(self, points): |
| 463 | """ |
| 464 | Desc: 层次聚类算法的实现 |
| 465 | Args: |
| 466 | self : self |
| 467 | points : 所有的点 |
| 468 | Return: |
| 469 | list of clusters |
| 470 | Raise: |
| 471 | None |
| 472 | """ |
| 473 | # 每个点构建一个HCCluster |
| 474 | self.build_clusters(points) |
| 475 | # 构建点与点之间的friend关系 |
| 476 | self.build_friends() |
| 477 | # 深度优先merge |
| 478 | merge_count = 1 |
| 479 | while merge_count > 0: |
| 480 | merge_count = 0 |
| 481 | for hc in self.hcclusters: |
| 482 | if not hc.is_empty(): |
| 483 | if hc.bfs_merge(self.width): |
| 484 | merge_count += 1 |
| 485 | # 从中间结果中得出最终的聚类结果 |
| 486 | ret = [] |
| 487 | for hc in self.hcclusters: |
| 488 | if not hc.is_empty(): |
| 489 | ret.append(hc) |
| 490 | return ret |
| 491 | |
| 492 | def build_clusters(self, points): |
| 493 | """ |
no test coverage detected