Skip to content

Commit

Permalink
[7.x][ML] Extend default evaluation metrics to all available (#63939) (
Browse files Browse the repository at this point in the history
…#63965)

This commit extends the set of default metrics for the
data frame analytics evaluation API to all available metrics.
The motivation is that if the user skips setting an explicit
set of metrics, they get most of the evaluation offering.

Backport of #63939
  • Loading branch information
dimitris-athanasiou authored Oct 21, 2020
1 parent b6db094 commit 367a135
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Classification(String actualField,
}

private static List<EvaluationMetric> defaultMetrics() {
return Arrays.asList(new MulticlassConfusionMatrix());
return Arrays.asList(new Accuracy(), new MulticlassConfusionMatrix(), new Precision(), new Recall());
}

public Classification(StreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public Huber(StreamInput in) throws IOException {
this.delta = in.readDouble();
}

public Huber() {
this(DEFAULT_DELTA);
}

public Huber(@Nullable Double delta) {
this.delta = delta != null ? delta : DEFAULT_DELTA;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Regression(String actualField, String predictedField, @Nullable List<Eval
}

private static List<EvaluationMetric> defaultMetrics() {
return Arrays.asList(new MeanSquaredError(), new RSquared());
return Arrays.asList(new MeanSquaredError(), new RSquared(), new Huber());
}

public Regression(StreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static org.elasticsearch.test.hamcrest.OptionalMatchers.isEmpty;
import static org.elasticsearch.test.hamcrest.OptionalMatchers.isPresent;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -110,6 +111,14 @@ public void testConstructor_GivenEmptyMetrics() {
assertThat(e.getMessage(), equalTo("[classification] must have one or more metrics"));
}

public void testConstructor_GivenDefaultMetrics() {
Classification classification = new Classification("actual", "predicted", null, null);

List<EvaluationMetric> metrics = classification.getMetrics();

assertThat(metrics, containsInAnyOrder(new Accuracy(), new MulticlassConfusionMatrix(), new Precision(), new Recall()));
}

public void testGetFields() {
Classification evaluation = new Classification("foo", "bar", "results", null);
EvaluationFields fields = evaluation.getFields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -89,6 +90,17 @@ public void testConstructor_GivenEmptyMetrics() {
assertThat(e.getMessage(), equalTo("[outlier_detection] must have one or more metrics"));
}

public void testConstructor_GivenDefaultMetrics() {
OutlierDetection outlierDetection = new OutlierDetection("actual", "predicted", null);

List<EvaluationMetric> metrics = outlierDetection.getMetrics();

assertThat(metrics, containsInAnyOrder(new AucRoc(false),
new Precision(Arrays.asList(0.25, 0.5, 0.75)),
new Recall(Arrays.asList(0.25, 0.5, 0.75)),
new ConfusionMatrix(Arrays.asList(0.25, 0.5, 0.75))));
}

public void testGetFields() {
OutlierDetection evaluation = new OutlierDetection("foo", "bar", null);
EvaluationFields fields = evaluation.getFields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -76,6 +77,14 @@ public void testConstructor_GivenEmptyMetrics() {
assertThat(e.getMessage(), equalTo("[regression] must have one or more metrics"));
}

public void testConstructor_GivenDefaultMetrics() {
Regression regression = new Regression("actual", "predicted", null);

List<EvaluationMetric> metrics = regression.getMetrics();

assertThat(metrics, containsInAnyOrder(new Huber(), new MeanSquaredError(), new RSquared()));
}

public void testGetFields() {
Regression evaluation = new Regression("foo", "bar", null);
EvaluationFields fields = evaluation.getFields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -82,7 +83,13 @@ public void testEvaluate_DefaultMetrics() {
assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName()));
assertThat(
evaluateDataFrameResponse.getMetrics().stream().map(EvaluationMetricResult::getMetricName).collect(toList()),
contains(MulticlassConfusionMatrix.NAME.getPreferredName()));
containsInAnyOrder(
MulticlassConfusionMatrix.NAME.getPreferredName(),
Accuracy.NAME.getPreferredName(),
Precision.NAME.getPreferredName(),
Recall.NAME.getPreferredName()
)
);
}

public void testEvaluate_AllMetrics() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

Expand Down Expand Up @@ -53,7 +54,12 @@ public void testEvaluate_DefaultMetrics() {
assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName()));
assertThat(
evaluateDataFrameResponse.getMetrics().stream().map(EvaluationMetricResult::getMetricName).collect(toList()),
contains(MeanSquaredError.NAME.getPreferredName(), RSquared.NAME.getPreferredName()));
containsInAnyOrder(
MeanSquaredError.NAME.getPreferredName(),
RSquared.NAME.getPreferredName(),
Huber.NAME.getPreferredName()
)
);
}

public void testEvaluate_AllMetrics() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,10 @@ setup:
}
- is_true: classification.multiclass_confusion_matrix
- is_true: classification.accuracy
- is_true: classification.precision
- is_true: classification.recall
- is_false: classification.auc_roc
---
"Test classification given missing actual_field":
- do:
Expand Down Expand Up @@ -1104,8 +1108,8 @@ setup:
- match: { regression.mse.value: 28.67749840974834 }
- match: { regression.r_squared.value: 0.8551031778603486 }
- match: { regression.huber.value: 1.9205280586939963 }
- is_false: regression.msle.value
- is_false: regression.huber.value
---
"Test regression given missing actual_field":
- do:
Expand Down

0 comments on commit 367a135

Please sign in to comment.