use of org.kie.kogito.explainability.model.GenericFeatureDistribution in project kogito-apps by kiegroup.
the class LimeExplainerTest method testWithDataDistribution.
@Test
void testWithDataDistribution() throws InterruptedException, ExecutionException, TimeoutException {
Random random = new Random();
PerturbationContext perturbationContext = new PerturbationContext(4L, random, 1);
List<FeatureDistribution> featureDistributions = new ArrayList<>();
int nf = 4;
List<Feature> features = new ArrayList<>();
for (int i = 0; i < nf; i++) {
Feature numericalFeature = FeatureFactory.newNumericalFeature("f-" + i, Double.NaN);
features.add(numericalFeature);
List<Value> values = new ArrayList<>();
for (int r = 0; r < 4; r++) {
values.add(Type.NUMBER.randomValue(perturbationContext));
}
featureDistributions.add(new GenericFeatureDistribution(numericalFeature, values));
}
DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(featureDistributions);
LimeConfig limeConfig = new LimeConfig().withDataDistribution(dataDistribution).withPerturbationContext(perturbationContext).withSamples(10);
LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
PredictionInput input = new PredictionInput(features);
PredictionProvider model = TestUtils.getSumThresholdModel(random.nextDouble(), random.nextDouble());
PredictionOutput output = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()).get(0);
Prediction prediction = new SimplePrediction(input, output);
Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
assertThat(saliencyMap).isNotNull();
String decisionName = "inside";
Saliency saliency = saliencyMap.get(decisionName);
assertThat(saliency).isNotNull();
}
use of org.kie.kogito.explainability.model.GenericFeatureDistribution in project kogito-apps by kiegroup.
the class HighScoreNumericFeatureZonesProviderTest method testNonEmptyData.
@Test
void testNonEmptyData() {
Random random = new Random();
random.setSeed(0);
PerturbationContext perturbationContext = new PerturbationContext(random, 1);
List<Feature> features = new ArrayList<>();
PredictionProvider predictionProvider = TestUtils.getSumThresholdModel(0.1, 0.1);
List<FeatureDistribution> featureDistributions = new ArrayList<>();
int nf = 4;
for (int i = 0; i < nf; i++) {
Feature numericalFeature = FeatureFactory.newNumericalFeature("f-" + i, Double.NaN);
features.add(numericalFeature);
List<Value> values = new ArrayList<>();
for (int r = 0; r < 4; r++) {
values.add(Type.NUMBER.randomValue(perturbationContext));
}
featureDistributions.add(new GenericFeatureDistribution(numericalFeature, values));
}
DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(featureDistributions);
Map<String, HighScoreNumericFeatureZones> highScoreFeatureZones = HighScoreNumericFeatureZonesProvider.getHighScoreFeatureZones(dataDistribution, predictionProvider, features, 10);
assertThat(highScoreFeatureZones).isNotNull();
assertThat(highScoreFeatureZones.size()).isEqualTo(4);
}
use of org.kie.kogito.explainability.model.GenericFeatureDistribution in project kogito-apps by kiegroup.
the class DataUtilsTest method testBootstrap.
@Test
void testBootstrap() {
List<Value> values = new ArrayList<>();
PerturbationContext perturbationContext = new PerturbationContext(random, 1);
for (int i = 0; i < 4; i++) {
values.add(Type.NUMBER.randomValue(perturbationContext));
}
Feature mockedNumericFeature = TestUtils.getMockedNumericFeature();
DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(List.of(new GenericFeatureDistribution(mockedNumericFeature, values)));
Map<String, FeatureDistribution> featureDistributionMap = DataUtils.boostrapFeatureDistributions(dataDistribution, perturbationContext, 10, 1, 500, new HashMap<>());
assertThat(featureDistributionMap).isNotNull();
assertThat(featureDistributionMap).isNotEmpty();
FeatureDistribution actual = featureDistributionMap.get(mockedNumericFeature.getName());
assertThat(actual).isNotNull();
List<Value> allSamples = actual.getAllSamples();
assertThat(allSamples).isNotNull();
assertThat(allSamples).hasSize(10);
}
Aggregations