Search in sources :

Example 26 with RandomCutForest

use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.

the class RandomCutForestMapperTest method compactForestProvider.

private static Stream<RandomCutForest> compactForestProvider() {
    RandomCutForest.Builder<?> builder = RandomCutForest.builder().compact(true).dimensions(dimensions).sampleSize(sampleSize);
    RandomCutForest cachedDouble = builder.boundingBoxCacheFraction(new Random().nextDouble()).precision(Precision.FLOAT_64).build();
    RandomCutForest cachedFloat = builder.boundingBoxCacheFraction(new Random().nextDouble()).precision(Precision.FLOAT_32).build();
    RandomCutForest uncachedDouble = builder.boundingBoxCacheFraction(0.0).precision(Precision.FLOAT_64).build();
    RandomCutForest uncachedFloat = builder.boundingBoxCacheFraction(0.0).precision(Precision.FLOAT_32).build();
    return Stream.of(cachedDouble, cachedFloat, uncachedDouble, uncachedFloat);
}
Also used : Random(java.util.Random) RandomCutForest(com.amazon.randomcutforest.RandomCutForest)

Example 27 with RandomCutForest

use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.

the class RandomCutForestMapperTest method testRoundTripForEmptyForest.

@Test
public void testRoundTripForEmptyForest() {
    Precision precision = Precision.FLOAT_64;
    RandomCutForest forest = RandomCutForest.builder().compact(true).dimensions(dimensions).sampleSize(sampleSize).precision(precision).numberOfTrees(1).build();
    mapper.setSaveTreeStateEnabled(true);
    RandomCutForest forest2 = mapper.toModel(mapper.toState(forest));
    assertCompactForestEquals(forest, forest2);
}
Also used : Precision(com.amazon.randomcutforest.config.Precision) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with RandomCutForest

use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.

the class AnomalyAttributionRunnerTest method testAnomalyAttributionTransformer.

@Test
public void testAnomalyAttributionTransformer() {
    RandomCutForest forest = mock(RandomCutForest.class);
    when(forest.getDimensions()).thenReturn(2);
    AnomalyAttributionRunner.AnomalyAttributionTransformer transformer = new AnomalyAttributionRunner.AnomalyAttributionTransformer(forest);
    DiVector vector = new DiVector(2);
    vector.low[0] = 1.1;
    vector.high[1] = 2.2;
    when(forest.getAnomalyAttribution(new double[] { 1.0, 2.0 })).thenReturn(vector);
    assertEquals(Arrays.asList("1.1", "0.0", "0.0", "2.2"), transformer.getResultValues(1.0, 2.0));
    assertEquals(Arrays.asList("anomaly_low_0", "anomaly_high_0", "anomaly_low_1", "anomaly_high_1"), transformer.getResultColumnNames());
    assertEquals(Arrays.asList("NA", "NA", "NA", "NA"), transformer.getEmptyResultValue());
}
Also used : DiVector(com.amazon.randomcutforest.returntypes.DiVector) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) Test(org.junit.jupiter.api.Test)

Example 29 with RandomCutForest

use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.

the class ObjectStreamExample method run.

