Search in sources :

Example 21 with PredictionInputsDataDistribution

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

the class FraudScoringDmnLimeExplainerTest method testExplanationWithDataDistribution.

@Test
void testExplanationWithDataDistribution() throws ExecutionException, InterruptedException, TimeoutException {
    PredictionProvider model = getModel();
    List<PredictionInput> samples = DmnTestUtils.randomFraudScoringInputs();
    List<PredictionInput> inputs = samples.subList(0, 10);
    Random random = new Random();
    random.setSeed(0);
    PerturbationContext perturbationContext = new PerturbationContext(random, 1);
    LimeConfig initialConfig = new LimeConfig().withSamples(10).withDataDistribution(new PredictionInputsDataDistribution(inputs)).withPerturbationContext(perturbationContext);
    LimeExplainer limeExplainer = new LimeExplainer(initialConfig);
    PredictionInput testPredictionInput = getTestInput();
    List<PredictionOutput> testPredictionOutputs = model.predictAsync(List.of(testPredictionInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    Prediction instance = new SimplePrediction(testPredictionInput, testPredictionOutputs.get(0));
    assertDoesNotThrow(() -> ValidationUtils.validateLocalSaliencyStability(model, instance, limeExplainer, 1, 0.5, 0.5));
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) Random(java.util.Random) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) PredictionInputsDataDistribution(org.kie.kogito.explainability.model.PredictionInputsDataDistribution) Test(org.junit.jupiter.api.Test)

Example 22 with PredictionInputsDataDistribution

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

the class PrequalificationDmnLimeExplainerTest method testPrequalificationDMNExplanation.

@Test
void testPrequalificationDMNExplanation() throws ExecutionException, InterruptedException, TimeoutException {
    PredictionProvider model = getModel();
    PredictionInput predictionInput = getTestInput();
    Random random = new Random();
    PerturbationContext perturbationContext = new PerturbationContext(0L, random, 1);
    LimeConfig limeConfig = new LimeConfig().withSamples(10).withPerturbationContext(perturbationContext);
    LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
    List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(predictionInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    Prediction prediction = new SimplePrediction(predictionInput, predictionOutputs.get(0));
    Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    for (Saliency saliency : saliencyMap.values()) {
        assertNotNull(saliency);
        List<FeatureImportance> topFeatures = saliency.getTopFeatures(2);
        if (!topFeatures.isEmpty()) {
            assertThat(ExplainabilityMetrics.impactScore(model, prediction, topFeatures)).isPositive();
        }
    }
    assertDoesNotThrow(() -> ValidationUtils.validateLocalSaliencyStability(model, prediction, limeExplainer, 1, 0.3, 0.3));
    String decision = "LLPA";
    List<PredictionInput> inputs = new ArrayList<>();
    for (int n = 0; n < 10; n++) {
        inputs.add(new PredictionInput(DataUtils.perturbFeatures(predictionInput.getFeatures(), perturbationContext)));
    }
    DataDistribution distribution = new PredictionInputsDataDistribution(inputs);
    int k = 2;
    int chunkSize = 2;
    double f1 = ExplainabilityMetrics.getLocalSaliencyF1(decision, model, limeExplainer, distribution, k, chunkSize);
    AssertionsForClassTypes.assertThat(f1).isBetween(0.5d, 1d);
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) ArrayList(java.util.ArrayList) Saliency(org.kie.kogito.explainability.model.Saliency) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) Random(java.util.Random) FeatureImportance(org.kie.kogito.explainability.model.FeatureImportance) PredictionInputsDataDistribution(org.kie.kogito.explainability.model.PredictionInputsDataDistribution) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionInputsDataDistribution(org.kie.kogito.explainability.model.PredictionInputsDataDistribution) Test(org.junit.jupiter.api.Test)

Aggregations

PredictionInputsDataDistribution (org.kie.kogito.explainability.model.PredictionInputsDataDistribution)22 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)21 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)20 Prediction (org.kie.kogito.explainability.model.Prediction)20 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)20 Random (java.util.Random)19 DataDistribution (org.kie.kogito.explainability.model.DataDistribution)19 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)19 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)19 Saliency (org.kie.kogito.explainability.model.Saliency)18 ArrayList (java.util.ArrayList)16 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)13 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)13 Test (org.junit.jupiter.api.Test)12 Feature (org.kie.kogito.explainability.model.Feature)12 FeatureImportance (org.kie.kogito.explainability.model.FeatureImportance)10 LinkedList (java.util.LinkedList)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ValueSource (org.junit.jupiter.params.provider.ValueSource)7 HashMap (java.util.HashMap)5