Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6854 - added precision, recall, f1 measure metrics to ConfusionMatrix

Tommaso Teofili4af4c701b242
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/utils/ConfusionMatrixGenerator.java b/lucene/classification/src/java/org/apache/lucene/classification/utils/ConfusionMatrixGenerator.java
index ce48d5c9108..78ad691ac1f 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/utils/ConfusionMatrixGenerator.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/utils/ConfusionMatrixGenerator.java
@@ -32,12 +32,10 @@ import org.apache.lucene.classification.ClassificationResult;
import org.apache.lucene.classification.Classifier;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.StoredDocument;
-import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
-import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NamedThreadFactory;
@@ -152,6 +150,7 @@ public class ConfusionMatrixGenerator {
/**
* get the linearized confusion matrix as a {@link Map}
+ *
* @return a {@link Map} whose keys are the correct classification answers and whose values are the actual answers'
* counts
*/
@@ -159,6 +158,61 @@ public class ConfusionMatrixGenerator {
return Collections.unmodifiableMap(linearizedMatrix);
}
+ /**
+ * calculate precision on the given class
+ *
+ * @param klass the class to calculate the precision for
+ * @return the precision for the given class
+ */
+ public double getPrecision(String klass) {
+ Map<String, Long> classifications = linearizedMatrix.get(klass);
+ double tp = 0;
+ double fp = 0;
+ for (Map.Entry<String, Long> entry : classifications.entrySet()) {
+ if (klass.equals(entry.getKey())) {
+ tp += entry.getValue();
+ }
+ }
+ for (Map<String, Long> values : linearizedMatrix.values()) {
+ if (values.containsKey(klass)) {
+ fp += values.get(klass);
+ }
+ }
+ return tp / (tp + fp);
+ }
+
+ /**
+ * calculate recall on the given class
+ *
+ * @param klass the class to calculate the recall for
+ * @return the recall for the given class
+ */
+ public double getRecall(String klass) {
+ Map<String, Long> classifications = linearizedMatrix.get(klass);
+ double tp = 0;
+ double fn = 0;
+ for (Map.Entry<String, Long> entry : classifications.entrySet()) {
+ if (klass.equals(entry.getKey())) {
+ tp += entry.getValue();
+ } else {
+ fn += entry.getValue();
+ }
+ }
+ return tp / (tp + fn);
+ }
+
+ /**
+ * get the F-1 measure of the given class
+ *
+ * @param klass the class to calculate the F-1 measure for
+ * @return the F-1 measure for the given class
+ */
+ public double getF1Measure(String klass) {
+ double recall = getRecall(klass);
+ double precision = getPrecision(klass);
+ return 2 * precision * recall / (precision + recall);
+ }
+
/**
* Calculate accuracy on this confusion matrix using the formula:
* {@literal accuracy = correctly-classified / (correctly-classified + wrongly-classified)}
@@ -199,6 +253,7 @@ public class ConfusionMatrixGenerator {
/**
* get the average classification time in milliseconds
+ *
* @return the avg classification time
*/
public double getAvgClassificationTime() {
@@ -207,6 +262,7 @@ public class ConfusionMatrixGenerator {
/**
* get the no. of documents evaluated while generating this confusion matrix
+ *
* @return the no. of documents evaluated
*/
public int getNumberOfEvaluatedDocs() {
diff --git a/lucene/classification/src/test/org/apache/lucene/classification/utils/ConfusionMatrixGeneratorTest.java b/lucene/classification/src/test/org/apache/lucene/classification/utils/ConfusionMatrixGeneratorTest.java
index 23d08fbba86..faae08393df 100644
--- a/lucene/classification/src/test/org/apache/lucene/classification/utils/ConfusionMatrixGeneratorTest.java
+++ b/lucene/classification/src/test/org/apache/lucene/classification/utils/ConfusionMatrixGeneratorTest.java
@@ -145,6 +145,12 @@ public class ConfusionMatrixGeneratorTest extends ClassificationTestBase<Object>
assertEquals(7, confusionMatrix.getNumberOfEvaluatedDocs());
assertTrue(confusionMatrix.getAvgClassificationTime() >= 0d);
assertTrue(confusionMatrix.getAccuracy() > 0d);
+ assertTrue(confusionMatrix.getPrecision("true") > 0d);
+ assertTrue(confusionMatrix.getPrecision("false") > 0d);
+ assertTrue(confusionMatrix.getRecall("true") > 0d);
+ assertTrue(confusionMatrix.getRecall("false") > 0d);
+ assertTrue(confusionMatrix.getF1Measure("true") > 0d);
+ assertTrue(confusionMatrix.getF1Measure("false") > 0d);
} finally {
if (reader != null) {
reader.close();