从头开始实现自定义聚合算法



我知道聚集聚类算法,它以每个数据点作为单独的聚类开始,然后组合点形成聚类的方式。

现在,我有一个 n 维空间和几个数据点,这些数据点在每个维度上都有值。我想根据业务规则对两个点/集群进行聚类,例如:

  • 如果跨维度 1 的聚类之间的距离<为 _x0032_=" 的距离为=" c1=" 和=" c2="><...以及跨维度>
  • 如果满足跨维度
  • 1 的规则并且满足跨维度 2 的规则,则将它们聚类,而无需为其他维度而烦恼......

....和类似的自定义规则。

此外,我有自己的方法来定义和测量任何特定维度上任何两个集群之间的距离。维度可能只包含字符串,我想定义自己的字符串距离指标。在另一个维度中,它可能保存位置名称,并且沿此维度的两个点之间的距离是命名位置之间的地理距离,对于其他维度也是如此。

是否有框架/软件可以让我实现这种定义自定义距离指标的方法,然后实现聚集聚类?当然,当任何时间点不满足业务规则时,聚集聚类就会停止,我们在最后的 n 维空间中形成了聚类。

谢谢阿布舍克·

你可以

用Weka来做。

您必须实现一个距离函数,并使用 setDistanceFunction(DistanceFunction distanceFunction) 方法将其传递给分层聚类器。

Weka中其他可用的集群器是:Cobweb,EM,FarthestFirst,FilteredClusterer,MakeDensityBasedClusterer,RandomizableClusterer,RandomizableDensityBasedClusterer,RandomizableSingleClustererEnhancer,SimpleKMeans,SingleClustererEnhancer。

来自 NormalizableDistance 类的一个示例距离函数:

  /** Index in ranges for MIN. */
  public static final int R_MIN = 0;
  /** Index in ranges for MAX. */
  public static final int R_MAX = 1;
  /** Index in ranges for WIDTH. */
  public static final int R_WIDTH = 2;
  /** the instances used internally. */
  protected Instances m_Data = null;
  /** True if normalization is turned off (default false).*/
  protected boolean m_DontNormalize = false;
  /** The range of the attributes. */
  protected double[][] m_Ranges;
  /** The range of attributes to use for calculating the distance. */
  protected Range m_AttributeIndices = new Range("first-last");
  /** The boolean flags, whether an attribute will be used or not. */
  protected boolean[] m_ActiveIndices;
  /** Whether all the necessary preparations have been done. */
  protected boolean m_Validated;

public double distance(Instance first, Instance second, double cutOffValue, PerformanceStats stats) {
    double distance = 0;
    int firstI, secondI;
    int firstNumValues = first.numValues();
    int secondNumValues = second.numValues();
    int numAttributes = m_Data.numAttributes();
    int classIndex = m_Data.classIndex();
    validate();
    for (int p1 = 0, p2 = 0; p1 < firstNumValues || p2 < secondNumValues; ) {
      if (p1 >= firstNumValues)
        firstI = numAttributes;
      else
        firstI = first.index(p1); 
      if (p2 >= secondNumValues)
        secondI = numAttributes;
      else
        secondI = second.index(p2);
      if (firstI == classIndex) {
        p1++; 
        continue;
      }
      if ((firstI < numAttributes) && !m_ActiveIndices[firstI]) {
        p1++; 
        continue;
      }
      if (secondI == classIndex) {
        p2++; 
        continue;
      }
      if ((secondI < numAttributes) && !m_ActiveIndices[secondI]) {
        p2++;
        continue;
      }
      double diff;
      if (firstI == secondI) {
        diff = difference(firstI,
                  first.valueSparse(p1),
                  second.valueSparse(p2));
        p1++;
        p2++;
      }
      else if (firstI > secondI) {
        diff = difference(secondI, 
                  0, second.valueSparse(p2));
        p2++;
      }
      else {
        diff = difference(firstI, 
                  first.valueSparse(p1), 0);
        p1++;
      }
      if (stats != null)
        stats.incrCoordCount();
      distance = updateDistance(distance, diff);
      if (distance > cutOffValue)
        return Double.POSITIVE_INFINITY;
    }
    return distance;
  }

显示您可以分别处理各种维度(在 Weka 中称为属性)。因此,您可以为每个维度/属性定义不同的距离。

关于业务规则,以避免将某些实例聚集在一起。我认为您可以创建一个距离函数,该函数在不满足业务规则时返回Double.positiveInfinity

ELKI是另一种选择。它比Weka具有更多的聚类算法(Weka主要用于分类)。他们甚至有一个 Wiki 教程来解释如何实现自定义距离函数(然后你应该能够在分层聚类中使用):距离函数教程。

请注意,"业务规则"不是指定距离函数的常用方法...

最新更新