2017-02-02 183 views
-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>> clustersSet<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; 
    } 
} 

回答

0

HashSet的是未排序数据结构。

如果您想要对sorted进行排序,请使用保留顺序的东西。

sorted = nes HashSet<>()是一个真正的跆拳道...

此外,DBSCAN并不意味着排序。改用OPTICS集群。

+0

谢谢你的回答!所以,我应该使用TreeSet结构来保持顺序。是的,排序未排序的名称是错误的,但我应该使用DBScan处理随机生成的点,我创建,保存和加载文件。我更新了原始帖子,以包括我所有的代码......任何建议? – JesteR

+0

请注意,它是DBSCAN,全部大写。我仍然没有得到你想要“排序”的东西。 DBSCAN不排序。 –

+0

通过使用DBSCAN,我想从随机生成的点创建群集。然后将它们保存在一个输出文件中,并在GNUPLOT中使用它们来显示它们。你能帮我吗? – JesteR

相关问题