0

I have a list of objects of Statistic class. It contains following attributes:

private String name;
private double min;
private double max;
private double avg;
private double stDev;
private double variation;
private double skewness;
private double median;
private double q1;
private double q3;
private double sum;
private int count;

During the runtime of a program they are calculated and at the end I want to create a list of 5 best results judging by the accuracy value. My code is as follows:

String filePath = "src/test/resources/json/test_write_to_file.json";
SimulationOption option = JsonUtil.read(filePath);

MainController mainController = new MainController();
List<CrossValidationResult> results = mainController.start(option);

int N = 5;

List<Statistic> testingStatistics = ClassificationStatisticHelper.calculateClassificationResultsStatistics(results
                        .parallelStream()
                        .map(CrossValidationResult::getTestingResults)
                        .sorted(Comparator.comparingDouble(o->o.getClassificationCharacteristic().getAccuracy()).reversed())
                        .limit(N)
                        .collect(Collectors.toList()));

I'm getting an error "Cannot resolve method 'getClassificationCharacteristic' in 'Object'" at "getClassificationCharacteristic()" part. This error disappears if I delete ".reversed" but then I get bottom N not top N results. Obviously it's the wrong way of doing what I wanted to do. How should I go about fixing this?

EDIT:

List<Statistic> testingStatistics = ClassificationStatisticHelper.calculateClassificationResultsStatistics(results
                        .parallelStream()
                        .map(CrossValidationResult::getTestingResults)
                        .sorted(Comparator.reverseOrder().thenComparingDouble(o->o.getClassificationCharacteristic().getAccuracy()))
                        .limit(N)
                        .collect(Collectors.toList()));

Doesn't work as well

EDIT 2: getClassificationCharacteristic().getAccuracy() comes from com.common.metrics.ClassificationCharacteristic. Here is how it's calculated:

private void calculateAccuracy() {
        int correctSum = 0;
        for (int i = 0; i < classes.length; i++) {
            correctSum += matrix[i][i];
        }
        int instances = classActualSupport.values().parallelStream().reduce(Integer::sum).orElse(0);
        this.accuracy = 1d * correctSum / instances;
    }

Hope it's relevant to the problem.

MegaKruk
  • 13
  • 4
  • 2
    The error *"Cannot resolve method 'getClassificationCharacteristic' in 'Object'"* is suggesting that not all objects in the pipeline before `sorted` has the `getClassificationCharacteristic` method. For e.g. some of the objects may be null after `map`. – SomeDude May 17 '21 at 13:30
  • But then why is this code working correctly if I just delete "reversed()" from it? Correctly but it does not do what I want. – MegaKruk May 17 '21 at 13:33
  • besides that, shouldn't it be `.sorted(Comperator.comparingDouble(o -> o.getClassificationCharacteristic().getAccuracy()).reversed()` - reverseOrder() just reverses the natural order – Daniel Rafael Wosch May 17 '21 at 13:35
  • If I do so I get "Variable 'o' is already defined in the scope" – MegaKruk May 17 '21 at 13:37
  • Does your statistics class implement the Comparable interface? – Daniel Rafael Wosch May 17 '21 at 13:41
  • Try to cast o variable to the desired class: `.sorted(Comparator.comparingDouble(o->((CrossValidationResult)o).getClassificationCharacteristic().getAccuracy()).reversed())` – Majo May 17 '21 at 13:49
  • Okay lets start from the beginning. Where does `getClassificationCharacteristic().getAccuracy()` come from? Could you please provide the relevant code parts? Otherwise its a little bit like stubbing in the dark – Daniel Rafael Wosch May 17 '21 at 13:49
  • @Majo Sadly it also doesn't work – MegaKruk May 17 '21 at 13:56
  • I edited the question to also contain where does "getClassificationCharacteristic().getAccuracy()" come from – MegaKruk May 17 '21 at 13:57
  • @MegaKruk please provide what class type returns function CrossValidationResult::getTestingResults. The Comparator depends on that type. – Majo May 17 '21 at 14:07
  • @Eritrean Sadly this also doesn't work: ``` List testingStatistics = ClassificationStatisticHelper.calculateClassificationResultsStatistics(results .parallelStream() .map(CrossValidationResult::getTestingResults) //.sorted(Comparator.comparingDouble(o->o.getClassificationCharacteristic().getAccuracy())) .sorted(Comparator.comparing((Statistic s) -> s.getAvg()).reversed()) .limit(N) .collect(Collectors.toList())); ``` – MegaKruk May 17 '21 at 14:09
  • @Majo It returns type of "EvaluationDataResults" – MegaKruk May 17 '21 at 14:14
  • @MegaKruk so try this: `.sorted(Comparator.comparingDouble(o->((EvaluationDataResults) o).getClassificationCharacteristic().getAccuracy()).reversed())` – Majo May 17 '21 at 14:45

1 Answers1

0

I was mistaken. The answer provided in the comments by @Eritrean is indeed correct one. Thank you very much for help

MegaKruk
  • 13
  • 4