Example usage for org.apache.commons.math3.ml.distance ManhattanDistance ManhattanDistance

List of usage examples for org.apache.commons.math3.ml.distance ManhattanDistance ManhattanDistance

Introduction

In this page you can find the example usage for org.apache.commons.math3.ml.distance ManhattanDistance ManhattanDistance.

Prototype

ManhattanDistance

Source Link

Usage

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiClusterSilhouetteScore.java

@Override
public double score(double[][][] samples) {

    ManhattanDistance distance = new ManhattanDistance();
    double score = 0.0;
    int m = 0, i, j, k;

    for (i = 0; i < samples.length; i++)
        m += samples[i].length;//w  w  w  .  j  ava2 s  .  c  o  m

    byte[] clusterAssignment = new byte[m];
    double[][] values = new double[m][];

    double[][] distances = new double[m - 1][];
    for (i = 0; i < m - 1; i++)
        distances[i] = new double[m - i - 1];

    int mIndex = 0;

    for (byte label = 0; label < samples.length; label++)
        for (i = 0; i < samples[label].length; i++) {
            values[mIndex] = samples[label][i];
            clusterAssignment[mIndex] = label;
            for (j = 0; j < mIndex; j++)
                distances[mIndex - j - 1][j] = distance.compute(values[mIndex], values[mIndex - j - 1]);
            mIndex++;
        }

    double dissimilarity[] = new double[samples.length];
    double a, b;
    for (i = 0; i < m; i++) {
        Arrays.fill(dissimilarity, 0.0);
        for (j = 0; j < m; j++)
            if (i != j) {
                k = Math.min(i, j);
                dissimilarity[clusterAssignment[j]] += distances[k][Math.max(i, j) - 1 - k];
            }

        a = dissimilarity[clusterAssignment[i]] / (samples[clusterAssignment[i]].length - 1);
        b = Double.MAX_VALUE;
        for (j = 0; j < dissimilarity.length; j++)
            if (clusterAssignment[i] != j)
                b = Math.min(b, dissimilarity[j] / samples[j].length);

        score += Math.max(a, b) == 0.0 ? -1.0 : (b - a) / Math.max(a, b);

    }

    return score / m;

}

From source file:GeMSE.GS.Analysis.Clustering.DistanceMatrix.java

public double[][] GetDistanceMatrix(double[][] space, Metrics metric, ClusteringDomains clusteringDomain) {
    euclideanDistance = new EuclideanDistance();
    manhattanDistance = new ManhattanDistance();
    earthMoversDistance = new EarthMoversDistance();
    chebyshevDistance = new ChebyshevDistance();
    canberraDistance = new CanberraDistance();

    double[][] rtv = null;

    int rowCount = space.length;
    int colCount = space[0].length;
    int rtvIndex = 0;

    switch (clusteringDomain) {
    case Rows:/*from w  ww  .j  ava  2s .com*/
        rtv = new double[1][(int) Math.floor((Math.pow(rowCount, 2) - rowCount) / 2f)];

        for (int i = 0; i < rowCount - 1; i++)
            for (int j = i + 1; j < rowCount; j++)
                // TODO: replace the following mess with the factory method. 
                switch (metric) {
                case EuclideanDistance:
                    rtv[0][rtvIndex++] = euclideanDistance.compute(space[i], space[j]);
                    break;

                case ManhattanDistance:
                    rtv[0][rtvIndex++] = manhattanDistance.compute(space[i], space[j]);
                    break;

                case EarthMoversDistance:
                    rtv[0][rtvIndex++] = earthMoversDistance.compute(space[i], space[j]);
                    break;

                case ChebyshevDistance:
                    rtv[0][rtvIndex++] = chebyshevDistance.compute(space[i], space[j]);
                    break;

                case CanberraDistance:
                    rtv[0][rtvIndex++] = canberraDistance.compute(space[i], space[j]);
                    break;

                case PearsonCorrelationCoefficient:
                    rtv[0][rtvIndex++] = ComputePCC(space[i], space[j]);
                    break;
                }

        break;

    case Columns:
        rtv = new double[1][(int) Math.floor((Math.pow(colCount, 2) - colCount) / 2f)];

        double[] col_i = new double[rowCount];
        double[] col_j = new double[rowCount];

        for (int i = 0; i < colCount - 1; i++) {
            for (int j = i + 1; j < colCount; j++) {
                for (int row = 0; row < rowCount; row++) {
                    col_i[row] = space[row][i];
                    col_j[row] = space[row][j];
                }

                // TODO: Replace the following mess with the factory pattern. 
                switch (metric) {
                case EuclideanDistance:
                    rtv[0][rtvIndex++] = euclideanDistance.compute(col_i, col_j);
                    break;

                case ManhattanDistance:
                    rtv[0][rtvIndex++] = manhattanDistance.compute(col_i, col_j);
                    break;

                case EarthMoversDistance:
                    rtv[0][rtvIndex++] = earthMoversDistance.compute(col_i, col_j);
                    break;

                case ChebyshevDistance:
                    rtv[0][rtvIndex++] = chebyshevDistance.compute(col_i, col_j);
                    break;

                case CanberraDistance:
                    rtv[0][rtvIndex++] = canberraDistance.compute(col_i, col_j);
                    break;

                case PearsonCorrelationCoefficient:
                    rtv[0][rtvIndex++] = ComputePCC(col_i, col_j);
                    break;
                }
            }
        }
        break;
    }

    return rtv;
}

