2013-11-25 38 views
5

我有一個距離矩陣在這裏提到的問題:如何使用Apache的DBSCANClusterer

Clustering with a distance matrix

現在,我想在使用的DBSCANclusterer.java類從阿帕奇這個矩陣進行DBSCAN。

該方法'集羣'作爲輸入,點的集合。這些要點的格式是什麼?

參考上面的矩陣,我將什麼添加到收集參數?

有人可以粘貼一段代碼嗎?我想指定距離爲:

A,B:20 A,C:20 。 。 。

然後當我完成聚類時,類似的樣本應該聚集在一起。

+0

那我該怎麼辦?另外,你能指點我一個以上述矩陣爲輸入並執行DBSCAN/Hierarchial集羣的程序嗎?我嘗試瞭解不同的程序。我經歷了交叉驗證和stackoverflow。但他們都指向方法。我只想要一個程序,我可以提供上述矩陣並進行聚類。 – Nikhil

+0

查找程序的問題對於StackOverflow是無關緊要的。這是一個*編程*網站。 –

+0

因此,無論你是谷歌多一些(有聚類工具包,可以讀取外部距離矩陣),或者你只是嘗試自己實現DBSCAN,這不是很難... –

回答

6

希望這會有所幫助。

public class App { 

public static void main(String[] args) throws FileNotFoundException, IOException { 
    File[] files = getFiles("./files2/"); 

    DBSCANClusterer dbscan = new DBSCANClusterer(.05, 50); 
    List<Cluster<DoublePoint>> cluster = dbscan.cluster(getGPS(files)); 

    for(Cluster<DoublePoint> c: cluster){ 
     System.out.println(c.getPoints().get(0)); 
    }      
} 

private static File[] getFiles(String args) { 
    return new File(args).listFiles(); 
} 

private static List<DoublePoint> getGPS(File[] files) throws FileNotFoundException, IOException { 

    List<DoublePoint> points = new ArrayList<DoublePoint>(); 
    for (File f : files) { 
     BufferedReader in = new BufferedReader(new FileReader(f)); 
     String line; 

     while ((line = in.readLine()) != null) { 
      try { 
       double[] d = new double[2]; 
       d[0] = Double.parseDouble(line.split(",")[1]); 
       d[1] = Double.parseDouble(line.split(",")[2]); 
       points.add(new DoublePoint(d)); 
      } catch (ArrayIndexOutOfBoundsException e) { 
      } catch(NumberFormatException e){ 
      } 
     } 
    } 
    return points; 
} 
} 

樣本數據:

12-01-99 11:31:01 AM, -40.010, -70.020 
12-01-99 11:32:01 AM, -41.010, -71.020 
12-01-99 11:33:01 AM, -42.010, -72.020 
12-01-99 11:34:01 AM, -43.010, -73.020 
12-01-99 11:35:01 AM, -40.010, -74.020 

一個叫files2與GetFiles方法聲明的位置文件夾中的所有文件。

+0

你能告訴我文件的格式嗎?文件中的數據應該如何顯示? – Nikhil

+0

Unixtimestamp,lat,lon –