DataMining

写了一个Data Mining的小测试,用的是DBSCAN的一个聚类算法, 刚自学Java, 感觉写的丑陋,O(∩_∩)O~

</pre><pre name="code" class="java">package DBSCAN;  
  
import java.util.Arrays;  
  
class Distance{  
    private Points point=new Points();  
    private int length=point.getLength();  
    private double[][] dist=new double[this.length][this.length];  
      
    public Points getPoint() {  
        return point;  
    }  
  
    public void setPoint(Points point) {  
        this.point = point;  
    }  
  
    public double getSqu(double val){  
        return val*val;  
    }  
      
    //method1, Euclidean distance  
    public double[][] getDist(){  
        for(int i1=0; i1<this.point.getLength(); i1++){  
            for(int i2=0; i2<this.point.getLength(); i2++){  
                this.dist[i1][i2]=(this.getSqu(this.point.getIntArray()[i1][0]-this.point.getIntArray()[i2][0])  
                                       +this.getSqu(this.point.getIntArray()[i1][1]-this.point.getIntArray()[i2][1]));  
            }  
        }  
        return this.dist;  
    }  
}  
  
  
class Points{  
    private int intArray[][]=new int[8][2];  
    private int error=4;  
    private int min_support=3;  
      
    public Points() {  
        super();  
        // TODO Auto-generated constructor stub  
    }  
  
  
    public Points(int[][] intArray, int error, int min_support) {  
        super();  
        this.intArray = intArray;  
        this.error = error;  
        this.min_support = min_support;  
    }  
  
    public int getLength(){  
        return this.intArray.length;  
    }  
  
    public int getError() {  
        return error;  
    }  
  
    public void setError(int error) {  
        this.error = error;  
    }  
  
    public int getMin_support() {  
        return min_support;  
    }  
  
    public void setMin_support(int min_support) {  
        this.min_support = min_support;  
    }  
  
    public int[][] getIntArray() {  
        return intArray;  
    }  
  
    public void setIntArray(int[][] intArray) {  
        this.intArray = intArray;  
    }  
  
    @Override  
    public String toString() {  
        return "Points [intArray=" + Arrays.toString(intArray) + ", error="  
                + error + ", min_support=" + min_support + "]";  
    }  
      
    public void showArray(){  
        for(int i=0; i<this.intArray.length; i++){  
            System.out.print("Point"+i+"--> (");  
            for(int j=0; j<this.intArray[i].length; j++){  
                System.out.print(this.intArray[i][j]+" ");  
            }  
            System.out.println(")");  
        }  
    }  
}  
  
public class DBSCANCLUSTERING {  
    public static void main(String[] args){  
        final int intArray[][]={{2,10},{5,8},{1,2},{2,5},{8,4},{7,5},{6,4},{4,9}};  
        double distance[][]=new double[intArray.length][intArray.length];  
        int counter[]=new int[intArray.length];  
        int index[][]=new int[intArray.length][intArray.length];  
        Points point=new Points(intArray,4,3);  
        point.showArray();  
          
        Distance dist=new Distance();  
        dist.setPoint(point);  
        distance=dist.getDist();  
        
        //check core points and clusters, with minimum support 3, and maximum distance 4  
        for(int i=0; i<distance.length; i++){  
            for(int j=0; j<distance[i].length; j++){  
                if(distance[i][j]<point.getError()*point.getError() && distance[i][j]>0){  
                    counter[i]++;  
                    index[i][j]=1;  
                }  
                System.out.print(distance[i][j]+" ");  
            }  
            System.out.println();  
            if(counter[i]>=(point.getMin_support())){  
                System.out.println("Point"+(i)+"-->("+point.getIntArray()[i][0]+","+point.getIntArray()[i][1]+")"+" are core points!");  
                for (int j=0; j<counter.length; j++){  
                    if(index[i][j]==1){  
                        System.out.println("Point"+(j)+" are points in this cluster.");  
                    }  
                }  
                  
            }  
        }  
        
        //check if exist two core points are the border points of each other, if exist combine two clusters
        for(int i=0; i<counter.length; i++){  
            if(counter[i]>=(point.getMin_support())){  
                for(int j=i+1; j<counter.length; j++){  
                    if(counter[j]>=(point.getMin_support())){  
                        if(distance[i][j]<point.getError()*point.getError()){  
                            System.out.println("Point"+(i)+"-->("+point.getIntArray()[i][0]+","+point.getIntArray()[i][1]+")"+" and "+  
                                    "Point"+(j)+"-->("+point.getIntArray()[j][0]+","+point.getIntArray()[j][1]+")"+" share the same cluster,"  
                                            + "combine these two clusters.");  
                        }  
                    }  
                }  
            }  
        }  
    }  
} 

 

最后跑出的数据的结果:

Point0--> (2 10 )
Point1--> (5 8 )
Point2--> (1 2 )
Point3--> (2 5 )
Point4--> (8 4 )
Point5--> (7 5 )
Point6--> (6 4 )
Point7--> (4 9 )
</pre><pre name="code" class="java">0.0 13.0 65.0 25.0 72.0 50.0 52.0 5.0 
13.0 0.0 52.0 18.0 25.0 13.0 17.0 2.0 
</pre><pre name="code" class="java">Point1-->(5,8) are core points!
Point0 are points in this cluster.
Point5 are points in this cluster.
Point7 are points in this cluster.
</pre><pre name="code" class="java">65.0 52.0 0.0 10.0 53.0 45.0 29.0 58.0 
25.0 18.0 10.0 0.0 37.0 25.0 17.0 20.0 
72.0 25.0 53.0 37.0 0.0 2.0 4.0 41.0 
50.0 13.0 45.0 25.0 2.0 0.0 2.0 25.0 
</pre><pre name="code" class="java">Point5-->(7,5) are core points!
Point1 are points in this cluster.
Point4 are points in this cluster.
Point6 are points in this cluster.
</pre><pre name="code" class="java">52.0 17.0 29.0 17.0 4.0 2.0 0.0 29.0 
5.0 2.0 58.0 20.0 41.0 25.0 29.0 0.0 
</pre><pre name="code" class="java">Point1-->(5,8) and Point5-->(7,5) share the same cluster,combine these two clusters.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值