聚类算法-DBSCAN-C++实现

本文介绍了DBSCAN聚类算法的C++实现,包括计算每个点的ε范围内的点数量,确定核心点,连接核心点形成聚类,处理边界点和噪声点。在处理核心点连接时,采用深度优先遍历确保准确性。并通过WPF(C#)技术对聚类结果进行图形化展示,展示了算法在处理不同形状和大小数据的优势,但也指出其在变化密度场景的局限性。
摘要由CSDN通过智能技术生成

程序流程图:


DBSCAN核心功能函数,计算每个point的eps范围内的point数量pts;

对于所有pts >Minpts的point,记为Core point;

对于所有的corepoint,将其eps范围内的core point下标添加到vector<int>::corepts中;

对于所有的corepoint,采用深度优先的方式遍历core point的所有cluster,使得相互连接的core point具有相同的cluster编号;

计算所有pts < Minpts且在Core point范围内的,记为Borderpoint;

将所有Borderpoint加入到任意一个关联的core point;

剩余的point的为Noise point,文件写写入时忽略;

将point信息写入clustering文件,程序结束。

/*
	DBSCAN Algorithm
	15S103182
	Ethan
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <limits>
#include <cmath>
#include <stack>
using namespace std;
class point{
public:
	float x;
	float y;
	int cluster=0;
	int pointType=1;//1 noise 2 border 3 core
	int pts=0;//points in MinPts 
	vector<int> corepts;
	int visited = 0;
	point (
  • 12
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 30
    评论
DBSCAN是一种基于密度聚类算法,它能够将数据点分为不同的簇,同时能够识别噪声点。该算法的思想是:密度越大的区域,越应该被划分为一个簇;密度不足的区域,应该被视为噪声点或边界点。 以下是实现鸢尾花聚类算法DBSCAN的代码: ```python import numpy as np from sklearn import datasets # 将数据集划分为核心点、边界点和噪声点 def classify_points(X, eps, min_pts): """ X: 数据集,numpy array类型 eps: 半径大小 min_pts: 最小的密度半径 """ m = X.shape[0] # 数据集大小 # 初始化点的类型(未访问) point_type = np.zeros(m, dtype=int) # 计算邻域中每个点的距离 dist = np.zeros((m, m)) for i in range(m): for j in range(m): dist[i,j] = np.linalg.norm(X[i]-X[j]) # 密度半径 neigh_points = np.array([np.where(dist[i] < eps)[0] for i in range(m)]) neigh_counts = np.array([len(neigh_points[i]) for i in range(m)]) # 当前簇的标识 cluster_id = 1 # 标记核心点、边界点以及噪声点 for i in range(m): if point_type[i] != 0: continue if neigh_counts[i] >= min_pts: point_type[i] = cluster_id # 若i是核心点,则将其邻域中的点也归为簇中 for j in neigh_points[i]: if neigh_counts[j] >= min_pts: point_type[j] = cluster_id if point_type[j] == 0: point_type[j] = -1 # 标记为边界点 else: point_type[i] = -1 # 标记为噪声点 cluster_id += 1 return point_type # DBSCAN算法主体 def dbscan(X, eps, min_pts): point_type = classify_points(X, eps, min_pts) n_clusters = max(point_type) clusters = [] for i in range(1, n_clusters+1): clusters.append(X[point_type == i]) return clusters # 加载数据集 iris = datasets.load_iris() X = iris["data"][:, :2] # 聚类 clusters = dbscan(X, 0.5, 5) # 绘制聚类结果 import matplotlib.pyplot as plt colors = ['red', 'green', 'blue', 'purple', 'black'] labels = ['Cluster {}'.format(i+1) for i in range(len(clusters))] for i, cluster in enumerate(clusters): plt.scatter(cluster[:,0], cluster[:,1], c=colors[i], label=labels[i]) plt.legend() plt.show() ``` 在上述代码中,`classify_points`函数计算每个点的密度半径,并将其分类为核心点、边界点和噪声点。`dbscan`函数将得到的结果按照簇的数量返回。 最后,我们使用鸢尾花数据集的前两列特征,用半径为0.5、最小密度半径为5的DBSCAN算法进行聚类,并绘制结果图。
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值