DBSCAN 聚类 和 gmm 聚类 测试
测试代码
import math
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.mixture import GaussianMixture
# 给定的线段数据(x1, y1, x2, y2)
lines = [
# (265, 106, 268, 1),
(265, 105, 268, 0), # 点1: (265, 105), (268, 0)
(246, 62, 247, 7), # 点2: (246, 62), (247, 7)
(246, 60, 246, 5) # 点3: (246, 60), (246, 5)
]
lines_polar=[]
for line in lines:
x1, y1, x2, y2 = line
dx = x2 - x1
dy = y2 - y1
if dx == 0:
x_value_when_y_0 = x1
if dy != 0:
angle_radians= math.pi / 2 # 对应90度,转换为弧度制为pi/2
else:
angle_radians= 0
else:
slope = (y2 - y1) / (x2 - x1)
if slope == 0:
x_value_when_y_0 = x1
else:
x_value_when_y_0 = x1 - y1 / slope
m = dy / dx
angle_radians = math.atan(m)
lines_polar.append((x_value_when_y_0, angle_radians*2))
# 提取所有点 (x1, y1) 和 (x2, y2)
# 将点转换为 NumPy 数组
points = np.array(lines)
# DBSCAN 聚类
def dbscan_cluster(points, eps=6, min_samples=2):
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
dbscan.fit(points)
return dbscan.labels_
# GMM 聚类
def gmm_cluster(points, n_components=2):
gmm = GaussianMixture(n_components=n_components)
gmm.fit(points)
labels = gmm.predict(points)
return labels
# 比较 DBSCAN 和 GMM 聚类
def compare_clustering(points):
# 使用 DBSCAN 聚类
dbscan_labels = dbscan_cluster(points)
# 使用 GMM 聚类
gmm_labels = gmm_cluster(points)
# 打印聚类标签
print("DBSCAN 聚类标签:", dbscan_labels)
print("GMM 聚类标签:", gmm_labels)
# 主函数
if __name__ == "__main__":
compare_clustering(points)
经过测试发现 ,当需要聚类的点数小于4个时,DBSCAN 聚类 效果很差,gmm效果较为不错。