2017-02-02 1 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

관련 문제