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);
}
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());
}
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));
}
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));
}
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());
}
Aggregations