-1
我正在嘗試使用DBSCANClusterer(apache.math3)對我生成的一組點進行排序並將其寫入文件。在這一點上,我被困在這裏:List <Cluster <DoublePoint>>設置爲<DoublePoint>
public Set<DoublePoint> DBSCAN(Set<DoublePoint> set2) {
Set<DoublePoint> points = new Set<DoublePoint>();
DBSCANClusterer<DoublePoint> dbscan = new DBSCANClusterer<DoublePoint>(1, 15);
//run dbscan on set of points
List<Cluster<DoublePoint>> clusters = dbscan.cluster(set2);
**sorted = clusters???**
我如何分配:List<Cluster<DoublePoint>> clusters
到Set<DoublePoint> sorted
?我猜它應該是2D-> 1D的東西!
這裏是我的代碼的其餘部分:
import java.awt.Point;
import java.io.*;
import java.util.*;
import org.apache.commons.math3.ml.clustering.Cluster;
import org.apache.commons.math3.ml.clustering.DBSCANClusterer;
import org.apache.commons.math3.ml.clustering.DoublePoint;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Main {
public static void main(final String[] args) throws Exception {
new Main().run();
}
public void run() {
Set<DoublePoint> set = generateSetPoints();
try {
writeToFile(set, "points");
} catch(IOException ex) {
System.out.println("IO Exception while writing to file");
}
Set<DoublePoint> set_by_dbscan = dbScan(set);//
try {
writeToFile(set_by_dbscan, "by_dbscan");
} catch(IOException ex) {
System.out.println("IO Exception while writing to file");
}
}
public Set<DoublePoint> generateSetPoints() {
int xx=100;
int yy=100;
Set<DoublePoint> set = new HashSet<>();
Random rnd = new Random();
int number=100;
do{
int tmp[] = new int[2];
tmp[0] = rnd.nextInt(xx);
tmp[1] = rnd.nextInt(yy);
DoublePoint rndpoint = new DoublePoint(tmp);
set.add(rndpoint);
}
while (set.size()<number);
return set;
}
public void writeToFile(Set<DoublePoint> set, String filename) throws IOException {
File fout = new File(filename + ".txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (DoublePoint p: set) {
bw.write(p.getPoint()[0] + "," + p.getPoint()[1]);
bw.newLine();
}
bw.close();
}
public Set<DoublePoint> dbScan(Set<DoublePoint> set2) {
Set<DoublePoint> points = new Set<DoublePoint>();
DBSCANClusterer dbscan = new DBSCANClusterer(1, 15);
List<Cluster<DoublePoint>> clusters = dbscan.cluster(set2);
return clusters;
}
}
謝謝你的回答!所以,我應該使用TreeSet結構來保持順序。是的,排序未排序的名稱是錯誤的,但我應該使用DBScan處理隨機生成的點,我創建,保存和加載文件。我更新了原始帖子,以包括我所有的代碼......任何建議? – JesteR
請注意,它是DBSCAN,全部大寫。我仍然沒有得到你想要「排序」的東西。 DBSCAN不排序。 –
通過使用DBSCAN,我想從隨機生成的點創建羣集。然後將它們保存在一個輸出文件中,並在GNUPLOT中使用它們來顯示它們。你能幫我嗎? – JesteR