Search in sources :

Example 36 with IntStream

use of java.util.stream.IntStream in project flink by apache.

the class DescriptorProperties method extractMaxIndex.

private int extractMaxIndex(String key, String suffixPattern) {
    // extract index and property keys
    final String escapedKey = Pattern.quote(key);
    final Pattern pattern = Pattern.compile(escapedKey + "\\.(\\d+)" + suffixPattern);
    final IntStream indexes = properties.keySet().stream().flatMapToInt(k -> {
        final Matcher matcher = pattern.matcher(k);
        if (matcher.find()) {
            return IntStream.of(Integer.valueOf(matcher.group(1)));
        }
        return IntStream.empty();
    });
    // determine max index
    return indexes.max().orElse(-1);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) IntStream(java.util.stream.IntStream)

Example 37 with IntStream

use of java.util.stream.IntStream in project flink by apache.

the class CatalogPropertiesUtil method getCount.

/**
 * Extracts the property count under the given key and suffix.
 *
 * <p>For example:
 *
 * <pre>
 *     schema.0.name, schema.1.name -> 2
 * </pre>
 */
private static int getCount(Map<String, String> map, String key, String suffix) {
    final String escapedKey = Pattern.quote(key);
    final String escapedSuffix = Pattern.quote(suffix);
    final String escapedSeparator = Pattern.quote(SEPARATOR);
    final Pattern pattern = Pattern.compile(escapedKey + escapedSeparator + "(\\d+)" + escapedSeparator + escapedSuffix);
    final IntStream indexes = map.keySet().stream().flatMapToInt(k -> {
        final Matcher matcher = pattern.matcher(k);
        if (matcher.find()) {
            return IntStream.of(Integer.parseInt(matcher.group(1)));
        }
        return IntStream.empty();
    });
    return indexes.max().orElse(-1) + 1;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) IntStream(java.util.stream.IntStream)

Example 38 with IntStream

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

the class LKBOutputCalculator method getLeafOutput.

@Override
public double getLeafOutput(double[] probabilities, double[] labels) {
    double numerator = 0;
    double denominator = 0;
    IntStream intStream = IntStream.range(0, probabilities.length);
    if (parallel) {
        intStream = intStream.parallel();
    }
    numerator = intStream.mapToDouble(i -> labels[i] * probabilities[i]).sum();
    IntStream intStream2 = IntStream.range(0, probabilities.length);
    if (parallel) {
        intStream2 = intStream2.parallel();
    }
    denominator = intStream2.mapToDouble(i -> Math.abs(labels[i]) * (1 - Math.abs(labels[i])) * probabilities[i]).sum();
    // for (int i=0;i<probabilities.length;i++) {
    // double label = labels[i];
    // numerator += label*probabilities[i];
    // denominator += Math.abs(label) * (1 - Math.abs(label))*probabilities[i];
    // }
    double out;
    if (denominator == 0) {
        out = 0;
    } else {
        out = ((numClasses - 1) * numerator) / (numClasses * denominator);
    }
    // todo does the threshold matter?
    if (out > 1) {
        out = 1;
    }
    if (out < -1) {
        out = -1;
    }
    if (Double.isNaN(out)) {
        throw new RuntimeException("leaf value is NaN");
    }
    if (Double.isInfinite(out)) {
        throw new RuntimeException("leaf value is Infinite");
    }
    return out;
}
Also used : IntStream(java.util.stream.IntStream)

Example 39 with IntStream

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

the class LogisticLoss method updatePredictedCounts.

private void updatePredictedCounts() {
    StopWatch stopWatch = new StopWatch();
    if (logger.isDebugEnabled()) {
        stopWatch.start();
    }
    IntStream intStream;
    if (isParallel) {
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(i -> this.predictedCounts.set(i, calPredictedCount(i)));
    if (logger.isDebugEnabled()) {
        logger.debug("time spent on updatePredictedCounts = " + stopWatch);
    }
}
Also used : IntStream(java.util.stream.IntStream) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 40 with IntStream

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

the class LogisticLoss method updateEmpricalCounts.

// todo removed isParallel
private void updateEmpricalCounts() {
    IntStream intStream;
    if (isParallel) {
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(i -> this.empiricalCounts.set(i, calEmpricalCount(i)));
}
Also used : IntStream(java.util.stream.IntStream)

Aggregations

IntStream (java.util.stream.IntStream)80 Test (org.junit.Test)20 List (java.util.List)15 ArrayList (java.util.ArrayList)13 Arrays (java.util.Arrays)10 Stream (java.util.stream.Stream)9 Random (java.util.Random)7 Collectors (java.util.stream.Collectors)7 Map (java.util.Map)5 Pattern (java.util.regex.Pattern)5 DoubleStream (java.util.stream.DoubleStream)5 LongStream (java.util.stream.LongStream)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 Test (org.junit.jupiter.api.Test)4