DBSCAN聚类算法(基于密度方法的聚类)
1、以下图的样本数据为测试数据、不使用java自带的DBSCAN算法进行编写,自行从0开始实现此算法,由于时间紧迫,比较粗糙,100可得60、因代码有太多遍历样本集合以及还有类似递归的调用自身方法的方法,导致代码不够优美,要不是时间太少,肯定是容忍不了的,但同时也有一二优点,比如会下意识的使用解耦,将几个复杂模块分开,然后逐一解决、如图我使用两个方法,和一个main方法,其中hx。。。方法主要处理判断是否能成簇,如果能则返回成簇的集合(即成簇中点的集合),不能则返回null。这样方便main方法和big。。方法获取数据以及让代码看起来优雅一点,big。。方法则是不断判断核心圆中的点是否为核心圆,如果是又调用自身继续判断,直到此簇的所有点都遍历到,其余不做赘述。
以此为例:
得java代码:
package bigData;
import java.awt.*;
import java.util.Arrays;
public class Test01 {
public int[] B=new int[20];
public int ib=0;
public Point[] hxPoint(int[][] A,double r,int a,Point point){
Point[] points = new Point[20];
int b=0;
for (int i=0;i<A.length;i++){
for (int j =0;j<A[i].length;j++){
if (A[i][j]>0){
if (i==point.x&&j!=point.y) {
if (Math.abs(point.y-j)<=r){
points[b] = new Point(i,j);
b++;
}
}else if (j==point.y&&i!=point.x) {
if (Math.abs(point.x - i) <= r) {
points[b] = new Point(i, j);
b++;
}
}else if (i==point.x&&j==point.y){
points[b] = new Point(i,j);
b++;
} else {
if ( Math.sqrt(Math.abs((point.x-i)*(point.x-i))+Math.abs((point.y-j)*(point.y-j)))<=r){
points[b] = new Point(i,j);
b++;
}
}
}
}
}
Point[] points1 = new Point[b];
for (int i=0;i<b;i++){
points1[i] = new Point(points[i].x,points[i].y);
}
if (b>=a) return points1;
return null;
}
public void bigData(int[][] A,double r,int a,Point[] points,Point point1){
for (Point point : points) {
if ((A[point.x][point.y]>100)) {
B[ib++] = A[point.x][point.y];
}
if (A[point.x][point.y] > 100) {
Point[] points1 = hxPoint(A, r, a, point);
if (points1 == null ) {
A[point.x][point.y] = A[point.x][point.y] - 100;
} else if (points1 != null) {
A[point.x][point.y] = A[point.x][point.y] - 100;
bigData(A, r, a, points1,point);
}
}
}
}
public static void main(String[] args) {
int[][] A = new int[][]{ {0,0 ,0 ,0 ,0 ,0 ,0},
{0,0 ,101,0 ,0 ,102,0},
{0,103,104,105,106,107,108},
{0,109,110,0 ,0 ,111,0},
{0,0 ,112,0 ,0 ,0 ,0}};
double r = 1.0; int a = 4;int c=0;int d=1;
Test01 test01 = new Test01();
for (int i=0;i<A.length;i++) {
for (int j = 0; j < A[i].length; j++) {
if (A[i][j] > 100) {
Point[] points = test01.hxPoint(A, r, a, new Point(i, j));
if (points != null) {
test01.B[test01.ib++] = A[i][j];
A[i][j] = A[i][j] - 100;
test01.bigData(A, r, a, points,new Point(i, j));
System.out.println("此时最终成簇"+(d++)+"的集合为:=======================\n");
System.out.print("(");
for (int n=c;n<test01.ib;n++) {
System.out.print(test01.B[n]+",");
}
System.out.println(")");
c=test01.ib;
}
}
}
}
System.out.println("A集合中的元素为:");
System.out.println(Arrays.toString(A[0]));
System.out.println(Arrays.toString(A[1]));
System.out.println(Arrays.toString(A[2]));
System.out.println(Arrays.toString(A[3]));
System.out.println(Arrays.toString(A[4]));
}
}
结果截图:
|