Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Extend default evaluation metrics to all available #63939

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it on purpose not to include MSLE here? Is it because we cannot set offset automagically for the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was intentional indeed. The reason is because we cannot calculate MSLE for negative values.

}

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 @@ -32,6 +32,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 @@ -81,7 +82,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 @@ -24,6 +24,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 @@ -52,7 +53,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