-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBScanCluster.java
executable file
·106 lines (81 loc) · 2.29 KB
/
DBScanCluster.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package wekatest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import weka.clusterers.*;
import weka.core.*;
import weka.core.converters.ConverterUtils.DataSource;
public class DBScanCluster {
private Instances insdata;
private String sourceFile;
private String targetFile;
private String[] assignment;
public DBScanCluster(String sourceFile,String targetFile){
this.sourceFile=sourceFile;
this.targetFile=targetFile;
}
public void loadData() throws Exception{
DataSource dataSource=new DataSource(sourceFile);
insdata=dataSource.getDataSet();
}
//public List cluster() throws Exception{
public void cluster() throws Exception{
DBScan db=new DBScan();
//set options
String[] options=new String[4];
options[0]="-E";
options[1]="0.1";
options[2]="-M";
options[3]="8";
db.setOptions(options);
db.buildClusterer(insdata);
assignment=new String[insdata.numInstances()];
//System.out.println(db.toString());
ClusterEvaluation eval=new ClusterEvaluation();
eval.setClusterer(db);
eval.evaluateClusterer(insdata);
double[] num=eval.getClusterAssignments();
for(int j=0;j<insdata.numInstances();j++){
assignment[j]=String.valueOf(num[j]);
}
}
public void writeResult() throws Exception{
FileReader fr=null;
BufferedReader br=null;
FileWriter fw=null;
BufferedWriter bw=null;
String line=null;
int j=0;
try{
fr=new FileReader(sourceFile);
br=new BufferedReader(fr);
fw=new FileWriter(targetFile);
bw=new BufferedWriter(fw);
line=br.readLine();
bw.write(line+",cluster\n");
while((line=br.readLine())!=null){
bw.write(line+","+assignment[j++]+"\n");
}
}finally{
if (br!=null){
br.close();}
if(bw!=null){
bw.close();}
}
}
public void process() throws Exception{
loadData();
cluster();
writeResult();
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
int i;
for(i=0;i<100;i++){
DBScanCluster cluster=new DBScanCluster("YOUR_INPUT_PATH"+i+".csv","YOUR_OUTPUT_PATH"+i+".csv");
cluster.process();
}
}
}