Exemple #1
0
 def __call__(self, state, scope, paramTypes, datum, clusters, *args):
     if len(clusters) == 0:
         raise PFARuntimeException("no clusters")
     if len(args) == 1:
         metric, = args
         distances = [callfcn(state, scope, metric, [datum, x["center"]]) for x in clusters]
     else:
         distances = [sum((di - xi)**2 for di, xi in zip(datum, x["center"])) for x in clusters]
     index, = argLowestN(distances, 1, lambda a, b: a < b)
     return clusters[index]
Exemple #2
0
 def __call__(self, state, scope, paramTypes, n, datum, clusters, *args):
     if n < 0:
         raise PFARuntimeException("n must be nonnegative")
     if len(args) == 1:
         metric, = args
         distances = [callfcn(state, scope, metric, [datum, x["center"]]) for x in clusters]
     else:
         distances = [sum((di - xi)**2 for di, xi in zip(datum, x["center"])) for x in clusters]
     indexes = argLowestN(distances, n, lambda a, b: a < b)
     return [clusters[i] for i in indexes]
Exemple #3
0
    def __call__(self, state, scope, paramTypes, k, datum, codebook, *args):
        if k < 0:
            raise PFARuntimeException("k must be nonnegative")

        if len(args) == 1:
            metric, = args
            distances = [callfcn(state, scope, metric, [datum, x]) for x in codebook]
        else:
            if len(codebook) == 0:
                return []
            else:
                dimensions = len(datum)
                for x in codebook:
                    if len(x) != dimensions:
                        raise PFARuntimeException("inconsistent dimensionality")
            distances = [sum((di - xi)**2 for di, xi in zip(datum, x)) for x in codebook]

        indexes = argLowestN(distances, k, lambda a, b: a < b)
        return [codebook[i] for i in indexes]