Search in sources :

Example 41 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method differentFeatureTypes.

@Test
void differentFeatureTypes() {
    Feature x = FeatureFactory.newCategoricalFeature("x", UUID.randomUUID().toString());
    Feature y = FeatureFactory.newNumericalFeature("y", 0.0);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        CounterFactualScoreCalculator.outputDistance(ox, oy);
    });
    assertEquals("Features must have the same type. Feature 'x', has type 'categorical' and 'number'", exception.getMessage());
}
Also used : PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method currencyDistanceDifferentValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void currencyDistanceDifferentValue(int seed) {
    final Random random = new Random(seed);
    Feature x = FeatureFactory.newCurrencyFeature("x", Currency.getInstance("GBP"));
    Feature y = FeatureFactory.newCurrencyFeature("y", Currency.getInstance("EUR"));
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.CURRENCY, ox.getType());
    assertEquals(Type.CURRENCY, oy.getType());
    assertEquals(1.0, distance);
    // Use a random threshold, mustn't make a difference
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
    assertEquals(1.0, distance);
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 43 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method DoubleDistanceDifferentValueThreshold.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceDifferentValueThreshold(int seed) {
    final Random random = new Random(seed);
    double value = random.nextDouble() * 100.0;
    Feature x = FeatureFactory.newNumericalFeature("x", value);
    Feature y = FeatureFactory.newNumericalFeature("y", value + 100.0);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.25);
    assertEquals(Type.NUMBER, ox.getType());
    assertEquals(Type.NUMBER, oy.getType());
    assertTrue(distance * distance > 0);
    y = FeatureFactory.newNumericalFeature("y", value - 100);
    oy = outputFromFeature(y);
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.25);
    assertTrue(distance * distance > 0);
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 44 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method binaryDistanceSameValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void binaryDistanceSameValue(int seed) {
    final Random random = new Random(seed);
    final ByteBuffer value = ByteBuffer.wrap("foo".getBytes());
    Feature x = FeatureFactory.newBinaryFeature("x", value);
    Feature y = FeatureFactory.newBinaryFeature("y", value);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.BINARY, ox.getType());
    assertEquals(0.0, distance);
    // Use a random threshold, mustn't make a difference
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
    assertEquals(0.0, distance);
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) ByteBuffer(java.nio.ByteBuffer) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 45 with Feature

use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.

the class DataUtils method dropOnLinearizedFeatures.

/**
 * Drop a target feature from a "linearized" version of a source feature.
 * Any of such linearized features are eventually dropped if they match on associated name, type and value.
 *
 * @param target the target feature
 * @param sourceFeature the source feature
 * @return the source feature having one of its underlying "linearized" values eventually dropped
 */
protected static Feature dropOnLinearizedFeatures(Feature target, Feature sourceFeature) {
    Feature f = null;
    List<Feature> linearizedFeatures = DataUtils.getLinearizedFeatures(List.of(sourceFeature));
    int i = 0;
    for (Feature linearizedFeature : linearizedFeatures) {
        if (target.getValue().equals(linearizedFeature.getValue())) {
            linearizedFeatures.set(i, FeatureFactory.copyOf(linearizedFeature, linearizedFeature.getType().drop(target.getValue())));
            f = FeatureFactory.newCompositeFeature(target.getName(), linearizedFeatures);
            break;
        } else {
            i++;
        }
    }
    // not found
    if (f == null) {
        f = FeatureFactory.copyOf(sourceFeature, sourceFeature.getValue());
    }
    return f;
}
Also used : Feature(org.kie.kogito.explainability.model.Feature)

Aggregations

Feature (org.kie.kogito.explainability.model.Feature)233 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)118 Test (org.junit.jupiter.api.Test)107 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)107 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)104 Output (org.kie.kogito.explainability.model.Output)102 ArrayList (java.util.ArrayList)97 Random (java.util.Random)92 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)78 Value (org.kie.kogito.explainability.model.Value)74 LinkedList (java.util.LinkedList)72 ValueSource (org.junit.jupiter.params.provider.ValueSource)71 Prediction (org.kie.kogito.explainability.model.Prediction)67 List (java.util.List)51 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)46 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)42 Type (org.kie.kogito.explainability.model.Type)39 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)37 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)35 FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)33