-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.java
More file actions
878 lines (713 loc) · 24.5 KB
/
Copy path15.java
File metadata and controls
878 lines (713 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
* Copyright 2013-2015 Ivan Habernal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.habernal.confusionmatrix;
import java.util.*;
/**
* Implementation of confusion matrix for evaluating learning algorithms; computes macro F-measure,
* accuracy, confidence intervals, Cohen's Kappa
*
* @author Ivan Habernal
*/
public class ConfusionMatrix
{
protected int total = 0;
protected int correct = 0;
protected Map<String, Map<String, Integer>> map;
protected int numberOfDecimalPlaces = 3;
protected TreeSet<String> allGoldLabels = new TreeSet<>();
protected TreeSet<String> allPredictedLabels = new TreeSet<>();
protected List<String> labelSeries = new ArrayList<>();
private Locale locale = Locale.ENGLISH;
public ConfusionMatrix()
{
this.map = new TreeMap<>();
}
public void setNumberOfDecimalPlaces(int numberOfDecimalPlaces)
throws IllegalArgumentException
{
if (numberOfDecimalPlaces < 1 || numberOfDecimalPlaces > 100) {
throw new IllegalArgumentException("Argument must be in rage 1-100");
}
this.numberOfDecimalPlaces = numberOfDecimalPlaces;
}
public void setLocale(Locale locale)
{
this.locale = locale;
}
private Locale getLocale() {
return locale;
}
private String getFormat()
{
return "%." + numberOfDecimalPlaces + "f";
}
public void increaseValue(String goldValue, String observedValue)
{
increaseValue(goldValue, observedValue, 1);
}
/**
* Returns the series of actual labels
*
* @return list of labels
*/
public List<String> getLabelSeries()
{
return labelSeries;
}
/**
* Increases value of goldValue x observedValue n times
*
* @param goldValue exp
* @param observedValue ac
* @param times n-times
*/
public void increaseValue(String goldValue, String observedValue, int times)
{
allGoldLabels.add(goldValue);
allPredictedLabels.add(observedValue);
for (int i = 0; i < times; i++) {
labelSeries.add(observedValue);
}
if (!map.containsKey(goldValue)) {
map.put(goldValue, new TreeMap<String, Integer>());
}
if (!map.get(goldValue).containsKey(observedValue)) {
map.get(goldValue).put(observedValue, 0);
}
int currentValue = this.map.get(goldValue).get(observedValue);
this.map.get(goldValue).put(observedValue, currentValue + times);
total += times;
if (goldValue.equals(observedValue)) {
correct += times;
}
}
public double getAccuracy()
{
return ((double) correct / (double) total);
}
public int getTotalSum()
{
return total;
}
public int getRowSum(String label)
{
int result = 0;
for (Integer i : map.get(label).values()) {
result += i;
}
return result;
}
public int getColSum(String label)
{
int result = 0;
for (Map<String, Integer> row : this.map.values()) {
if (row.containsKey(label)) {
result += row.get(label);
}
}
return result;
}
public Map<String, Double> getPrecisionForLabels()
{
Map<String, Double> precisions = new LinkedHashMap<>();
for (String label : allGoldLabels) {
double precision = getPrecisionForLabel(label);
precisions.put(label, precision);
}
return precisions;
}
public double getPrecisionForLabel(String label)
{
double precision = 0;
int tp = 0;
int fpAndTp = 0;
if (map.containsKey(label) && map.get(label).containsKey(label)) {
tp = this.map.get(label).get(label);
fpAndTp = getColSum(label);
}
if (fpAndTp > 0) {
precision = (double) tp / (double) (fpAndTp);
}
return precision;
}
/**
* Micro-averaged F-measure gives equal weight to each document and is therefore
* considered as an average over all the document/category pairs. It tends to be
* dominated by the classifier’s performance on common categories. (It's actually the accuracy).
* <p/>
* (from Ozgur et al., 2005. Text Categorization with Class-Based and Corpus-Based Keyword Selection.)
*
* @return double
*/
public double getMicroFMeasure()
{
int allTruePositives = 0;
int allTruePositivesAndFalsePositives = 0;
int allTruePositivesAndFalseNegatives = 0;
for (String label : map.keySet()) {
if (map.containsKey(label) && map.get(label).containsKey(label)) {
allTruePositives += this.map.get(label).get(label);
}
allTruePositivesAndFalsePositives += getColSum(label);
allTruePositivesAndFalseNegatives += getRowSum(label);
}
double precision = (double) allTruePositives / (double) allTruePositivesAndFalsePositives;
double recall = (double) allTruePositives / (double) allTruePositivesAndFalseNegatives;
return (2.0 * precision * recall) / (precision + recall);
}
/**
* Macro-averaged F-measure gives equal
* weight to each category, regardless of its frequency. It is influenced more by the
* classifier performance on rare categories.
*
* @return double
*/
public double getMacroFMeasure()
{
Map<String, Double> fMeasureForLabels = getFMeasureForLabels();
double totalFMeasure = 0;
for (Double d : fMeasureForLabels.values()) {
totalFMeasure += d;
}
return totalFMeasure / fMeasureForLabels.size();
}
/**
* Macro-averaged F-measure with beta parameter.
*
* @param beta beta parameter
* @return double
*/
public double getMacroFMeasure(double beta)
{
Map<String, Double> fMeasureForLabels = getFMeasureForLabels(beta);
double totalFMeasure = 0;
for (Double d : fMeasureForLabels.values()) {
totalFMeasure += d;
}
return totalFMeasure / fMeasureForLabels.size();
}
/**
* Returns F-measure for categories; see See http://en.wikipedia.org/wiki/F1_score
*
* @return double
*/
public Map<String, Double> getFMeasureForLabels()
{
Map<String, Double> fMeasure = new LinkedHashMap<>();
Map<String, Double> precisionForLabels = getPrecisionForLabels();
Map<String, Double> recallForLabels = getRecallForLabels();
for (String label : allGoldLabels) {
double p = precisionForLabels.get(label);
double r = recallForLabels.get(label);
double fm = 0;
if ((p + r) > 0) {
fm = (2 * p * r) / (p + r);
}
fMeasure.put(label, fm);
}
return fMeasure;
}
/**
* See http://en.wikipedia.org/wiki/F1_score
*
* @param beta beta paremeter; higher than 1 prefers recall, lower than 1 prefers precision
* @return double
*/
public Map<String, Double> getFMeasureForLabels(double beta)
{
Map<String, Double> fMeasure = new LinkedHashMap<>();
Map<String, Double> precisionForLabels = getPrecisionForLabels();
Map<String, Double> recallForLabels = getRecallForLabels();
for (String label : allGoldLabels) {
double p = precisionForLabels.get(label);
double r = recallForLabels.get(label);
double fm = 0;
if ((p + r) > 0) {
fm = (1.0 + (beta * beta)) * ((p * r) / ((beta * beta * p) + r));
}
fMeasure.put(label, fm);
}
return fMeasure;
}
/**
* Return recall for labels
*
* @return double
*/
public Map<String, Double> getRecallForLabels()
{
Map<String, Double> recalls = new LinkedHashMap<>();
for (String label : allGoldLabels) {
double recall = getRecallForLabel(label);
recalls.put(label, recall);
}
return recalls;
}
/**
* Return recall for single label
*
* @param label label
* @return double
*/
public double getRecallForLabel(String label)
{
int fnAndTp = 0;
double recall = 0;
int tp = 0;
if (map.containsKey(label) && map.get(label).containsKey(label)) {
tp = this.map.get(label).get(label);
fnAndTp = getRowSum(label);
}
if (fnAndTp > 0) {
recall = (double) tp / (double) (fnAndTp);
}
return recall;
}
/**
* Returns the half of the confidence interval on accuracy on alpha = 95
*
* @return conf. int
*/
public double getConfidence95Accuracy()
{
return 1.96 * Math.sqrt(getAccuracy() * (1.0 - getAccuracy()) / total);
}
/**
* Returns the half of the confidence interval on accuracy on alpha = 90
*
* @return conf. int
*/
public double getConfidence90Accuracy()
{
return 1.645 * Math.sqrt(getAccuracy() * (1.0 - getAccuracy()) / total);
}
public double getConfidence90AccuracyLow()
{
return getAccuracy() - getConfidence90Accuracy();
}
public double getConfidence90AccuracyHigh()
{
return getAccuracy() + getConfidence90Accuracy();
}
/**
* Returns the lower bound of the accuracy with alpha = 95
*
* @return accuracy minus half of the confidence interval
*/
public double getConfidence95AccuracyLow()
{
return getAccuracy() - getConfidence95Accuracy();
}
/**
* Returns the upper bound of the accuracy with alpha = 95
*
* @return accuracy plus half of the confidence interval
*/
public double getConfidence95AccuracyHigh()
{
return getAccuracy() + getConfidence95Accuracy();
}
/**
* Returns the half of confidence interval on alpha = 95 (see
* http://alias-i.com/lingpipe/docs/api/com/aliasi/classify/ConfusionMatrix.html#confidence95%28%29)
*
* @return conf
*/
public double getConfidence95MacroFM()
{
return 1.96 * Math.sqrt(getMacroFMeasure() * (1.0 - getMacroFMeasure()) / total);
}
public double getConfidence90MacroFM()
{
return 1.66 * Math.sqrt(getMacroFMeasure() * (1.0 - getMacroFMeasure()) / total);
}
/**
* Returns the lower bound of the macro F-measure with alpha = 95
*
* @return macro F-measure minus half of the confidence interval
*/
public double getConfidence95MacroFMLow()
{
return getMacroFMeasure() - getConfidence95MacroFM();
}
/**
* Returns the upper bound of the macro F-measure with alpha = 95
*
* @return macro F-measure plus half of the confidence interval
*/
public double getConfidence95MacroFMHigh()
{
return getMacroFMeasure() + getConfidence95MacroFM();
}
/**
* Computes Cohen's Kappa
*
* @return double
*/
public double getCohensKappa()
{
// compute p (which is actually accuracy)
double p = getAccuracy();
// System.out.println(p);
// compute pe
double pe = 0;
for (String label : this.allGoldLabels) {
double row = getRowSum(label);
double col = getColSum(label);
// System.out.println("Label " + label + ", sumCol: " + col + ", sumRow: " + row);
pe += (row * col) / getTotalSum();
}
pe = pe / getTotalSum();
return (p - pe) / (1 - pe);
}
private List<List<String>> prepareToString()
{
// adding zeros
for (String row : allGoldLabels) {
if (!map.containsKey(row)) {
map.put(row, new TreeMap<String, Integer>());
}
for (String col : allPredictedLabels) {
if (!map.get(row).containsKey(col)) {
map.get(row).put(col, 0);
}
}
}
List<List<String>> result = new ArrayList<>();
List<String> allPredictedLabelsSorted = new ArrayList<>();
TreeSet<String> extraPredictedLabels = new TreeSet<>(allPredictedLabels);
extraPredictedLabels.removeAll(allGoldLabels);
allPredictedLabelsSorted.addAll(allGoldLabels);
allPredictedLabelsSorted.addAll(extraPredictedLabels);
// header
List<String> header = new ArrayList<>();
header.add("↓gold\\pred→");
header.addAll(allPredictedLabelsSorted);
result.add(header);
for (String rowLabel : allGoldLabels) {
List<String> row = new ArrayList<>();
row.add(rowLabel);
for (String predictedLabel : allPredictedLabelsSorted) {
int value = 0;
if (this.map.containsKey(rowLabel) && this.map.get(rowLabel)
.containsKey(predictedLabel)) {
value = this.map.get(rowLabel).get(predictedLabel);
}
row.add(Integer.toString(value));
}
result.add(row);
}
return result;
}
protected String tableToString(List<List<String>> table)
{
// finding the maximum entry length
int maxEntryLength = Integer.MIN_VALUE;
for (List<String> row : table) {
for (String value : row) {
if (value.length() > maxEntryLength) {
maxEntryLength = value.length();
}
}
}
String f = "%" + (maxEntryLength + 1) + "s";
StringBuilder sb = new StringBuilder();
for (List<String> row : table) {
for (String value : row) {
sb.append(String.format(f, value));
}
sb.append("\n");
}
return sb.toString();
}
@Override
public String toString()
{
List<List<String>> table = prepareToString();
return tableToString(table);
}
/**
* Prints in LaTeX format
*
* @return string
*/
public String toStringLatex()
{
List<List<String>> table = prepareToString();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < table.size(); i++) {
List<String> row = table.get(i);
for (int j = 0; j < row.size(); j++) {
String value = row.get(j);
if ((i == 0 || j == 0) && !value.isEmpty()) {
sb.append("\\textbf{").append(value).append("} ");
}
else {
sb.append(value);
sb.append(" ");
}
if (j < row.size() - 1) {
sb.append("& ");
}
}
sb.append("\\\\\n");
}
return sb.toString();
}
public String printNiceResults()
{
return "Macro F-measure: " + String.format(Locale.ENGLISH, getFormat(), getMacroFMeasure())
+ ", (CI at .95: " + String
.format(Locale.ENGLISH, getFormat(), getConfidence95MacroFM())
+ "), micro F-measure (acc): " + String
.format(Locale.ENGLISH, getFormat(), getMicroFMeasure());
}
public String printLabelPrecRecFm()
{
Map<String, Double> precisionForLabels = getPrecisionForLabels();
Map<String, Double> recallForLabels = getRecallForLabels();
Map<String, Double> fMForLabels = getFMeasureForLabels();
StringBuilder sb = new StringBuilder("P/R/Fm: ");
for (Map.Entry<String, Double> entry : precisionForLabels.entrySet()) {
sb.append(entry.getKey());
sb.append("=");
sb.append(String.format(Locale.ENGLISH, getFormat(), entry.getValue()));
sb.append("/");
sb.append(String.format(Locale.ENGLISH, getFormat(),
recallForLabels.get(entry.getKey())));
sb.append("/");
sb.append(String.format(Locale.ENGLISH, getFormat(), fMForLabels.get(entry.getKey())));
sb.append(" ");
}
return sb.toString();
}
public double getAvgPrecision()
{
double res = 0;
Collection<Double> values = getPrecisionForLabels().values();
for (double d : values) {
res += d;
}
return res / (double) values.size();
}
public double getAvgRecall()
{
double res = 0;
Collection<Double> values = getRecallForLabels().values();
for (double d : values) {
res += d;
}
return res / (double) values.size();
}
/**
* Sums up all matrices into a new one
*
* @param matrices confusion matrices
* @return confusion matrix
*/
public static ConfusionMatrix createCumulativeMatrix(ConfusionMatrix... matrices)
{
ConfusionMatrix result = new ConfusionMatrix();
for (ConfusionMatrix matrix : matrices) {
for (Map.Entry<String, Map<String, Integer>> gold : matrix.map.entrySet()) {
for (Map.Entry<String, Integer> actual : gold.getValue().entrySet()) {
result.increaseValue(gold.getKey(), actual.getKey(), actual.getValue());
}
}
}
return result;
}
/**
* Returns C + C^T - InC (this + transposed this - unit matrix * this), see
* <p/>
* See
* Silvie Cinkova, Martin Holub, and Vincent Kriz. 2012. Managing uncertainty in semantic
* tagging. In Proceedings of the 13th Conference of the European Chapter of the Association
* for Computational Linguistics, EACL '12, pages 840-850, Stroudsburg, PA, USA.
* Association for Computational Linguistics.
*
* @return new instance
*/
public ConfusionMatrix getSymmetricConfusionMatrix()
{
return createCumulativeMatrix(this, getTransposedMatrix(), getNegativeUnitMatrix());
}
/**
* Returns transposed confusion matrix (gold and predicted are switched)
*
* @return new instance
*/
public ConfusionMatrix getTransposedMatrix()
{
ConfusionMatrix result = new ConfusionMatrix();
for (Map.Entry<String, Map<String, Integer>> gold : this.map.entrySet()) {
for (Map.Entry<String, Integer> predicted : gold.getValue().entrySet()) {
int value = predicted.getValue();
// add reverted values
result.increaseValue(predicted.getKey(), gold.getKey(), value);
}
}
return result;
}
/**
* Returns unit matrix (identity matrix) with all diagonal values negative and non-diagonal zeros
* <pre>
* (- 1) * In
* </pre>
*
* @return negative unit matrix
*/
protected ConfusionMatrix getNegativeUnitMatrix()
{
ConfusionMatrix result = new ConfusionMatrix();
for (Map.Entry<String, Map<String, Integer>> gold : this.map.entrySet()) {
for (Map.Entry<String, Integer> predicted : gold.getValue().entrySet()) {
int value = predicted.getValue();
// negative value on diagonal
if (gold.getKey().equals(predicted.getKey())) {
result.increaseValue(gold.getKey(), predicted.getKey(), -value);
}
else {
// zeros elsewhere
result.increaseValue(gold.getKey(), predicted.getKey(), 0);
}
}
}
return result;
}
/**
* Confusion matrix printed to text by toString can be parsed back
*
* @param text input text
* @return confusion matrix
* @throws IllegalArgumentException if input is malformed
*/
public static ConfusionMatrix parseFromText(String text)
throws IllegalArgumentException
{
try {
String[] lines = text.split("\n");
String[] l = lines[0].split("\\s+");
List<String> labels = new ArrayList<>();
for (String aL : l) {
if (!aL.isEmpty()) {
labels.add(aL);
}
}
ConfusionMatrix result = new ConfusionMatrix();
for (int i = 1; i < lines.length; i++) {
String line = lines[i];
String[] split = line.split("\\s+");
List<String> row = new ArrayList<>();
for (String aSplit : split) {
if (!aSplit.isEmpty()) {
row.add(aSplit);
}
}
String predictedLabel = row.get(0);
for (int r = 1; r < row.size(); r++) {
String s = row.get(r);
Integer val = Integer.valueOf(s);
String acutalLabel = labels.get(r - 1);
result.increaseValue(predictedLabel, acutalLabel, val);
}
}
return result;
}
catch (Exception e) {
throw new IllegalArgumentException("Wrong input format", e);
}
}
/**
* Returns a distribution of classes in gold data and predicted data (absolute
* and relative)
*
* @return string
*/
public String printClassDistributionGold()
{
StringBuilder sb = new StringBuilder("Gold data distribution\t\t");
sb.append("Predicted data distribution\n");
for (String goldLabel : this.allGoldLabels) {
int rowSum = getRowSum(goldLabel);
int colSum = getColSum(goldLabel);
sb.append(String.format(Locale.ENGLISH, "%s\t%d\t%.1f", goldLabel, rowSum,
(double) rowSum / (double) getTotalSum() * 100.0));
sb.append("%\t");
sb.append(String.format(Locale.ENGLISH, "%d\t%.1f", colSum,
(double) colSum / (double) getTotalSum() * 100.0));
sb.append("%\n");
}
sb.append(String.format(Locale.ENGLISH, "Sum\t%d%n", getTotalSum()));
return sb.toString().trim();
}
/**
* Makes probabilistic confusion matrix, where the entries are real numbers sum up to 1 for
* each gold row
*
* @return table
*/
protected List<List<String>> prepareToStringProbabilistic()
{
// adding zeros
for (String row : allGoldLabels) {
if (!map.containsKey(row)) {
map.put(row, new TreeMap<String, Integer>());
}
for (String col : allPredictedLabels) {
if (!map.get(row).containsKey(col)) {
map.get(row).put(col, 0);
}
}
}
List<List<String>> result = new ArrayList<>();
List<String> allPredictedLabelsSorted = new ArrayList<>();
TreeSet<String> extraPredictedLabels = new TreeSet<>(allPredictedLabels);
extraPredictedLabels.removeAll(allGoldLabels);
allPredictedLabelsSorted.addAll(allGoldLabels);
allPredictedLabelsSorted.addAll(extraPredictedLabels);
// header
List<String> header = new ArrayList<>();
header.add("↓gold\\pred→");
header.addAll(allPredictedLabelsSorted);
result.add(header);
for (String rowLabel : allGoldLabels) {
List<String> row = new ArrayList<>();
row.add(rowLabel);
double rowSum = getRowSum(rowLabel);
for (String predictedLabel : allPredictedLabelsSorted) {
double value = 0;
if (this.map.containsKey(rowLabel) && this.map.get(rowLabel)
.containsKey(predictedLabel)) {
value = this.map.get(rowLabel).get(predictedLabel) / rowSum;
}
row.add(String.format(locale, getFormat(), value));
}
result.add(row);
}
return result;
}
/**
* Returns probabilistic confusion matrix (as table String)
*
* @return string
*/
public String toStringProbabilistic()
{
List<List<String>> table = prepareToStringProbabilistic();
return tableToString(table);
}
}