Example usage for org.apache.commons.math3.util Pair Pair

List of usage examples for org.apache.commons.math3.util Pair Pair

Introduction

In this page you can find the example usage for org.apache.commons.math3.util Pair Pair.

Prototype

public Pair(K k, V v) 

Source Link

Document

Create an entry representing a mapping from the specified key to the specified value.

Usage

From source file:com.insightml.evaluation.functions.Gini.java

private static <T> double gini(final T[] preds, final Object[] expected, final boolean doPerfect) {
    final List<Pair<Object, T>> sortedByPdesc = new LinkedList<>();
    for (int i = 0; i < preds.length; ++i) {
        sortedByPdesc.add(new Pair<>(expected[i], preds[i]));
    }/*from  w ww  .  j av  a 2 s. c  o m*/
    final boolean isBinary = sortedByPdesc.get(0).getSecond() instanceof Boolean;
    Collections.sort(sortedByPdesc, (o1, o2) -> {
        if (doPerfect) {
            if (isBinary) {
                return ((Boolean) o1.getFirst()).booleanValue() ? -1 : 1;
            }
            return (Double) o1.getFirst() >= (Double) o2.getFirst() ? -1 : 1;
        }
        return (Double) o1.getSecond() >= (Double) o2.getSecond() ? -1 : 1;
    });

    double sum = 0;
    double giniSum = 0;
    for (final Pair<Object, T> prediction : sortedByPdesc) {
        if (isBinary) {
            sum += (Boolean) prediction.getFirst() ? 1 : 0;
        } else {
            sum += (Double) prediction.getFirst();
        }
        giniSum += sum;
    }

    giniSum = giniSum / sum - (sortedByPdesc.size() + 1.0) / 2;
    return giniSum / sortedByPdesc.size();
}

From source file:com.cloudera.oryx.common.math.DummyVisitor.java

@Override
public void visit(int index, double value) {
    seenValues.add(new Pair<Integer, Double>(index, value));
}

From source file:com.insightml.evaluation.simulation.SplitSimulation.java

public static <S extends Sample> Pair<Iterable<S>, List<S>> split(final Iterable<S> instances,
        final double trainFraction, final Random random) {
    if (trainFraction == 1.0) {
        return new Pair<>(instances, null);
    }//ww  w.j ava  2s.co m
    final List<S> train = new LinkedList<>();
    final List<S> test = new LinkedList<>();
    for (final S sample : instances) {
        if (random.nextDouble() < trainFraction) {
            train.add(sample);
        } else {
            test.add(sample);
        }
    }
    return new Pair<>(train, test);
}

From source file:com.insightml.models.AbstractBasicDoubleLearner.java

private static Pair<double[], double[][]> filter(final LearnerInput<? extends Sample, ? extends Double> input) {
    final double[][] features = input.getTrain().features();
    final Double[] expected = input.getTrain().expected(input.labelIndex);

    final DoubleArray expFiltered = new DoubleArray(expected.length);
    final List<double[]> featsFiltered = new LinkedList<>();
    for (int i = 0; i < expected.length; ++i) {
        if (expected[i] != null) {
            expFiltered.add(expected[i].doubleValue());
            featsFiltered.add(features[i]);
        }/*  w  w  w  . j  ava2s  .c o  m*/
    }
    final double[][] featsArray = Arrays.of(featsFiltered, double[].class);
    return new Pair<>(expFiltered.toArray(), featsArray);
}

From source file:com.insightml.models.optimization.LinearModelBlenderTest.java

@Override
protected Pair<? extends ILearner<Sample, ? super Double, Double>, Double> getNumeric() {
    return new Pair(new LinearModelBlender(true, new double[1][2], new RMSE(), new OLS()), -0.75341);
}

From source file:com.insightml.models.optimization.LinearModelBlenderTest.java

@Override
protected Pair getBoolean() {
    return new Pair<>(new LinearModelBlender(true, new double[1][2], new LogLoss(false), new OLS()), -0.03123);
}

From source file:io.druid.benchmark.datagen.SequentialDistribution.java

public SequentialDistribution(Integer start, Integer end, List<Object> enumeratedValues) {
    // just pass in some bogus probability mass function, we won't be using it
    super(Arrays.asList(new Pair<Object, Double>(null, 1.0)));
    this.start = start;
    this.end = end;
    this.enumeratedValues = enumeratedValues;
    if (enumeratedValues == null) {
        counter = start;/*from  w  w w. j  a va 2  s  .com*/
    } else {
        counter = 0;
    }
}

From source file:it.unibo.alchemist.SupportedIncarnations.java

/**
 * Fetches an incarnation whose name matches the supplied string.
 * /*  w  w  w  . j  a  v a 2  s  . com*/
 * @param s
 *            the name of the {@link Incarnation}
 * @param <T>
 *            {@link Concentration} type
 * @return an {@link Optional} containing the incarnation, if one with a
 *         matching name exists
 */
@SuppressWarnings("unchecked")
public static <T> Optional<Incarnation<T>> get(final String s) {
    final String cmp = preprocess(s);
    return INCARNATIONS.stream()
            .map(clazz -> new Pair<>(METRIC.distance(preprocess(clazz.getSimpleName()), cmp), clazz))
            .min((p1, p2) -> Double.compare(p1.getFirst(), p2.getFirst())).map(Pair::getSecond)
            .flatMap(clazz -> {
                try {
                    return Optional.of(clazz.newInstance());
                } catch (Exception e) {
                    L.error("Unable to instance incarnation " + clazz + " (closest match to " + s + " among "
                            + INCARNATIONS + ")", e);
                    return Optional.empty();
                }
            });
}

From source file:it.unibo.alchemist.language.protelis.datatype.FieldTroveMapImpl.java

@Override
public void addSample(final DeviceUID n, final Object v) {
    fld.put(n.getId(), new Pair<>(n, v));
}

From source file:it.unibo.alchemist.language.protelis.datatype.AbstractField.java

@Override
public Pair<DeviceUID, Object> reducePairs(final BinaryOperator<Pair<DeviceUID, Object>> accumulator,
        final DeviceUID exclude) {
    return reduce(coupleIterator(), accumulator,
            exclude == null ? null : new Pair<>(exclude, getSample(exclude)), null);
}