利用随机数产生原始数据,包括正常数据和噪声数据。运算后将不同簇的数据放入不同的txt文件中,异常点单独存放于一个文件,然后用matlab画图。100个正常点,10个异常点的运行结果如图:
Kmedoids.java
import java.io.*;
import java.util.*;
public class Kmedoids {
int k; //dimensionality
int len;
double threshold=6; //distinguish the normal and noise points
// double[] ceneru;
// double[][] dismatrix;
// double undis;
// double[] crita;
// int repeat;
int node; //the number of normal points
int noinode; //the number of noise points
DataObject[] center; //center point
ArrayList<DataObject> data; //store all the points
public Kmedoids(int k,int len,int node,int noinode) throws IOException{
this.k=k;
this.node=node;
this.noinode=noinode;
this.len=len;
center=new DataObject[k];
Random random=new Random(System.currentTimeMillis());
data=new DataSource(node,noinode,len).getObjects();
int[] equal=new int[k];
for(int i=0;i<k;){
int j=0;
int t=random.nextInt(node+noinode);
for(;j<=i;j++){
if(t==equal[j]){
break;
}
else{
continue;
}
}//for...j
if(j>i){
DataObject tmp=data.get(t);
equal[i]=t;
center[i++]=tmp;
}
}//for...i
}
//initialize all points,classify all of them into different centers based on distance
private void initCenter(){
Iterator<DataObject> iter=data.iterator();
while(iter.hasNext()){
int number1=0;
int number2=0;
DataObject point=iter.next();
double max