Example usage for com.google.common.collect Iterables limit

List of usage examples for com.google.common.collect Iterables limit

Introduction

In this page you can find the example usage for com.google.common.collect Iterables limit.

Prototype

public static <T> Iterable<T> limit(final Iterable<T> iterable, final int limitSize) 

Source Link

Document

Creates an iterable with the first limitSize elements of the given iterable.

Usage

From source file:com.technobium.MultinomialLogisticRegression.java

public static void main(String[] args) throws Exception {
    // this test trains a 3-way classifier on the famous Iris dataset.
    // a similar exercise can be accomplished in R using this code:
    //    library(nnet)
    //    correct = rep(0,100)
    //    for (j in 1:100) {
    //      i = order(runif(150))
    //      train = iris[i[1:100],]
    //      test = iris[i[101:150],]
    //      m = multinom(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, train)
    //      correct[j] = mean(predict(m, newdata=test) == test$Species)
    //    }//ww  w.  ja va 2 s.  c  om
    //    hist(correct)
    //
    // Note that depending on the training/test split, performance can be better or worse.
    // There is about a 5% chance of getting accuracy < 90% and about 20% chance of getting accuracy
    // of 100%
    //
    // This test uses a deterministic split that is neither outstandingly good nor bad

    RandomUtils.useTestSeed();
    Splitter onComma = Splitter.on(",");

    // read the data
    List<String> raw = Resources.readLines(Resources.getResource("iris.csv"), Charsets.UTF_8);

    // holds features
    List<Vector> data = Lists.newArrayList();

    // holds target variable
    List<Integer> target = Lists.newArrayList();

    // for decoding target values
    Dictionary dict = new Dictionary();

    // for permuting data later
    List<Integer> order = Lists.newArrayList();

    for (String line : raw.subList(1, raw.size())) {
        // order gets a list of indexes
        order.add(order.size());

        // parse the predictor variables
        Vector v = new DenseVector(5);
        v.set(0, 1);
        int i = 1;
        Iterable<String> values = onComma.split(line);
        for (String value : Iterables.limit(values, 4)) {
            v.set(i++, Double.parseDouble(value));
        }
        data.add(v);

        // and the target
        target.add(dict.intern(Iterables.get(values, 4)));
    }

    // randomize the order ... original data has each species all together
    // note that this randomization is deterministic
    Random random = RandomUtils.getRandom();
    Collections.shuffle(order, random);

    // select training and test data
    List<Integer> train = order.subList(0, 100);
    List<Integer> test = order.subList(100, 150);
    logger.warn("Training set = {}", train);
    logger.warn("Test set = {}", test);

    // now train many times and collect information on accuracy each time
    int[] correct = new int[test.size() + 1];
    for (int run = 0; run < 200; run++) {
        OnlineLogisticRegression lr = new OnlineLogisticRegression(3, 5, new L2(1));
        // 30 training passes should converge to > 95% accuracy nearly always but never to 100%
        for (int pass = 0; pass < 30; pass++) {
            Collections.shuffle(train, random);
            for (int k : train) {
                lr.train(target.get(k), data.get(k));
            }
        }

        // check the accuracy on held out data
        int x = 0;
        int[] count = new int[3];
        for (Integer k : test) {
            Vector vt = lr.classifyFull(data.get(k));
            int r = vt.maxValueIndex();
            count[r]++;
            x += r == target.get(k) ? 1 : 0;
        }
        correct[x]++;

        if (run == 199) {

            Vector v = new DenseVector(5);
            v.set(0, 1);
            int i = 1;
            Iterable<String> values = onComma.split("6.0,2.7,5.1,1.6,versicolor");
            for (String value : Iterables.limit(values, 4)) {
                v.set(i++, Double.parseDouble(value));
            }

            Vector vt = lr.classifyFull(v);
            for (String value : dict.values()) {
                System.out.println("target:" + value);
            }
            int t = dict.intern(Iterables.get(values, 4));

            int r = vt.maxValueIndex();
            boolean flag = r == t;
            lr.close();

            Closer closer = Closer.create();

            try {
                FileOutputStream byteArrayOutputStream = closer
                        .register(new FileOutputStream(new File("model.txt")));
                DataOutputStream dataOutputStream = closer
                        .register(new DataOutputStream(byteArrayOutputStream));
                PolymorphicWritable.write(dataOutputStream, lr);
            } finally {
                closer.close();
            }
        }
    }

    // verify we never saw worse than 95% correct,
    for (int i = 0; i < Math.floor(0.95 * test.size()); i++) {
        System.out.println(String.format("%d trials had unacceptable accuracy of only %.0f%%: ", correct[i],
                100.0 * i / test.size()));
    }
    // nor perfect
    System.out.println(String.format("%d trials had unrealistic accuracy of 100%%", correct[test.size() - 1]));
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.SqlUtil.java

public static String getQs(int num) {
    return Joiner.on(", ").join(Iterables.limit(Iterables.cycle("?"), num));
}

From source file:org.jbb.lib.logging.health.LogbackStateStorage.java

public static List<Status> getLastStatuses() {
    return Lists.newLinkedList(Iterables.limit(STATUSES.descendingMap().values(), TAIL_SIZE));
}

From source file:me.taylorkelly.mywarp.util.IterableUtils.java

/**
 * Returns whether the given Iterable contains has least the given number of entries.
 *
 * @param <T>      the type of entries
 * @param iterable the iterable to check
 * @param count    the number of entries the iterable should have at least
 * @return true if the given Iterable has at least the given number of entries
 *//*w  ww .j  a v  a  2s. c  om*/
public static <T> boolean atLeast(Iterable<T> iterable, int count) {
    return Iterables.size(Iterables.limit(iterable, count)) == count;
}

From source file:org.apache.james.transport.mailets.remote.delivery.Repeat.java

public static <T> List<T> repeat(T element, int times) {
    Preconditions.checkArgument(times >= 0, "Times argument should be strictly positive");
    return ImmutableList.copyOf(Iterables.limit(Iterables.cycle(element), times));
}

From source file:org.apache.james.transport.mailets.remoteDelivery.Repeat.java

@SuppressWarnings("unchecked")
public static <T> List<T> repeat(T element, int times) {
    Preconditions.checkArgument(times >= 0, "Times argument should be strictly positive");
    return ImmutableList.copyOf(Iterables.limit(Iterables.cycle(element), times));
}

From source file:io.prestosql.tests.utils.QueryAssertions.java

public static void assertContains(QueryResult all, QueryResult expectedSubset) {
    for (Object row : expectedSubset.rows()) {
        if (!all.rows().contains(row)) {
            fail(format("expected row missing: %s\nAll %s rows:\n    %s\nExpected subset %s rows:\n    %s\n",
                    row, all.getRowsCount(), Joiner.on("\n    ").join(Iterables.limit(all.rows(), 100)),
                    expectedSubset.getRowsCount(),
                    Joiner.on("\n    ").join(Iterables.limit(expectedSubset.rows(), 100))));
        }//w  ww  . j a v  a  2s  .  c  om
    }
}

From source file:com.github.jasonruckman.sidney.generator.Generator.java

public final Iterable<T> stream(int count) {
    return Iterables.limit(stream(), count);
}

From source file:org.onos.yangtools.yang.data.api.schema.tree.StoreTreeNodes.java

public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
    T current = tree;//ww  w  .  j  a  v a2  s .c  o  m

    int i = 1;
    for (PathArgument pathArg : path.getPathArguments()) {
        Optional<T> potential = current.getChild(pathArg);
        if (!potential.isPresent()) {
            throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
                    Iterables.toString(Iterables.limit(path.getPathArguments(), i))));
        }
        current = potential.get();
        ++i;
    }
    return current;
}

From source file:com.android.tools.idea.npw.importing.ImportUIUtil.java

private static String atMostTwo(Iterable<String> names, int size) {
    return Joiner.on(", ").join(Iterables.limit(names, Math.min(size - 1, 2)));
}