【问题标题】:How to cluster my data with a custom distance matrix using smile library's CLARANS method如何使用微笑库的 CLARANS 方法使用自定义距离矩阵对我的数据进行聚类
【发布时间】:2019-10-15 01:09:55
【问题描述】:

我希望使用自定义距离矩阵而不是内置算法(即欧几里得)对我的数据进行聚类。而且似乎没有明确的方法。

我尝试将我的一些代码添加到 Smile 项目的演示中。还尝试在我的项目中进行测试,这是代码的一部分:

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = vrpJsonFromFile.readLine()) != null) {
            sb.append(line).append("\n");
        }
        JSONArray jsonArray = new JSONObject(sb.toString()).getJSONArray("services");
        Double[][] data = new Double[jsonArray.length()][2];
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject address = jsonArray.getJSONObject(i).getJSONObject("address");
            data[i][0] = Double.parseDouble(address.getString("lon"));
            data[i][1] = Double.parseDouble(address.getString("lat"));
        }

        // here
        Distance<Double[]> distance1 = (x, y) -> Math.sqrt(Math.pow(y[1]-x[1],2) + Math.pow(y[0]-x[0], 2));
        CLARANS<Double[]> clarans = new CLARANS<>(data, distance1, 3);
        System.out.println(clarans);

此代码使用欧几里得算法创建 CLARANS 聚类(请参见 //here 注释下方的行)。我应该用我自己的距离矩阵来改变它,我希望在 Smile 有办法做到这一点。

【问题讨论】:

    标签: java cluster-analysis k-means distance-matrix smile


    【解决方案1】:

    你可能会使用

    Distance<Integer> d = (i,j) -> matrix[i][j];
    

    聚类对象编号,而不是它们的向量。

    但可能值得关注 ELKI,它具有预定义的距离矩阵类,并为对象集使用优化的表示,而不是像上面的 lambda 那样使用昂贵的盒装 Integer。因为ij 是装箱整数,所以每次距离计算都需要额外的内存间接(和缓存未命中),这会大大降低性能。它还具有更好的 FastCLARANS 算法,以及据称速度快 O(k) 倍的 FastPAM。

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2011-04-13
      • 1970-01-01
      • 2014-07-09
      • 2017-04-13
      • 2014-03-20
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多