From source file:org.apache.solr.client.solrj.io.eval.DistanceEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    if (values.length == 2) {
        Object first = values[0];
        Object second = values[1];

        if (null == first) {
            throw new IOException(
                    String.format(Locale.ROOT, "Invalid expression %s - null found for the first value",
                            toExpression(constructingFactory)));
        }/* ww  w  .j  av  a  2s. c o  m*/
        if (null == second) {
            throw new IOException(
                    String.format(Locale.ROOT, "Invalid expression %s - null found for the second value",
                            toExpression(constructingFactory)));
        }
        if (!(first instanceof List<?>)) {
            throw new IOException(String.format(Locale.ROOT,
                    "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                    toExpression(constructingFactory), first.getClass().getSimpleName()));
        }
        if (!(second instanceof List<?>)) {
            throw new IOException(String.format(Locale.ROOT,
                    "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                    toExpression(constructingFactory), first.getClass().getSimpleName()));
        }

        if (type.equals(DistanceType.euclidean)) {
            EuclideanDistance euclideanDistance = new EuclideanDistance();
            return euclideanDistance.compute(
                    ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());
        } else if (type.equals(DistanceType.manhattan)) {
            ManhattanDistance manhattanDistance = new ManhattanDistance();
            return manhattanDistance.compute(
                    ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());

        } else if (type.equals(DistanceType.canberra)) {
            CanberraDistance canberraDistance = new CanberraDistance();
            return canberraDistance.compute(
                    ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());
        } else if (type.equals(DistanceType.earthMovers)) {
            EarthMoversDistance earthMoversDistance = new EarthMoversDistance();
            return earthMoversDistance.compute(
                    ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());
        } else {
            return null;
        }
    } else if (values.length == 1) {
        if (values[0] instanceof Matrix) {
            Matrix matrix = (Matrix) values[0];
            if (type.equals(DistanceType.euclidean)) {
                EuclideanDistance euclideanDistance = new EuclideanDistance();
                return distance(euclideanDistance, matrix);
            } else if (type.equals(DistanceType.canberra)) {
                CanberraDistance canberraDistance = new CanberraDistance();
                return distance(canberraDistance, matrix);
            } else if (type.equals(DistanceType.manhattan)) {
                ManhattanDistance manhattanDistance = new ManhattanDistance();
                return distance(manhattanDistance, matrix);
            } else if (type.equals(DistanceType.earthMovers)) {
                EarthMoversDistance earthMoversDistance = new EarthMoversDistance();
                return distance(earthMoversDistance, matrix);
            } else {
                return null;
            }
        } else {
            throw new IOException(
                    "distance function operates on either two numeric arrays or a single matrix as parameters.");
        }
    } else {
        throw new IOException(
                "distance function operates on either two numeric arrays or a single matrix as parameters.");
    }
}

From source file:org.apache.solr.client.solrj.io.eval.KnnEvaluator.java

public KnnEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
    super(expression, factory);

    DistanceEvaluator.DistanceType type = null;
    List<StreamExpressionNamedParameter> namedParams = factory.getNamedOperands(expression);
    if (namedParams.size() > 0) {
        if (namedParams.size() > 1) {
            throw new IOException("distance function expects only one named parameter 'distance'.");
        }/* www.j a  va  2  s .c  o  m*/

        StreamExpressionNamedParameter namedParameter = namedParams.get(0);
        String name = namedParameter.getName();
        if (!name.equalsIgnoreCase("distance")) {
            throw new IOException("distance function expects only one named parameter 'distance'.");
        }

        String typeParam = namedParameter.getParameter().toString().trim();
        type = DistanceEvaluator.DistanceType.valueOf(typeParam);
    } else {
        type = DistanceEvaluator.DistanceType.euclidean;
    }

    if (type.equals(DistanceEvaluator.DistanceType.euclidean)) {
        distanceMeasure = new EuclideanDistance();
    } else if (type.equals(DistanceEvaluator.DistanceType.manhattan)) {
        distanceMeasure = new ManhattanDistance();
    } else if (type.equals(DistanceEvaluator.DistanceType.canberra)) {
        distanceMeasure = new CanberraDistance();
    } else if (type.equals(DistanceEvaluator.DistanceType.earthMovers)) {
        distanceMeasure = new EarthMoversDistance();
    }

}

From source file:org.apache.solr.client.solrj.io.eval.ManhattanDistanceEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }//from   w w  w.j  a  v a2s  .  co m
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    ManhattanDistance distance = new ManhattanDistance();
    return distance.compute(
            ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
            ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray());
}