@Override
public void run() throws Exception {
    // Create and populate a random cut forest
    int dimensions = 10;
    int numberOfTrees = 50;
    int sampleSize = 256;
    Precision precision = Precision.FLOAT_32;
    RandomCutForest forest = RandomCutForest.builder().compact(true).dimensions(dimensions).numberOfTrees(numberOfTrees).sampleSize(sampleSize).precision(precision).build();
    int dataSize = 1000 * sampleSize;
    NormalMixtureTestData testData = new NormalMixtureTestData();
    for (double[] point : testData.generateTestData(dataSize, dimensions)) {
        forest.update(point);
    }
    // Convert to an array of bytes and print the size
    RandomCutForestMapper mapper = new RandomCutForestMapper();
    mapper.setSaveExecutorContextEnabled(true);
    System.out.printf("dimensions = %d, numberOfTrees = %d, sampleSize = %d, precision = %s%n", dimensions, numberOfTrees, sampleSize, precision);
    byte[] bytes = serialize(mapper.toState(forest));
    System.out.printf("Object output stream size = %d bytes%n", bytes.length);
    // Restore from object stream and compare anomaly scores produced by the two
    // forests
    RandomCutForestState state2 = (RandomCutForestState) deserialize(bytes);
    RandomCutForest forest2 = mapper.toModel(state2);
    int testSize = 100;
    double delta = Math.log(sampleSize) / Math.log(2) * 0.05;
    int differences = 0;
    int anomalies = 0;
    for (double[] point : testData.generateTestData(testSize, dimensions)) {
        double score = forest.getAnomalyScore(point);
        double score2 = forest2.getAnomalyScore(point);
        // also scored as an anomaly by the other forest
        if (score > 1 || score2 > 1) {
            anomalies++;
            if (Math.abs(score - score2) > delta) {
                differences++;
            }
        }
        forest.update(point);
        forest2.update(point);
    }
    // first validate that this was a nontrivial test
    if (anomalies == 0) {
        throw new IllegalStateException("test data did not produce any anomalies");
    }
    // validate that the two forests agree on anomaly scores
    if (differences >= 0.01 * testSize) {
        throw new IllegalStateException("restored forest does not agree with original forest");
    }
    System.out.println("Looks good!");
}
Also used : Precision(com.amazon.randomcutforest.config.Precision) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) NormalMixtureTestData(com.amazon.randomcutforest.testutils.NormalMixtureTestData) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState)

Example 30 with RandomCutForest

use of com.amazon.randomcutforest.RandomCutForest in project ml-commons by opensearch-project.

the class BatchRandomCutForest method predict.

@Override
public MLOutput predict(DataFrame dataFrame, Model model) {
    if (model == null) {
        throw new IllegalArgumentException("No model found for batch RCF prediction.");
    }
    RandomCutForestState state = (RandomCutForestState) ModelSerDeSer.deserialize(model.getContent());
    RandomCutForest forest = rcfMapper.toModel(state);
    List<Map<String, Object>> predictResult = process(dataFrame, forest, 0);
    return MLPredictionOutput.builder().predictionResult(DataFrameBuilder.load(predictResult)).build();
}
Also used : RandomCutForest(com.amazon.randomcutforest.RandomCutForest) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RandomCutForest (com.amazon.randomcutforest.RandomCutForest)33 Random (java.util.Random)14 RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)11 Precision (com.amazon.randomcutforest.config.Precision)10 RandomCutForestState (com.amazon.randomcutforest.state.RandomCutForestState)10 Test (org.junit.jupiter.api.Test)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 NormalMixtureTestData (com.amazon.randomcutforest.testutils.NormalMixtureTestData)7 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)5 AnomalyDescriptor (com.amazon.randomcutforest.parkservices.AnomalyDescriptor)4 CompactSampler (com.amazon.randomcutforest.sampler.CompactSampler)4 MultiDimDataWithKey (com.amazon.randomcutforest.testutils.MultiDimDataWithKey)4 ArrayList (java.util.ArrayList)4 ComponentList (com.amazon.randomcutforest.ComponentList)3 PointStoreCoordinator (com.amazon.randomcutforest.executor.PointStoreCoordinator)3 CompactSamplerMapper (com.amazon.randomcutforest.state.sampler.CompactSamplerMapper)3 CompactSamplerState (com.amazon.randomcutforest.state.sampler.CompactSamplerState)3 PointStoreMapper (com.amazon.randomcutforest.state.store.PointStoreMapper)3 CompactRandomCutTreeContext (com.amazon.randomcutforest.state.tree.CompactRandomCutTreeContext)3 IPointStore (com.amazon.randomcutforest.store.IPointStore)3