List of usage examples for org.apache.commons.math3.util Pair getFirst
public K getFirst()
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])); }/* w ww. j a va2 s.c om*/ 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.als.serving.web.RecommendToAnonymousServlet.java
static Pair<String[], float[]> parseItemValuePairs(Iterator<String> pathComponents) { List<Pair<String, Float>> itemValuePairs = Lists.newArrayListWithCapacity(1); while (pathComponents.hasNext()) { itemValuePairs.add(parseItemValue(pathComponents.next())); }//from w ww.ja v a 2 s. com int size = itemValuePairs.size(); String[] itemIDs = new String[size]; float[] values = new float[size]; for (int i = 0; i < size; i++) { Pair<String, Float> itemValuePair = itemValuePairs.get(i); itemIDs[i] = itemValuePair.getFirst(); Float value = itemValuePair.getSecond(); values[i] = value == null ? 1.0f : value; } return new Pair<String[], float[]>(itemIDs, values); }
From source file:net.myrrix.web.servlets.RecommendToAnonymousServlet.java
static Pair<long[], float[]> parseItemValuePairs(Iterator<String> pathComponents) { List<Pair<Long, Float>> itemValuePairs = Lists.newArrayListWithCapacity(1); while (pathComponents.hasNext()) { itemValuePairs.add(parseItemValue(pathComponents.next())); }// ww w . jav a 2 s. com int size = itemValuePairs.size(); long[] itemIDs = new long[size]; float[] values = new float[size]; for (int i = 0; i < size; i++) { Pair<Long, Float> itemValuePair = itemValuePairs.get(i); itemIDs[i] = itemValuePair.getFirst(); Float value = itemValuePair.getSecond(); values[i] = value == null ? 1.0f : value; } return new Pair<long[], float[]>(itemIDs, values); }
From source file:com.insightml.utils.ui.UiUtils.java
public static String format(final PairList<?, ?> list) { final StringBuilder builder = new StringBuilder(); if (list == null) { return null; }//from w w w . j av a2 s. c o m for (final Pair<?, ?> entry : list) { builder.append(fill(format(entry.getFirst()), 45) + " "); builder.append(format(entry.getSecond())); builder.append('\n'); } return builder.toString(); }
From source file:it.unibo.alchemist.language.protelis.util.ReflectionUtils.java
private static Method loadBestMethod(final Class<?> clazz, final String methodName, final Class<?>[] argClass) { Objects.requireNonNull(clazz, "The class on which the method will be invoked can not be null."); Objects.requireNonNull(methodName, "Method name can not be null."); Objects.requireNonNull(argClass, "Method arguments can not be null."); /*//from w w w . j a v a 2 s . c o m * If there is a matching method, return it */ try { return clazz.getMethod(methodName, argClass); } catch (NoSuchMethodException | SecurityException e) { /* * Look it up on the cache */ /* * Deal with Java method overloading scoring methods */ final Method[] candidates = clazz.getMethods(); final List<Pair<Integer, Method>> lm = new ArrayList<>(candidates.length); for (final Method m : candidates) { // TODO: Workaround for different Method API if (m.getParameterTypes().length == argClass.length && methodName.equals(m.getName())) { final Class<?>[] params = m.getParameterTypes(); int p = 0; boolean compatible = true; for (int i = 0; compatible && i < argClass.length; i++) { final Class<?> expected = params[i]; if (expected.isAssignableFrom(argClass[i])) { /* * No downcast required, there is compatibility */ p++; } else if (!PrimitiveUtils.classIsNumber(expected)) { compatible = false; } } if (compatible) { lm.add(new Pair<>(p, m)); } } } /* * Find best */ if (lm.size() > 0) { Pair<Integer, Method> max = lm.get(0); for (Pair<Integer, Method> cPair : lm) { if (cPair.getFirst().compareTo(max.getFirst()) > 0) { max = cPair; } } return max.getSecond(); } } List<Class<?>> list = new ArrayList<>(); for (Class<?> c : argClass) { list.add(c); } final String argType = list.toString(); throw new NoSuchMethodError( methodName + "/" + argClass.length + argType + " does not exist in " + clazz + "."); }
From source file:net.myrrix.online.som.SelfOrganizingMaps.java
private static void sortMembers(Node[][] map) { for (Node[] mapRow : map) { for (Node node : mapRow) { Collections.sort(node.getAssignedIDs(), new Comparator<Pair<Double, Long>>() { @Override//from www . j a va 2s.c om public int compare(Pair<Double, Long> a, Pair<Double, Long> b) { if (a.getFirst() > b.getFirst()) { return -1; } if (a.getFirst() < b.getFirst()) { return 1; } return 0; } }); } } }
From source file:com.insightml.AbstractModelTest.java
private static void test(final Pair<? extends ILearner, Double> tuple, final IDataset<SimpleSample, Double, ?> instances, final ObjectiveFunction<? super SimpleSample, ? super Serializable> objective) { if (tuple != null) { final double expected = tuple.getSecond(); Assert.assertTrue(expected + " > -0.82", expected > -0.82); Tests.testLearner(tuple.getFirst(), instances, objective, expected); }//from w w w.j a va 2s . co m }
From source file:com.insightml.utils.io.IoUtils.java
public static void zip2(final List<Pair<InputStream, String>> files, final File target) { final byte[] buffer = new byte[1024]; try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target))) { for (final Pair<InputStream, String> file : files) { final ZipEntry ze = new ZipEntry(file.getSecond()); zos.putNextEntry(ze);/* w w w . j av a 2s .com*/ int len; while ((len = file.getFirst().read(buffer)) > 0) { zos.write(buffer, 0, len); } file.getFirst().close(); zos.closeEntry(); } } catch (final IOException e) { throw new IllegalStateException(e); } }
From source file:com.cloudera.oryx.rdf.common.tree.DecisionTree.java
private static TreeNode build(ExampleSet examples, int buildAtDepth, int minNodeSize, double minInfoGainNats, int featuresToTry, int numFeatures, int suggestedMaxSplitCandidates, int maxDepth, RandomGenerator random) {/* w w w .j a v a 2s . com*/ if (buildAtDepth >= maxDepth - 1 || examples.getExamples().size() < minNodeSize) { return new TerminalNode(Prediction.buildPrediction(examples)); } double bestGain = Double.NEGATIVE_INFINITY; Decision bestDecision = null; for (int featureNumber : randomFeatures(examples, featuresToTry, numFeatures, random)) { Iterable<Decision> decisions = Decision.decisionsFromExamples(examples, featureNumber, suggestedMaxSplitCandidates); Pair<Decision, Double> decisionAndGain = Information.bestGain(decisions, examples); if (decisionAndGain != null) { double gain = decisionAndGain.getSecond(); if (gain > bestGain) { bestGain = gain; bestDecision = decisionAndGain.getFirst(); } } } if (Double.isNaN(bestGain) || bestGain < minInfoGainNats) { return new TerminalNode(Prediction.buildPrediction(examples)); } bestDecision.setInformationGain(bestGain); ExampleSet[] negPosSplit = examples.split(bestDecision); examples = null; // For GC? TreeNode left = build(negPosSplit[0], buildAtDepth + 1, minNodeSize, minInfoGainNats, featuresToTry, numFeatures, suggestedMaxSplitCandidates, maxDepth, random); TreeNode right = build(negPosSplit[1], buildAtDepth + 1, minNodeSize, minInfoGainNats, featuresToTry, numFeatures, suggestedMaxSplitCandidates, maxDepth, random); return new DecisionNode(bestDecision, left, right); }
From source file:edu.unc.cs.gamma.rvo.KdTree.java
/** * Recursive method for building an obstacle k-D tree. * * @param obstacles A list of obstacles. * @return An obstacle k-D tree node.// w ww . j a va 2 s . c om */ private static ObstacleTreeNode buildObstacleTreeRecursive(List<Obstacle> obstacles) { if (obstacles.isEmpty()) { return null; } final ObstacleTreeNode node = new ObstacleTreeNode(); int optimalSplit = 0; int minLeft = obstacles.size(); int minRight = obstacles.size(); for (int i = 0; i < obstacles.size(); i++) { int leftSize = 0; int rightSize = 0; final Obstacle obstacleI1 = obstacles.get(i); Obstacle obstacleI2 = obstacleI1.next; // Compute optimal split node. for (int j = 0; j < obstacles.size(); j++) { if (i == j) { continue; } final Obstacle obstacleJ1 = obstacles.get(j); final Obstacle obstacleJ2 = obstacleJ1.next; final double j1LeftOfI = RVOMath.leftOf(obstacleI1.point, obstacleI2.point, obstacleJ1.point); final double j2LeftOfI = RVOMath.leftOf(obstacleI1.point, obstacleI2.point, obstacleJ2.point); if (j1LeftOfI >= -RVOMath.EPSILON && j2LeftOfI >= -RVOMath.EPSILON) { leftSize++; } else if (j1LeftOfI <= RVOMath.EPSILON && j2LeftOfI <= RVOMath.EPSILON) { rightSize++; } else { leftSize++; rightSize++; } final Pair<Integer, Integer> pair1 = new Pair<>(FastMath.max(leftSize, rightSize), FastMath.min(leftSize, rightSize)); final Pair<Integer, Integer> pair2 = new Pair<>(FastMath.max(minLeft, minRight), FastMath.min(minLeft, minRight)); if (!(pair1.getFirst() < pair2.getFirst() || pair1.getFirst() <= pair2.getFirst() && pair1.getSecond() < pair2.getSecond())) { break; } } final Pair<Integer, Integer> pair1 = new Pair<>(FastMath.max(leftSize, rightSize), FastMath.min(leftSize, rightSize)); final Pair<Integer, Integer> pair2 = new Pair<>(FastMath.max(minLeft, minRight), FastMath.min(minLeft, minRight)); if (pair1.getFirst() < pair2.getFirst() || pair1.getFirst() <= pair2.getFirst() && pair1.getSecond() < pair2.getSecond()) { minLeft = leftSize; minRight = rightSize; optimalSplit = i; } } // Build split node. final List<Obstacle> leftObstacles = new ArrayList<>(minLeft); for (int n = 0; n < minLeft; n++) { leftObstacles.add(null); } final List<Obstacle> rightObstacles = new ArrayList<>(minRight); for (int n = 0; n < minRight; n++) { rightObstacles.add(null); } int leftCounter = 0; int rightCounter = 0; final Obstacle obstacleI1 = obstacles.get(optimalSplit); final Obstacle obstacleI2 = obstacleI1.next; for (int j = 0; j < obstacles.size(); j++) { if (optimalSplit == j) { continue; } final Obstacle obstacleJ1 = obstacles.get(j); final Obstacle obstacleJ2 = obstacleJ1.next; final double j1LeftOfI = RVOMath.leftOf(obstacleI1.point, obstacleI2.point, obstacleJ1.point); final double j2LeftOfI = RVOMath.leftOf(obstacleI1.point, obstacleI2.point, obstacleJ2.point); if (j1LeftOfI >= -RVOMath.EPSILON && j2LeftOfI >= -RVOMath.EPSILON) { leftObstacles.set(leftCounter++, obstacles.get(j)); } else if (j1LeftOfI <= RVOMath.EPSILON && j2LeftOfI <= RVOMath.EPSILON) { rightObstacles.set(rightCounter++, obstacles.get(j)); } else { // Split obstacle j. final double t = RVOMath.det(obstacleI2.point.subtract(obstacleI1.point), obstacleJ1.point.subtract(obstacleI1.point)) / RVOMath.det(obstacleI2.point.subtract(obstacleI1.point), obstacleJ1.point.subtract(obstacleJ2.point)); final Vector2D splitPoint = obstacleJ1.point .add(obstacleJ2.point.subtract(obstacleJ1.point).scalarMultiply(t)); final Obstacle newObstacle = new Obstacle(); newObstacle.point = splitPoint; newObstacle.previous = obstacleJ1; newObstacle.next = obstacleJ2; newObstacle.convex = true; newObstacle.direction = obstacleJ1.direction; newObstacle.id = Simulator.instance.obstacles.size(); Simulator.instance.obstacles.add(newObstacle); obstacleJ1.next = newObstacle; obstacleJ2.previous = newObstacle; if (j1LeftOfI > 0.0) { leftObstacles.set(leftCounter++, obstacleJ1); rightObstacles.set(rightCounter++, newObstacle); } else { rightObstacles.set(rightCounter++, obstacleJ1); leftObstacles.set(leftCounter++, newObstacle); } } } node.obstacle = obstacleI1; node.left = buildObstacleTreeRecursive(leftObstacles); node.right = buildObstacleTreeRecursive(rightObstacles); return node; }