-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[ML] Add r_squared eval metric to regression #44248
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
Changes from all commits
ea89212
1651974
335a9e1
d707de8
d2b6adf
cfcddce
372a949
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.client.ml.dataframe.evaluation.regression; | ||
|
||
import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
|
||
/** | ||
* Calculates R-Squared between two known numerical fields. | ||
* | ||
* equation: mse = 1 - SSres/SStot | ||
* such that, | ||
* SSres = Σ(y - y´)^2 | ||
* SStot = Σ(y - y_mean)^2 | ||
*/ | ||
public class RSquaredMetric implements EvaluationMetric { | ||
|
||
public static final String NAME = "r_squared"; | ||
|
||
private static final ObjectParser<RSquaredMetric, Void> PARSER = | ||
new ObjectParser<>("r_squared", true, RSquaredMetric::new); | ||
|
||
public static RSquaredMetric fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
public RSquaredMetric() { | ||
|
||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
// create static hash code from name as there are currently no unique fields per class instance | ||
return Objects.hashCode(NAME); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
public static class Result implements EvaluationMetric.Result { | ||
|
||
public static final ParseField VALUE = new ParseField("value"); | ||
private final double value; | ||
|
||
public static Result fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
private static final ConstructingObjectParser<Result, Void> PARSER = | ||
new ConstructingObjectParser<>("r_squared_result", true, args -> new Result((double) args[0])); | ||
|
||
static { | ||
PARSER.declareDouble(constructorArg(), VALUE); | ||
} | ||
|
||
public Result(double value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(VALUE.getPreferredName(), value); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public double getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String getMetricName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Result that = (Result) o; | ||
return Objects.equals(that.value, this.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
|
@@ -52,6 +53,7 @@ public class BinarySoftClassification implements Evaluation { | |
public static final ConstructingObjectParser<BinarySoftClassification, Void> PARSER = | ||
new ConstructingObjectParser<>( | ||
NAME, | ||
true, | ||
args -> new BinarySoftClassification((String) args[0], (String) args[1], (List<EvaluationMetric>) args[2])); | ||
|
||
static { | ||
|
@@ -80,6 +82,10 @@ public static BinarySoftClassification fromXContent(XContentParser parser) { | |
*/ | ||
private final List<EvaluationMetric> metrics; | ||
|
||
public BinarySoftClassification(String actualField, String predictedField) { | ||
this(actualField, predictedField, (List<EvaluationMetric>)null); | ||
} | ||
|
||
public BinarySoftClassification(String actualField, String predictedProbabilityField, EvaluationMetric... metric) { | ||
this(actualField, predictedProbabilityField, Arrays.asList(metric)); | ||
} | ||
|
@@ -88,7 +94,10 @@ public BinarySoftClassification(String actualField, String predictedProbabilityF | |
@Nullable List<EvaluationMetric> metrics) { | ||
this.actualField = Objects.requireNonNull(actualField); | ||
this.predictedProbabilityField = Objects.requireNonNull(predictedProbabilityField); | ||
this.metrics = Objects.requireNonNull(metrics); | ||
if (metrics != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does it mean if there are no Evaluation Metrics, does that make any sense to allow this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way this is an improvement on the code that tagged There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see now. The server side has a set of default metrics that are used if this parameter is null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, |
||
metrics.sort(Comparator.comparing(EvaluationMetric::getName)); | ||
} | ||
this.metrics = metrics; | ||
} | ||
|
||
@Override | ||
|
@@ -102,11 +111,13 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par | |
builder.field(ACTUAL_FIELD.getPreferredName(), actualField); | ||
builder.field(PREDICTED_PROBABILITY_FIELD.getPreferredName(), predictedProbabilityField); | ||
|
||
builder.startObject(METRICS.getPreferredName()); | ||
for (EvaluationMetric metric : metrics) { | ||
builder.field(metric.getName(), metric); | ||
if (metrics != null) { | ||
builder.startObject(METRICS.getPreferredName()); | ||
for (EvaluationMetric metric : metrics) { | ||
builder.field(metric.getName(), metric); | ||
} | ||
builder.endObject(); | ||
} | ||
builder.endObject(); | ||
|
||
builder.endObject(); | ||
return builder; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.client.ml.dataframe.evaluation.regression; | ||
|
||
import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.test.AbstractXContentTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
public class RSquaredMetricResultTests extends AbstractXContentTestCase<RSquaredMetric.Result> { | ||
|
||
public static RSquaredMetric.Result randomResult() { | ||
return new RSquaredMetric.Result(randomDouble()); | ||
} | ||
|
||
@Override | ||
protected RSquaredMetric.Result createTestInstance() { | ||
return randomResult(); | ||
} | ||
|
||
@Override | ||
protected RSquaredMetric.Result doParseInstance(XContentParser parser) throws IOException { | ||
return RSquaredMetric.Result.fromXContent(parser); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected NamedXContentRegistry xContentRegistry() { | ||
return new NamedXContentRegistry(new MlEvaluationNamedXContentProvider().getNamedXContentParsers()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.