|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml.dataframe.evaluation.classification; |
| 20 | + |
| 21 | +import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 24 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link AccuracyMetric} is a metric that answers the question: |
| 39 | + * "What fraction of examples have been classified correctly by the classifier?" |
| 40 | + * |
| 41 | + * equation: accuracy = 1/n * Σ(y == y´) |
| 42 | + */ |
| 43 | +public class AccuracyMetric implements EvaluationMetric { |
| 44 | + |
| 45 | + public static final String NAME = "accuracy"; |
| 46 | + |
| 47 | + private static final ObjectParser<AccuracyMetric, Void> PARSER = new ObjectParser<>(NAME, true, AccuracyMetric::new); |
| 48 | + |
| 49 | + public static AccuracyMetric fromXContent(XContentParser parser) { |
| 50 | + return PARSER.apply(parser, null); |
| 51 | + } |
| 52 | + |
| 53 | + public AccuracyMetric() {} |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getName() { |
| 57 | + return NAME; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 62 | + builder.startObject(); |
| 63 | + builder.endObject(); |
| 64 | + return builder; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean equals(Object o) { |
| 69 | + if (this == o) return true; |
| 70 | + if (o == null || getClass() != o.getClass()) return false; |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int hashCode() { |
| 76 | + return Objects.hashCode(NAME); |
| 77 | + } |
| 78 | + |
| 79 | + public static class Result implements EvaluationMetric.Result { |
| 80 | + |
| 81 | + private static final ParseField ACTUAL_CLASSES = new ParseField("actual_classes"); |
| 82 | + private static final ParseField OVERALL_ACCURACY = new ParseField("overall_accuracy"); |
| 83 | + |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + private static final ConstructingObjectParser<Result, Void> PARSER = |
| 86 | + new ConstructingObjectParser<>("accuracy_result", true, a -> new Result((List<ActualClass>) a[0], (double) a[1])); |
| 87 | + |
| 88 | + static { |
| 89 | + PARSER.declareObjectArray(constructorArg(), ActualClass.PARSER, ACTUAL_CLASSES); |
| 90 | + PARSER.declareDouble(constructorArg(), OVERALL_ACCURACY); |
| 91 | + } |
| 92 | + |
| 93 | + public static Result fromXContent(XContentParser parser) { |
| 94 | + return PARSER.apply(parser, null); |
| 95 | + } |
| 96 | + |
| 97 | + /** List of actual classes. */ |
| 98 | + private final List<ActualClass> actualClasses; |
| 99 | + /** Fraction of documents predicted correctly. */ |
| 100 | + private final double overallAccuracy; |
| 101 | + |
| 102 | + public Result(List<ActualClass> actualClasses, double overallAccuracy) { |
| 103 | + this.actualClasses = Collections.unmodifiableList(Objects.requireNonNull(actualClasses)); |
| 104 | + this.overallAccuracy = overallAccuracy; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getMetricName() { |
| 109 | + return NAME; |
| 110 | + } |
| 111 | + |
| 112 | + public List<ActualClass> getActualClasses() { |
| 113 | + return actualClasses; |
| 114 | + } |
| 115 | + |
| 116 | + public double getOverallAccuracy() { |
| 117 | + return overallAccuracy; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 122 | + builder.startObject(); |
| 123 | + builder.field(ACTUAL_CLASSES.getPreferredName(), actualClasses); |
| 124 | + builder.field(OVERALL_ACCURACY.getPreferredName(), overallAccuracy); |
| 125 | + builder.endObject(); |
| 126 | + return builder; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean equals(Object o) { |
| 131 | + if (this == o) return true; |
| 132 | + if (o == null || getClass() != o.getClass()) return false; |
| 133 | + Result that = (Result) o; |
| 134 | + return Objects.equals(this.actualClasses, that.actualClasses) |
| 135 | + && this.overallAccuracy == that.overallAccuracy; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public int hashCode() { |
| 140 | + return Objects.hash(actualClasses, overallAccuracy); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static class ActualClass implements ToXContentObject { |
| 145 | + |
| 146 | + private static final ParseField ACTUAL_CLASS = new ParseField("actual_class"); |
| 147 | + private static final ParseField ACTUAL_CLASS_DOC_COUNT = new ParseField("actual_class_doc_count"); |
| 148 | + private static final ParseField ACCURACY = new ParseField("accuracy"); |
| 149 | + |
| 150 | + @SuppressWarnings("unchecked") |
| 151 | + private static final ConstructingObjectParser<ActualClass, Void> PARSER = |
| 152 | + new ConstructingObjectParser<>("accuracy_actual_class", true, a -> new ActualClass((String) a[0], (long) a[1], (double) a[2])); |
| 153 | + |
| 154 | + static { |
| 155 | + PARSER.declareString(constructorArg(), ACTUAL_CLASS); |
| 156 | + PARSER.declareLong(constructorArg(), ACTUAL_CLASS_DOC_COUNT); |
| 157 | + PARSER.declareDouble(constructorArg(), ACCURACY); |
| 158 | + } |
| 159 | + |
| 160 | + /** Name of the actual class. */ |
| 161 | + private final String actualClass; |
| 162 | + /** Number of documents (examples) belonging to the {code actualClass} class. */ |
| 163 | + private final long actualClassDocCount; |
| 164 | + /** Fraction of documents belonging to the {code actualClass} class predicted correctly. */ |
| 165 | + private final double accuracy; |
| 166 | + |
| 167 | + public ActualClass( |
| 168 | + String actualClass, long actualClassDocCount, double accuracy) { |
| 169 | + this.actualClass = Objects.requireNonNull(actualClass); |
| 170 | + this.actualClassDocCount = actualClassDocCount; |
| 171 | + this.accuracy = accuracy; |
| 172 | + } |
| 173 | + |
| 174 | + public String getActualClass() { |
| 175 | + return actualClass; |
| 176 | + } |
| 177 | + |
| 178 | + public long getActualClassDocCount() { |
| 179 | + return actualClassDocCount; |
| 180 | + } |
| 181 | + |
| 182 | + public double getAccuracy() { |
| 183 | + return accuracy; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 188 | + builder.startObject(); |
| 189 | + builder.field(ACTUAL_CLASS.getPreferredName(), actualClass); |
| 190 | + builder.field(ACTUAL_CLASS_DOC_COUNT.getPreferredName(), actualClassDocCount); |
| 191 | + builder.field(ACCURACY.getPreferredName(), accuracy); |
| 192 | + builder.endObject(); |
| 193 | + return builder; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public boolean equals(Object o) { |
| 198 | + if (this == o) return true; |
| 199 | + if (o == null || getClass() != o.getClass()) return false; |
| 200 | + ActualClass that = (ActualClass) o; |
| 201 | + return Objects.equals(this.actualClass, that.actualClass) |
| 202 | + && this.actualClassDocCount == that.actualClassDocCount |
| 203 | + && this.accuracy == that.accuracy; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public int hashCode() { |
| 208 | + return Objects.hash(actualClass, actualClassDocCount, accuracy); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments