Search in sources :

Example 81 with IntStream

use of java.util.stream.IntStream in project pyramid by cheng-li.

the class RegTreeTrainer method splitNode.

/**
 * xgboost monotonicity
 * split a splitable node
 * @param leafToSplit
 * @param regTreeConfig
 * @param dataSet
 */
private static void splitNode(RegressionTree tree, Node leafToSplit, RegTreeConfig regTreeConfig, DataSet dataSet, double[] labels, int[] monotonicity, LeafOutputCalculator leafOutputCalculator) {
    int numDataPoints = dataSet.getNumDataPoints();
    /**
     * split this leaf node
     */
    int featureIndex = leafToSplit.getFeatureIndex();
    double threshold = leafToSplit.getThreshold();
    Vector inputVector = dataSet.getColumn(featureIndex);
    Vector columnVector;
    if (inputVector.isDense()) {
        columnVector = inputVector;
    } else {
        columnVector = new DenseVector(inputVector);
    }
    /**
     * create children
     */
    Node leftChild = new Node();
    leftChild.setId(tree.numNodes);
    tree.numNodes += 1;
    Node rightChild = new Node();
    rightChild.setId(tree.numNodes);
    tree.numNodes += 1;
    double[] parentProbs = leafToSplit.getProbs();
    double[] leftProbs = new double[numDataPoints];
    double[] rightProbs = new double[numDataPoints];
    IntStream intStream = IntStream.range(0, numDataPoints);
    if (regTreeConfig.isParallel()) {
        intStream = intStream.parallel();
    }
    intStream.forEach(i -> {
        double featureValue = columnVector.get(i);
        if (Double.isNaN(featureValue)) {
            // go to both branches probabilistically
            leftProbs[i] = parentProbs[i] * leafToSplit.getLeftProb();
            rightProbs[i] = parentProbs[i] * leafToSplit.getRightProb();
        } else {
            // <= go left, > go right
            if (featureValue <= threshold) {
                leftProbs[i] = parentProbs[i];
                rightProbs[i] = 0;
            } else {
                leftProbs[i] = 0;
                rightProbs[i] = parentProbs[i];
            }
        }
    });
    leftChild.setProbs(leftProbs);
    rightChild.setProbs(rightProbs);
    // the last two leaves need not to be updated completely
    // as we don't need to split them later
    int maxNumLeaves = regTreeConfig.getMaxNumLeaves();
    if (tree.leaves.size() != maxNumLeaves - 1) {
        updateNode(leftChild, regTreeConfig, dataSet, labels, monotonicity);
        updateNode(rightChild, regTreeConfig, dataSet, labels, monotonicity);
    }
    /**
     * link left and right child to the parent
     */
    leafToSplit.setLeftChild(leftChild);
    leafToSplit.setRightChild(rightChild);
    /**
     * update leaves, remove the parent, and add children
     */
    leafToSplit.setLeaf(false);
    leafToSplit.clearProbs();
    tree.leaves.remove(leafToSplit);
    leftChild.setLeaf(true);
    rightChild.setLeaf(true);
    tree.leaves.add(leftChild);
    tree.leaves.add(rightChild);
    tree.allNodes.add(leftChild);
    tree.allNodes.add(rightChild);
    int mono = monotonicity[featureIndex];
    leafOutputCalculator.setParallel(regTreeConfig.isParallel());
    setLeafOutput(leftChild, leafOutputCalculator, labels);
    setLeafOutput(rightChild, leafOutputCalculator, labels);
    setBoundForChildren(leafToSplit, mono);
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) IntStream(java.util.stream.IntStream) DenseVector(org.apache.mahout.math.DenseVector)

Example 82 with IntStream

use of java.util.stream.IntStream in project druid by druid-io.

the class MaxSizeSplitHintSpecTest method testSplitSkippingEmptyInputs.

@Test
public void testSplitSkippingEmptyInputs() {
    final int nonEmptyInputSize = 3;
    final MaxSizeSplitHintSpec splitHintSpec = new MaxSizeSplitHintSpec(new HumanReadableBytes(10L), null);
    final Function<Integer, InputFileAttribute> inputAttributeExtractor = InputFileAttribute::new;
    final IntStream dataStream = IntStream.concat(IntStream.concat(IntStream.generate(() -> 0).limit(10), IntStream.generate(() -> nonEmptyInputSize).limit(10)), IntStream.generate(() -> 0).limit(10));
    final List<List<Integer>> splits = Lists.newArrayList(splitHintSpec.split(dataStream.iterator(), inputAttributeExtractor));
    Assert.assertEquals(4, splits.size());
    Assert.assertEquals(3, splits.get(0).size());
    Assert.assertEquals(3, splits.get(1).size());
    Assert.assertEquals(3, splits.get(2).size());
    Assert.assertEquals(1, splits.get(3).size());
}
Also used : List(java.util.List) HumanReadableBytes(org.apache.druid.java.util.common.HumanReadableBytes) IntStream(java.util.stream.IntStream) Test(org.junit.Test)

