- 任务: 用DBSCAN在二维或三维实数坐标上做聚类,输出“簇的数量”和“噪声点数量”。
- 定义: 距离为欧氏距离;某点的邻域半径为eps;若该点邻域内样本数(含自身)≥ min_samples,则为核心点;从未访问核心点出发,按邻域可达关系扩展一个簇;不被任何簇吸收的点视为噪声
1.5 2 6 0 0 0.5 0 0 0.5 10 10 10.5 10 10 10.5
2 0
前3个点彼此间距都≤1.5,形成一簇;后3个点同理形成另一簇;无噪声。
本题由牛友@Charles 整理上传
from math import sqrt eps, min_sample, x = map(eval, input().split()) nodes = [] for _ in range(x): nodes.append(tuple(map(eval, input().split()))) def dist(n1, n2): tmp = 0 for x1, x2 in zip(n1, n2): tmp += (x1 - x2) ** 2 return sqrt(tmp) def func(node): if node in centers: return True tmp = [] for n in nodes: if dist(node, n) <= eps: tmp.append(n) if len(tmp) < min_sample: return False centers.add(node) closed.update(tmp) for n in tmp: func(n) return True centers = set() closed = set() clusters = 0 for node in nodes: if node in closed: continue elif func(node): clusters += 1 print(clusters, x - len(closed))