java - 如何使用apache的DBSCANClusterer

标签 java matrix cluster-analysis

我有一个问题中提到的距离矩阵:

Clustering with a distance matrix

现在,我想使用 apache 中的 DBSCANclusterer.java 类在此矩阵上执行 DBSCAN。

“cluster”方法将点的集合作为输入。这些点的格式是什么?

引用上面的矩阵,我应该在集合参数中添加什么?

有人可以粘贴代码片段吗?我想将距离指定为:

甲乙:20 甲、乙:20 . . .

然后当我完成聚类时,相似的样本应该聚集在一起。

最佳答案

希望这对您有所帮助。

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 方法中声明。

关于java - 如何使用apache的DBSCANClusterer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188418/

相关文章:

java Scanner 只读取前 2048 个字节

java - 如果某些东西已被弃用,我仍然可以使用它吗?

java - jackson :反序列化递归对象

matlab - 排列矩阵行内的元素

r - 没有数据矩阵,nbclust 不起作用

java - 在 java 中访问网络共享文件夹(位于 Windows 或 Linux 中)的推荐方法是什么

arrays - 查找分组在数组中的特定零点 - matlab

opencv - O'Reilly 书籍对 2D 线性系统的澄清

matlab - 朴素贝叶斯分类器和判别分析的准确性还差得很远

machine-learning - 聚类算法(如 Kmeans 和 EM)的特征缩放(归一化)