Example 83 with IntStream

use of java.util.stream.IntStream in project neo4j by neo4j.

the class GraphDatabaseSettingsTest method transactionSamplingCanBePercentageValues.

@Test
void transactionSamplingCanBePercentageValues() {
    IntStream range = IntStream.range(1, 101);
    range.forEach(percentage -> {
        Config config = Config.defaults(transaction_sampling_percentage, percentage);
        int configuredSampling = config.get(transaction_sampling_percentage);
        assertEquals(percentage, configuredSampling);
    });
    assertThrows(IllegalArgumentException.class, () -> Config.defaults(transaction_sampling_percentage, 0));
    assertThrows(IllegalArgumentException.class, () -> Config.defaults(transaction_sampling_percentage, 101));
    assertThrows(IllegalArgumentException.class, () -> Config.defaults(transaction_sampling_percentage, 10101));
}
Also used : IntStream(java.util.stream.IntStream) Test(org.junit.jupiter.api.Test)

Example 84 with IntStream

use of java.util.stream.IntStream in project guava by google.

the class Streams method concat.

/**
 * Returns an {@link IntStream} containing the elements of the first stream, followed by the
 * elements of the second stream, and so on.
 *
 * <p>This is equivalent to {@code Stream.of(streams).flatMapToInt(stream -> stream)}, but the
 * returned stream may perform better.
 *
 * @see IntStream#concat(IntStream, IntStream)
 */
public static IntStream concat(IntStream... streams) {
    boolean isParallel = false;
    int characteristics = Spliterator.ORDERED | Spliterator.SIZED | Spliterator.NONNULL;
    long estimatedSize = 0L;
    ImmutableList.Builder<Spliterator.OfInt> splitrsBuilder = new ImmutableList.Builder<>(streams.length);
    for (IntStream stream : streams) {
        isParallel |= stream.isParallel();
        Spliterator.OfInt splitr = stream.spliterator();
        splitrsBuilder.add(splitr);
        characteristics &= splitr.characteristics();
        estimatedSize = LongMath.saturatedAdd(estimatedSize, splitr.estimateSize());
    }
    return StreamSupport.intStream(CollectSpliterators.flatMapToInt(splitrsBuilder.build().spliterator(), splitr -> splitr, characteristics, estimatedSize), isParallel).onClose(() -> closeAll(streams));
}
Also used : IntStream(java.util.stream.IntStream) AbstractSpliterator(java.util.Spliterators.AbstractSpliterator) Spliterator(java.util.Spliterator)

Example 85 with IntStream

use of java.util.stream.IntStream in project waltz by khartec.

the class RandomUtilities_randomlySizedIntStreamTest method simpleRandomlySizedIntStream1.

@Test
public void simpleRandomlySizedIntStream1() {
    IntStream val = RandomUtilities.randomlySizedIntStream(0, 1);
    assertEquals(0, val.count());
}
Also used : IntStream(java.util.stream.IntStream) Test(org.junit.jupiter.api.Test)

Aggregations

IntStream (java.util.stream.IntStream)87 Test (org.junit.Test)20 List (java.util.List)16 ArrayList (java.util.ArrayList)13 Arrays (java.util.Arrays)10 Stream (java.util.stream.Stream)9 Test (org.junit.jupiter.api.Test)8 Random (java.util.Random)7 Collectors (java.util.stream.Collectors)7 DoubleStream (java.util.stream.DoubleStream)6 LongStream (java.util.stream.LongStream)6 Map (java.util.Map)5 Pattern (java.util.regex.Pattern)5 DecimalBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.decimalBox.definition.DecimalBoxFieldDefinition)5 MultipleSubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)5 SubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition)5 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)5 LayoutRow (org.uberfire.ext.layout.editor.api.editor.LayoutRow)5 LayoutTemplate (org.uberfire.ext.layout.editor.api.editor.LayoutTemplate)5 Assert (org.junit.Assert)4