您的位置:首页 > 编程语言 > Java开发

DataMining

2014-09-11 05:38 134 查看
写了一个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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 DataMining Java